Skip to content

Commit 8a7e34b

Browse files
committed
add api tests
1 parent 9a7f906 commit 8a7e34b

File tree

5 files changed

+97
-6
lines changed

5 files changed

+97
-6
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ freezegun = "^1.1.0"
2525
SQLAlchemy-Utils = "^0.38.3"
2626
pre-commit = "^2.20.0"
2727
click = "8.0.4"
28+
httpx = "^0.23.1"
29+
requests = "^2.28.1"
2830

2931
[tool.poetry.dev-dependencies]
3032
poethepoet = "^0.10.0"

src/api/tests/test_catalog.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
from modules.catalog.application.command import CreateListingDraftCommand
2+
from seedwork.domain.value_objects import Money
3+
4+
5+
def test_empty_catalog_list(api_client):
6+
response = api_client.get("/catalog")
7+
assert response.status_code == 200
8+
assert response.json() == {"data": []}
9+
10+
11+
def test_catalog_list_with_one_item(api, api_client):
12+
# arrange
13+
catalog_module = api.container.catalog_module()
14+
with catalog_module.unit_of_work():
15+
command_result = catalog_module.execute_command(
16+
CreateListingDraftCommand(
17+
title="Foo", description="Bar", ask_price=Money(10), seller_id="abcd"
18+
)
19+
)
20+
21+
# act
22+
response = api_client.get("/catalog")
23+
24+
# assert
25+
assert response.status_code == 200
26+
response_data = response.json()["data"]
27+
assert len(response_data) == 1
28+
assert response.json() == {
29+
"data": [
30+
{
31+
"id": str(command_result.result),
32+
"title": "Foo",
33+
"description": "Bar",
34+
"ask_price_amount": 10.0,
35+
"ask_price_currency": "USD",
36+
}
37+
]
38+
}
39+
40+
41+
def test_catalog_list_with_two_items(api, api_client):
42+
# arrange
43+
catalog_module = api.container.catalog_module()
44+
with catalog_module.unit_of_work():
45+
catalog_module.execute_command(
46+
CreateListingDraftCommand(
47+
title="Foo #1", description="Bar", ask_price=Money(10), seller_id="abcd"
48+
)
49+
)
50+
catalog_module.execute_command(
51+
CreateListingDraftCommand(
52+
title="Foo #2", description="Bar", ask_price=Money(10), seller_id="abcd"
53+
)
54+
)
55+
56+
# act
57+
response = api_client.get("/catalog")
58+
59+
# assert
60+
assert response.status_code == 200
61+
response_data = response.json()["data"]
62+
assert len(response_data) == 2

src/api/tests/test_common.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from fastapi.testclient import TestClient
2+
3+
from api.main import app
4+
5+
client = TestClient(app)
6+
7+
8+
def test_homepage_returns_200():
9+
response = client.get("/")
10+
assert response.status_code == 200
11+
12+
13+
def test_docs_page_returns_200():
14+
response = client.get("/docs")
15+
assert response.status_code == 200

src/conftest.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import pytest
2+
from fastapi.testclient import TestClient
23
from sqlalchemy import create_engine
34
from sqlalchemy.orm import Session
45

6+
from api.main import app
57
from config.api_config import ApiConfig
68
from seedwork.infrastructure.database import Base
79

@@ -14,7 +16,6 @@ def engine():
1416
with engine.begin() as connection:
1517
Base.metadata.drop_all(connection)
1618
Base.metadata.create_all(connection)
17-
1819
return engine
1920

2021

@@ -23,3 +24,14 @@ def db_session(engine):
2324

2425
with Session(engine) as session:
2526
yield session
27+
28+
29+
@pytest.fixture
30+
def api(engine):
31+
return app
32+
33+
34+
@pytest.fixture
35+
def api_client(api):
36+
client = TestClient(api)
37+
return client

src/seedwork/tests/application/test_decorators.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from dataclasses import dataclass
22

33
from seedwork.application.commands import Command
4-
from seedwork.application.decorators import command_handler, registry
4+
from seedwork.application.decorators import command_handler, query_handler, registry
55
from seedwork.application.queries import Query
66

77

@@ -45,9 +45,9 @@ def test_query_handler_decorator_registers_query_handler():
4545
class FooQuery(Query):
4646
...
4747

48-
@command_handler
49-
def foo_query_handler(command: FooQuery):
48+
@query_handler
49+
def foo_query_handler(query: FooQuery):
5050
...
5151

52-
assert registry.get_command_handler_for(FooQuery) == foo_query_handler
53-
assert registry.get_command_handler_parameters_for(FooQuery) == {}
52+
assert registry.get_query_handler_for(FooQuery) == foo_query_handler
53+
assert registry.get_query_handler_parameters_for(FooQuery) == {}

0 commit comments

Comments
 (0)