Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
janbuchar committed Sep 20, 2024
1 parent f15275b commit ef878b5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 16 deletions.
4 changes: 3 additions & 1 deletion src/crawlee/_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@ def create(
help='The URL where crawling should start. If none is given, you will be prompted.',
),
enable_apify_integration: Optional[bool] = typer.Option(
default=None,
None,
'--apify/--no-apify',
show_default=False,
help='Should Apify integration be set up for you? If not given, you will be prompted.',
),
Expand Down Expand Up @@ -189,6 +190,7 @@ def create(
'package_manager': package_manager,
'crawler_type': crawler_type,
'enable_apify_integration': enable_apify_integration,
'start_url': start_url,
},
)

Expand Down
104 changes: 89 additions & 15 deletions tests/unit/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ def test_create_interactive(mock_cookiecutter: Mock, monkeypatch: pytest.MonkeyP
*'my_project',
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
]
)
monkeypatch.setattr(target=readchar, name='readkey', value=lambda: next(mock_input))
Expand All @@ -35,9 +38,15 @@ def test_create_interactive(mock_cookiecutter: Mock, monkeypatch: pytest.MonkeyP

mock_cookiecutter.assert_called_with(
template='gh:apify/crawlee-python',
directory='templates/beautifulsoup',
directory='templates/crawler',
no_input=True,
extra_context={'project_name': 'my_project'},
extra_context={
'project_name': 'my_project',
'package_manager': 'poetry',
'crawler_type': 'beautifulsoup',
'enable_apify_integration': False,
'start_url': 'https://crawlee.dev',
},
)


Expand All @@ -48,6 +57,9 @@ def test_create_interactive_non_default_template(mock_cookiecutter: Mock, monkey
readchar.key.ENTER,
readchar.key.DOWN,
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
]
)
monkeypatch.setattr(target=readchar, name='readkey', value=lambda: next(mock_input))
Expand All @@ -57,20 +69,45 @@ def test_create_interactive_non_default_template(mock_cookiecutter: Mock, monkey

mock_cookiecutter.assert_called_with(
template='gh:apify/crawlee-python',
directory='templates/playwright',
directory='templates/crawler',
no_input=True,
extra_context={'project_name': 'my_project'},
extra_context={
'project_name': 'my_project',
'package_manager': 'poetry',
'crawler_type': 'parsel',
'enable_apify_integration': False,
'start_url': 'https://crawlee.dev',
},
)


def test_create_non_interactive(mock_cookiecutter: Mock) -> None:
runner.invoke(crawlee._cli.cli, ['create', 'my_project', '--template', 'playwright'])
runner.invoke(
crawlee._cli.cli,
[
'create',
'my_project',
'--crawler-type',
'playwright',
'--package-manager',
'pip',
'--start-url',
'https://yr.no',
'--no-apify',
],
)

mock_cookiecutter.assert_called_with(
template='gh:apify/crawlee-python',
directory='templates/playwright',
directory='templates/crawler',
no_input=True,
extra_context={'project_name': 'my_project'},
extra_context={
'project_name': 'my_project',
'package_manager': 'pip',
'crawler_type': 'playwright',
'start_url': 'https://yr.no',
'enable_apify_integration': False,
},
)


Expand All @@ -89,14 +126,33 @@ def test_create_existing_folder(
os.chdir(tmp)
(tmp / 'existing_project').mkdir()

result = runner.invoke(crawlee._cli.cli, ['create', 'existing_project', '--template', 'playwright'])
result = runner.invoke(
crawlee._cli.cli,
[
'create',
'existing_project',
'--crawler-type',
'playwright',
'--package-manager',
'pip',
'--start-url',
'https://yr.no',
'--no-apify',
],
)
assert 'existing_project already exists' in result.output

mock_cookiecutter.assert_called_with(
template='gh:apify/crawlee-python',
directory='templates/playwright',
directory='templates/crawler',
no_input=True,
extra_context={'project_name': 'my_project'},
extra_context={
'project_name': 'my_project',
'package_manager': 'pip',
'crawler_type': 'playwright',
'start_url': 'https://yr.no',
'enable_apify_integration': False,
},
)


Expand All @@ -109,6 +165,9 @@ def test_create_existing_folder_interactive(
readchar.key.ENTER,
*'my_project',
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
]
)
monkeypatch.setattr(target=readchar, name='readkey', value=lambda: next(mock_input))
Expand All @@ -122,9 +181,15 @@ def test_create_existing_folder_interactive(

mock_cookiecutter.assert_called_with(
template='gh:apify/crawlee-python',
directory='templates/playwright',
directory='templates/crawler',
no_input=True,
extra_context={'project_name': 'my_project'},
extra_context={
'project_name': 'my_project',
'package_manager': 'poetry',
'crawler_type': 'playwright',
'start_url': 'https://crawlee.dev',
'enable_apify_integration': False,
},
)


Expand All @@ -139,6 +204,9 @@ def test_create_existing_folder_interactive_multiple_attempts(
readchar.key.ENTER,
*'my_project',
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
readchar.key.ENTER,
]
)
monkeypatch.setattr(target=readchar, name='readkey', value=lambda: next(mock_input))
Expand All @@ -148,12 +216,18 @@ def test_create_existing_folder_interactive_multiple_attempts(
(tmp / 'existing_project').mkdir()
(tmp / 'existing_project_2').mkdir()

result = runner.invoke(crawlee._cli.cli, ['create', '--template', 'playwright'])
result = runner.invoke(crawlee._cli.cli, ['create', '--crawler-type', 'playwright'])
assert 'existing_project already exists' in result.output

mock_cookiecutter.assert_called_with(
template='gh:apify/crawlee-python',
directory='templates/playwright',
directory='templates/crawler',
no_input=True,
extra_context={'project_name': 'my_project'},
extra_context={
'project_name': 'my_project',
'package_manager': 'poetry',
'crawler_type': 'playwright',
'start_url': 'https://crawlee.dev',
'enable_apify_integration': False,
},
)

0 comments on commit ef878b5

Please sign in to comment.