Skip to content

Commit

Permalink
feat: add unit tests for routes
Browse files Browse the repository at this point in the history
Signed-off-by: majorchork <[email protected]>
  • Loading branch information
majorchork committed Mar 20, 2023
1 parent 6915413 commit 94d7f29
Show file tree
Hide file tree
Showing 12 changed files with 239 additions and 0 deletions.
Empty file added src/tests/__init__.py
Empty file.
111 changes: 111 additions & 0 deletions src/tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import pytest
from fastapi.testclient import TestClient
from schemas.base import BaseArgs
from schemas.schedule import SchedulerArgs
from schemas.suite import SuiteArgs
from schemas.kill import KillArgs


from main import app

client = TestClient(app)


@pytest.fixture(scope="module")
def test_client() -> TestClient:
"""
create test client instance
"""
client = TestClient(app)
yield client


@pytest.fixture(scope="module")
def suite_payload() -> dict:
"""
create test client instance
"""
payload = {
"--ceph": "wip-dis-testing-2",
"--ceph-repo": "https://github.com/ceph/ceph-ci.git",
"--kernel": "distro",
"--limit": "2",
"--newest": "0",
"--machine-type": "testnode",
"--num": "1",
"--priority": "70",
"--suite": "teuthology:no-ceph",
"--suite-branch": "wip-dis-testing-2",
"--suite-repo": "https://github.com/ceph/ceph-ci.git",
"--teuthology-branch": "main",
"--verbose": "1",
"--user": "vallariag"
}

return payload


@pytest.fixture(scope="module")
def base_schema_payload() -> tuple:
payload: dict = {
"--dry-run":True,
"--non-interactive":True,
"--verbose":1,
"--help":True,
"--user":"vallariag"
}

return (payload, BaseArgs )


@pytest.fixture(scope="module")
def schedule_schema_payload() -> tuple:
payload: dict = {
"--owner":"vallariag",
"--seed":"-4",
"--force-priority":True,
"--no-nested-subset":True,
"--job-threshold":"627",
}

return (payload, SchedulerArgs)




@pytest.fixture(scope="module")
def suite_schema_payload() -> tuple:
payload: dict = {
"--ceph": "wip-dis-testing-2",
"--ceph-repo": "https://github.com/ceph/ceph-ci.git",
"--kernel": "distro",
"--limit": "2",
"--newest": "0",
"--machine-type": "testnode",
"--num": "1",
"--priority": "70",
"--suite": "teuthology:no-ceph",
"--suite-branch": "wip-dis-testing-2",
"--suite-repo": "https://github.com/ceph/ceph-ci.git",
"--teuthology-branch": "main",
"--verbose": "1",
"--user": "vallariag"
}
return (payload, SuiteArgs)


@pytest.fixture(scope="module")
def kill_schema_payload() -> tuple:
payload: dict = {

"--run": "run job",
"--preserve-queue": True,
"--job": ["job1", "job2"],
"--jobspec": "do job",
"--machine-type": "debian",
"--archive": "",
"--user": "vallariag",
"--dry-run": False,
}

return (payload, KillArgs)
Empty file.
10 changes: 10 additions & 0 deletions src/tests/test_routes/test_kill
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


def test_create_kill(test_client, payload):
response = test_client.post(
"/kill",
headers={"Content-Type": "application/json"},
params={"dry_run": False, "logs":True},
json=payload,
)
assert response.status_code == 200
3 changes: 3 additions & 0 deletions src/tests/test_routes/test_login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def test_git_login(test_client):
response = test_client.get("/login")
assert response.status_code == 302
7 changes: 7 additions & 0 deletions src/tests/test_routes/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@


def test_root(test_client):
response = test_client.get("/")
assert response.status_code == 200
assert response.json().get("root") == "success"
assert len(response.json()) == 2
22 changes: 22 additions & 0 deletions src/tests/test_routes/test_suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
def test_create_suite(test_client, suite_payload):
response = test_client.post(
"/suite",
headers={"Content-Type": "application/json"},
params={"dry_run": False, "logs":True},
json=suite_payload,
)
assert response.status_code == 200
assert response.get("run") == {}
assert len(response.get("logs")) > 0


def test_failed_create_suite(test_client, suite_payload):
response = test_client.post(
"/suite",
headers={"Content-Type": "application/json"},
params={"dry_run": False, "logs":True},
json=suite_payload,
)
assert response.status_code == 401


Empty file.
15 changes: 15 additions & 0 deletions src/tests/test_schemas/test_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@



from schemas.base import BaseArgs

def test_base_schema(base_schema_payload):
payload, BaseArgs = base_schema_payload
base_args = BaseArgs(**payload)

assert base_args.dry_run == payload.get("--dry-run")
assert base_args.non_interactive == payload.get("--non-interactive")
assert base_args.help == payload.get("--help")
assert base_args.user == payload.get("--user")
assert base_args.verbose == payload.get("--verbose")

22 changes: 22 additions & 0 deletions src/tests/test_schemas/test_kill.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

def test_kill_schema(base_schema_payload, kill_schema_payload):
"""
test ScheduleArgs schema which inherits from BaseArgs
use the base_schema fixtures
"""
bs_payload, _ = base_schema_payload
ks_payload, KilArgs = kill_schema_payload

ks_payload.update(bs_payload)

kill_args = KilArgs(**ks_payload)

assert kill_args.owner == ks_payload.get("--owner")
assert kill_args.run == ks_payload.get("--run")
assert kill_args.preserve_queue == ks_payload.get("--preserve-queue")
assert kill_args.job == ks_payload.get("--job")
assert kill_args.machine_type == ks_payload.get("--machine-type")
assert kill_args.archive == ks_payload.get("--archive")



29 changes: 29 additions & 0 deletions src/tests/test_schemas/test_schedule.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

def test_schedule_schema(base_schema_payload, schedule_schema_payload):
"""
test ScheduleArgs schema which inherits from BaseArgs
use the base_schema fixtures
"""
bs_payload, _ = base_schema_payload

ss_payload, SchedulerArgs = schedule_schema_payload

ss_payload.update(bs_payload)

schedule_args = SchedulerArgs(**ss_payload)

assert schedule_args.dry_run == ss_payload.get("--dry-run")
assert schedule_args.non_interactive == ss_payload.get("--non-interactive")
assert schedule_args.help == ss_payload.get("--help")
assert schedule_args.user == ss_payload.get("--user")
assert schedule_args.filter_all == None
assert schedule_args.subset == None
assert schedule_args.owner == ss_payload.get("--owner")
assert schedule_args.force_priority == ss_payload.get("--force-priority")
assert schedule_args.no_nested_subset == ss_payload.get("--no-nested-subset")
assert schedule_args.job_threshold == ss_payload.get("--job-threshold")





20 changes: 20 additions & 0 deletions src/tests/test_schemas/test_suite.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

def test_suite_schema(base_schema_payload, suite_schema_payload):
"""
test ScheduleArgs schema which inherits from BaseArgs
use the base_schema fixtures
"""
bs_payload, _ = base_schema_payload
ss_payload, SuiteArgs = suite_schema_payload

ss_payload.update(bs_payload)

suite_args = SuiteArgs(**ss_payload)

assert suite_args.dry_run == ss_payload.get("--dry-run")
assert suite_args.suite == ss_payload.get("--suite")
assert suite_args.suite_branch == ss_payload.get("--suite-branch")
assert suite_args.suite_repo == ss_payload.get("--suite-repo")



0 comments on commit 94d7f29

Please sign in to comment.