33
44import pytest
55from assertpy import assert_that
6- from fastapi import Response
76from fastapi .encoders import jsonable_encoder
87from fastapi .testclient import TestClient
8+ from httpx import Response
99from pytest_mock import MockerFixture
1010
1111from app .model .customer import Customer
@@ -19,12 +19,16 @@ def mock_execute(mocker: MockerFixture) -> AsyncMock:
1919
2020@pytest .fixture
2121def mock_execute_many (mocker : MockerFixture ) -> AsyncMock :
22- mock_execute_many : AsyncMock = mocker .patch (
23- "app.db.database_config.database.execute_many"
24- )
22+ mock_execute_many : AsyncMock = mocker .patch ("app.db.database_config.database.execute_many" )
2523 return mock_execute_many
2624
2725
26+ @pytest .fixture
27+ def mock_fetch_all (mocker : MockerFixture ) -> AsyncMock :
28+ mock_fetch_all : AsyncMock = mocker .patch ("app.db.database_config.database.fetch_all" )
29+ return mock_fetch_all
30+
31+
2832def test_customers_delete (mock_execute : AsyncMock , test_client : TestClient ):
2933 initial_delete : Response = test_client .delete ("/context/customers" )
3034 assert_that (initial_delete .status_code ).is_equal_to (204 )
@@ -48,3 +52,24 @@ def test_customers_insert(mock_execute_many: AsyncMock, test_client: TestClient)
4852 assert_that (inserted_rows ).is_length (2 )
4953 assert_that (inserted_rows ).contains (jsonable_encoder (customer1 ))
5054 assert_that (inserted_rows ).contains (jsonable_encoder (customer2 ))
55+
56+
57+ def test_customers_read (mock_fetch_all : AsyncMock , test_client : TestClient ):
58+ customer1 = Customer (first_name = "fname1" , last_name = "lname1" )
59+ customer2 = Customer (first_name = "fname2" , last_name = "lname2" )
60+
61+ mock_fetch_all .return_value = jsonable_encoder ([customer1 , customer2 ])
62+
63+ response : Response = test_client .get ("/context/customers" )
64+
65+ assert_that (response .status_code ).is_equal_to (200 )
66+ response_json : list [dict [str , Any ]] = response .json ()
67+
68+ assert_that (response_json ).is_length (2 )
69+ assert_that (response_json ).contains (jsonable_encoder (customer1 ))
70+ assert_that (response_json ).contains (jsonable_encoder (customer2 ))
71+
72+ assert len (mock_fetch_all .call_args_list ) == 1
73+ assert_that (mock_fetch_all .call_args .kwargs ["query" ]).is_equal_to (
74+ "SELECT * FROM CUSTOMERS"
75+ )
0 commit comments