Skip to content

Commit 576ca60

Browse files
committed
Added more test
1 parent 36d7632 commit 576ca60

File tree

8 files changed

+115
-18
lines changed

8 files changed

+115
-18
lines changed

ellar_cli/service/cli.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
import typing as t
66

7+
import click
78
from ellar.app import App
89
from ellar.app.context import ApplicationContext
910
from ellar.common.constants import ELLAR_CONFIG_MODULE
@@ -297,16 +298,16 @@ def _import_from_string(self) -> t.Any:
297298
message = (
298299
'Attribute "{attrs_str}" not found in python module "{module_file}".'
299300
)
300-
raise Exception(
301+
raise click.ClickException(
301302
message.format(attrs_str=attrs_str, module_file=module.__file__)
302303
) from attr_ex
303304
return instance
304305

305-
def import_configuration(self) -> t.Type[Config]:
306+
def import_configuration(self) -> t.Type[Config]: # pragma: no cover
306307
raise Exception("Not Available")
307308

308309
def get_application_config(self) -> Config:
309310
return self.import_application().config
310311

311-
def import_root_module(self) -> t.Type[ModuleBase]:
312+
def import_root_module(self) -> t.Type[ModuleBase]: # pragma: no cover
312313
raise Exception("Not Available")

tests/conftest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os.path
22
import subprocess
33
import sys
4+
from pathlib import Path
45
from uuid import uuid4
56

67
import pytest
@@ -10,6 +11,17 @@
1011
from ellar_cli.service.pyproject import PY_PROJECT_TOML
1112
from ellar_cli.testing import EllarCliRunner
1213

14+
sample_app_path = os.path.join(Path(__file__).parent, "sample_app")
15+
16+
17+
@pytest.fixture()
18+
def change_os_dir():
19+
sys.path.append(sample_app_path)
20+
os.chdir(sample_app_path)
21+
print(f"working director - {os.getcwd()}")
22+
yield
23+
sys.path.remove(sample_app_path)
24+
1325

1426
@pytest.fixture
1527
def random_type():

tests/sample_app/apps/__init__.py

Whitespace-only changes.

tests/sample_app/apps/bad_app.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/env python
2+
import click
3+
from ellar.app import AppFactory
4+
5+
from ellar_cli.main import create_ellar_cli
6+
7+
8+
async def bootstrap():
9+
application = AppFactory.create_app()
10+
return application
11+
12+
13+
cli = create_ellar_cli("apps.bad_app:bootstrap")
14+
15+
16+
@cli.command()
17+
def working():
18+
click.echo("Working")
19+
20+
21+
if __name__ == "__main__":
22+
cli()

tests/sample_app/apps/bad_app_2.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/env python
2+
from ellar.app import AppFactory
3+
from ellar.samples import HomeModule
4+
5+
from ellar_cli.main import create_ellar_cli
6+
7+
8+
def bootstrap():
9+
application = AppFactory.create_app(modules=[HomeModule])
10+
return application
11+
12+
13+
cli = create_ellar_cli("apps.bad_app_2:bootstrap_unknown")
14+
15+
if __name__ == "__main__":
16+
cli()

tests/sample_app/apps/good_app.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/env python
2+
import click
3+
from ellar.app import AppFactory
4+
from ellar.samples import HomeModule
5+
6+
from ellar_cli.main import create_ellar_cli
7+
8+
9+
def bootstrap():
10+
application = AppFactory.create_app(modules=[HomeModule])
11+
return application
12+
13+
14+
cli = create_ellar_cli("apps.good_app:bootstrap")
15+
16+
17+
@cli.command()
18+
def working():
19+
click.echo("Working")
20+
21+
22+
if __name__ == "__main__":
23+
cli()

tests/test_app_and_ellar_command_group_command.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,10 @@
11
import importlib
2-
import os
32
import subprocess
4-
import sys
5-
from pathlib import Path
63
from unittest import mock
74

8-
import pytest
9-
10-
sample_app_path = os.path.join(Path(__file__).parent, "sample_app")
115
runserver = importlib.import_module("ellar_cli.manage_commands.runserver")
126

137

14-
@pytest.fixture()
15-
def change_os_dir():
16-
sys.path.append(sample_app_path)
17-
os.chdir(sample_app_path)
18-
print(f"working director - {os.getcwd()}")
19-
yield
20-
sys.path.remove(sample_app_path)
21-
22-
238
def test_ellar_command_group_works_for_default_project(change_os_dir):
249
result = subprocess.run(["ellar", "db", "create-migration"], stdout=subprocess.PIPE)
2510
assert result.returncode == 0

tests/test_ellar_cli_service.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import subprocess
23

34
import pytest
45
from ellar.app import App
@@ -11,6 +12,15 @@
1112
)
1213
from ellar_cli.service.pyproject import PY_PROJECT_TOML
1314

15+
good_app_info = (
16+
b"Usage: good_app.py [OPTIONS] COMMAND [ARGS]...\n\n "
17+
b"Ellar, ASGI Python Web framework\n\nOptions:\n "
18+
b" --project TEXT Run Specific Command on a specific project [default:\n "
19+
b" default]\n -v, --version Show the version and exit.\n --help "
20+
b"Show this message and exit.\n\nCommands:\n create-module - Scaffolds Ellar Application Module -\n "
21+
b"runserver - Starts Uvicorn Server -\n working\n"
22+
)
23+
1424

1525
def test_import_project_meta_returns_default_when_py_project_is_none(tmp_path):
1626
os.chdir(tmp_path)
@@ -180,3 +190,31 @@ def test_version_works(write_empty_py_project, process_runner):
180190
assert "Ellar CLI Version:" in str(result.stdout) and "Ellar Version:" in str(
181191
result.stdout
182192
)
193+
194+
195+
def test_apps_good_app_cli_works(change_os_dir):
196+
result = subprocess.run(["python", "apps/good_app.py"], stdout=subprocess.PIPE)
197+
assert result.returncode == 0
198+
assert result.stdout == good_app_info
199+
200+
201+
def test_apps_bad_app_fails(change_os_dir):
202+
result = subprocess.run(
203+
["python", "apps/bad_app.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
204+
)
205+
assert result.returncode == 1
206+
assert (
207+
result.stderr
208+
== b"Error: Coroutine Application Bootstrapping is not supported.\n"
209+
)
210+
211+
212+
def test_apps_bad_app_2_fails(change_os_dir):
213+
result = subprocess.run(
214+
["python", "apps/bad_app_2.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
215+
)
216+
assert result.returncode == 1
217+
assert (
218+
b'Error: Attribute "bootstrap_unknown" not found in python module'
219+
in result.stderr
220+
)

0 commit comments

Comments
 (0)