Skip to content

Commit 87c6fae

Browse files
Verifying inserts
1 parent c9ef759 commit 87c6fae

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

tests_unit/routers/test_customer.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
from typing import Any
12
from unittest.mock import AsyncMock
23

34
import pytest
45
from assertpy import assert_that
56
from fastapi import Response
7+
from fastapi.encoders import jsonable_encoder
68
from fastapi.testclient import TestClient
79
from pytest_mock import MockerFixture
810

11+
from app.model.customer import Customer
12+
913

1014
@pytest.fixture
1115
def mock_execute(mocker: MockerFixture) -> AsyncMock:
@@ -30,10 +34,17 @@ def test_customers_delete(mock_execute: AsyncMock, test_client: TestClient):
3034

3135

3236
def 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

Comments
 (0)