Skip to content

Commit

Permalink
Added more test
Browse files Browse the repository at this point in the history
  • Loading branch information
eadwinCode committed Jan 13, 2024
1 parent 36d7632 commit 576ca60
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 18 deletions.
7 changes: 4 additions & 3 deletions ellar_cli/service/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import sys
import typing as t

import click
from ellar.app import App
from ellar.app.context import ApplicationContext
from ellar.common.constants import ELLAR_CONFIG_MODULE
Expand Down Expand Up @@ -297,16 +298,16 @@ def _import_from_string(self) -> t.Any:
message = (
'Attribute "{attrs_str}" not found in python module "{module_file}".'
)
raise Exception(
raise click.ClickException(
message.format(attrs_str=attrs_str, module_file=module.__file__)
) from attr_ex
return instance

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

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

Check warning on line 310 in ellar_cli/service/cli.py

View check run for this annotation

Codecov / codecov/patch

ellar_cli/service/cli.py#L310

Added line #L310 was not covered by tests

def import_root_module(self) -> t.Type[ModuleBase]:
def import_root_module(self) -> t.Type[ModuleBase]: # pragma: no cover
raise Exception("Not Available")
12 changes: 12 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os.path
import subprocess
import sys
from pathlib import Path
from uuid import uuid4

import pytest
Expand All @@ -10,6 +11,17 @@
from ellar_cli.service.pyproject import PY_PROJECT_TOML
from ellar_cli.testing import EllarCliRunner

sample_app_path = os.path.join(Path(__file__).parent, "sample_app")


@pytest.fixture()
def change_os_dir():
sys.path.append(sample_app_path)
os.chdir(sample_app_path)
print(f"working director - {os.getcwd()}")
yield
sys.path.remove(sample_app_path)


@pytest.fixture
def random_type():
Expand Down
Empty file.
22 changes: 22 additions & 0 deletions tests/sample_app/apps/bad_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/env python
import click
from ellar.app import AppFactory

from ellar_cli.main import create_ellar_cli


async def bootstrap():
application = AppFactory.create_app()
return application


cli = create_ellar_cli("apps.bad_app:bootstrap")


@cli.command()
def working():
click.echo("Working")


if __name__ == "__main__":
cli()
16 changes: 16 additions & 0 deletions tests/sample_app/apps/bad_app_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/env python
from ellar.app import AppFactory
from ellar.samples import HomeModule

from ellar_cli.main import create_ellar_cli


def bootstrap():
application = AppFactory.create_app(modules=[HomeModule])
return application


cli = create_ellar_cli("apps.bad_app_2:bootstrap_unknown")

if __name__ == "__main__":
cli()
23 changes: 23 additions & 0 deletions tests/sample_app/apps/good_app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/env python
import click
from ellar.app import AppFactory
from ellar.samples import HomeModule

from ellar_cli.main import create_ellar_cli


def bootstrap():
application = AppFactory.create_app(modules=[HomeModule])
return application


cli = create_ellar_cli("apps.good_app:bootstrap")


@cli.command()
def working():
click.echo("Working")


if __name__ == "__main__":
cli()
15 changes: 0 additions & 15 deletions tests/test_app_and_ellar_command_group_command.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,10 @@
import importlib
import os
import subprocess
import sys
from pathlib import Path
from unittest import mock

import pytest

sample_app_path = os.path.join(Path(__file__).parent, "sample_app")
runserver = importlib.import_module("ellar_cli.manage_commands.runserver")


@pytest.fixture()
def change_os_dir():
sys.path.append(sample_app_path)
os.chdir(sample_app_path)
print(f"working director - {os.getcwd()}")
yield
sys.path.remove(sample_app_path)


def test_ellar_command_group_works_for_default_project(change_os_dir):
result = subprocess.run(["ellar", "db", "create-migration"], stdout=subprocess.PIPE)
assert result.returncode == 0
Expand Down
38 changes: 38 additions & 0 deletions tests/test_ellar_cli_service.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import subprocess

import pytest
from ellar.app import App
Expand All @@ -11,6 +12,15 @@
)
from ellar_cli.service.pyproject import PY_PROJECT_TOML

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


def test_import_project_meta_returns_default_when_py_project_is_none(tmp_path):
os.chdir(tmp_path)
Expand Down Expand Up @@ -180,3 +190,31 @@ def test_version_works(write_empty_py_project, process_runner):
assert "Ellar CLI Version:" in str(result.stdout) and "Ellar Version:" in str(
result.stdout
)


def test_apps_good_app_cli_works(change_os_dir):
result = subprocess.run(["python", "apps/good_app.py"], stdout=subprocess.PIPE)
assert result.returncode == 0
assert result.stdout == good_app_info


def test_apps_bad_app_fails(change_os_dir):
result = subprocess.run(
["python", "apps/bad_app.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
assert result.returncode == 1
assert (
result.stderr
== b"Error: Coroutine Application Bootstrapping is not supported.\n"
)


def test_apps_bad_app_2_fails(change_os_dir):
result = subprocess.run(
["python", "apps/bad_app_2.py"], stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
assert result.returncode == 1
assert (
b'Error: Attribute "bootstrap_unknown" not found in python module'
in result.stderr
)

0 comments on commit 576ca60

Please sign in to comment.