1+ from typing import Any
12from unittest .mock import AsyncMock
23
34import pytest
45from assertpy import assert_that
56from fastapi import Response
7+ from fastapi .encoders import jsonable_encoder
68from fastapi .testclient import TestClient
79from pytest_mock import MockerFixture
810
11+ from app .model .customer import Customer
12+
913
1014@pytest .fixture
1115def mock_execute (mocker : MockerFixture ) -> AsyncMock :
@@ -30,10 +34,17 @@ def test_customers_delete(mock_execute: AsyncMock, test_client: TestClient):
3034
3135
3236def test_customers_insert (mock_execute_many : AsyncMock , test_client : TestClient ):
33- initial_delete : Response = test_client .put ("/context/customers" , json = [])
34- assert_that (initial_delete .status_code ).is_equal_to (204 )
37+ customer1 = Customer (first_name = "fname1" , last_name = "lname1" )
38+ customer2 = Customer (first_name = "fname2" , last_name = "lname2" )
39+ input_json = jsonable_encoder ([customer1 , customer2 ])
40+ response : Response = test_client .put ("/context/customers" , json = input_json )
41+ assert_that (response .status_code ).is_equal_to (204 )
3542
3643 assert len (mock_execute_many .call_args_list ) == 1
3744 assert_that (mock_execute_many .call_args .kwargs ["query" ]).is_equal_to (
3845 "INSERT INTO CUSTOMERS(FIRST_NAME, LAST_NAME) VALUES (:first_name, :last_name)"
3946 )
47+ inserted_rows : list [dict [str , Any ]] = mock_execute_many .call_args .kwargs ["values" ]
48+ assert_that (inserted_rows ).is_length (2 )
49+ assert_that (inserted_rows ).contains (jsonable_encoder (customer1 ))
50+ assert_that (inserted_rows ).contains (jsonable_encoder (customer2 ))
0 commit comments