Skip to content

Commit 47a9236

Browse files
Complete integration tests
1 parent 895905e commit 47a9236

File tree

2 files changed

+43
-7
lines changed

2 files changed

+43
-7
lines changed

tests_integration/routers/test_customer.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
from typing import Any
2+
13
import pytest
24
from assertpy import assert_that
3-
from fastapi import Response
5+
from fastapi.encoders import jsonable_encoder
46
from fastapi.testclient import TestClient
57
from importlib import reload
8+
9+
from httpx import Response
10+
611
import app.routers.customer as customer
12+
from app.model.customer import Customer
713

814

915
@pytest.fixture
@@ -16,5 +22,30 @@ def test_customers_insert_read_delete(
1622
test_client: TestClient,
1723
reload_customer_router: None,
1824
):
19-
initial_delete: Response = test_client.delete("/context/customers")
20-
assert_that(initial_delete.status_code).is_equal_to(204)
25+
initial_delete_response: Response = test_client.delete("/context/customers")
26+
assert_that(initial_delete_response.status_code).is_equal_to(204)
27+
28+
customer1 = Customer(first_name="fname1", last_name="lname1")
29+
customer2 = Customer(first_name="fname2", last_name="lname2")
30+
input_json = jsonable_encoder([customer1, customer2])
31+
32+
create_response: Response = test_client.put("/context/customers", json=input_json)
33+
assert_that(create_response.status_code).is_equal_to(204)
34+
35+
read_response: Response = test_client.get("/context/customers")
36+
37+
assert_that(read_response.status_code).is_equal_to(200)
38+
response_json: list[dict[str, Any]] = read_response.json()
39+
40+
assert_that(response_json).is_length(2)
41+
assert_that(response_json).contains(jsonable_encoder(customer1))
42+
assert_that(response_json).contains(jsonable_encoder(customer2))
43+
44+
delete_response: Response = test_client.delete("/context/customers")
45+
assert_that(delete_response.status_code).is_equal_to(204)
46+
47+
read_response_after_delete: Response = test_client.get("/context/customers")
48+
49+
assert_that(read_response_after_delete.status_code).is_equal_to(200)
50+
response_json_after_delete: list[dict[str, Any]] = read_response_after_delete.json()
51+
assert_that(response_json_after_delete).is_length(0)

tests_unit/routers/test_customer.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,23 @@ def mock_execute(mocker: MockerFixture) -> AsyncMock:
1919

2020
@pytest.fixture
2121
def mock_execute_many(mocker: MockerFixture) -> AsyncMock:
22-
mock_execute_many: AsyncMock = mocker.patch("app.db.database_config.database.execute_many")
22+
mock_execute_many: AsyncMock = mocker.patch(
23+
"app.db.database_config.database.execute_many"
24+
)
2325
return mock_execute_many
2426

2527

2628
@pytest.fixture
2729
def mock_fetch_all(mocker: MockerFixture) -> AsyncMock:
28-
mock_fetch_all: AsyncMock = mocker.patch("app.db.database_config.database.fetch_all")
30+
mock_fetch_all: AsyncMock = mocker.patch(
31+
"app.db.database_config.database.fetch_all"
32+
)
2933
return mock_fetch_all
3034

3135

3236
def test_customers_delete(mock_execute: AsyncMock, test_client: TestClient):
33-
initial_delete: Response = test_client.delete("/context/customers")
34-
assert_that(initial_delete.status_code).is_equal_to(204)
37+
response: Response = test_client.delete("/context/customers")
38+
assert_that(response.status_code).is_equal_to(204)
3539

3640
assert len(mock_execute.call_args_list) == 1
3741
assert mock_execute.call_args.kwargs["query"] == "TRUNCATE TABLE CUSTOMERS"
@@ -41,6 +45,7 @@ def test_customers_insert(mock_execute_many: AsyncMock, test_client: TestClient)
4145
customer1 = Customer(first_name="fname1", last_name="lname1")
4246
customer2 = Customer(first_name="fname2", last_name="lname2")
4347
input_json = jsonable_encoder([customer1, customer2])
48+
4449
response: Response = test_client.put("/context/customers", json=input_json)
4550
assert_that(response.status_code).is_equal_to(204)
4651

0 commit comments

Comments
 (0)