Skip to content

Commit 7705e5d

Browse files
with transactions
1 parent 0973140 commit 7705e5d

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

app/routers/customer.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,24 @@
1616

1717
@router.get("")
1818
async def read_customers() -> list[Customer]:
19-
rows = await database.fetch_all(query=SELECT_ALL_CUSTOMERS)
20-
customers: list[Customer] = list(map(lambda row: Customer(**dict(row)), rows))
21-
return customers
19+
async with database.transaction():
20+
rows = await database.fetch_all(query=SELECT_ALL_CUSTOMERS)
21+
customers: list[Customer] = list(map(lambda row: Customer(**dict(row)), rows))
22+
return customers
2223

2324

2425
@router.put("", status_code=status.HTTP_204_NO_CONTENT, tags=["users"])
2526
async def insert_customers(customers: list[Customer]) -> None:
26-
rows: list[dict[str, Any]] = list(map(lambda customer: vars(customer), customers))
27-
await database.execute_many(query=INSERT_CUSTOMER, values=rows)
28-
return None
27+
async with database.transaction():
28+
rows: list[dict[str, Any]] = list(
29+
map(lambda customer: vars(customer), customers)
30+
)
31+
await database.execute_many(query=INSERT_CUSTOMER, values=rows)
32+
return None
2933

3034

3135
@router.delete("", status_code=status.HTTP_204_NO_CONTENT, tags=["users"])
3236
async def delete_all_customers() -> None:
33-
await database.execute(query=DELETE_ALL_CUSTOMERS)
34-
return None
37+
async with database.transaction():
38+
await database.execute(query=DELETE_ALL_CUSTOMERS)
39+
return None

tests_unit/routers/test_customer.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@
1313
CUSTOMERS_ENDPOINT = "/context/customers"
1414

1515

16+
@pytest.fixture
17+
def mock_transaction(mocker: MockerFixture) -> AsyncMock:
18+
mock_transaction: AsyncMock = mocker.patch(
19+
"app.db.database_config.database.transaction"
20+
)
21+
return mock_transaction
22+
23+
1624
@pytest.fixture
1725
def mock_execute(mocker: MockerFixture) -> AsyncMock:
1826
mock_execute: AsyncMock = mocker.patch("app.db.database_config.database.execute")
@@ -35,15 +43,19 @@ def mock_fetch_all(mocker: MockerFixture) -> AsyncMock:
3543
return mock_fetch_all
3644

3745

38-
def test_customers_delete(mock_execute: AsyncMock, test_client: TestClient):
46+
def test_customers_delete(
47+
mock_transaction: AsyncMock, mock_execute: AsyncMock, test_client: TestClient
48+
):
3949
response: Response = test_client.delete(CUSTOMERS_ENDPOINT)
4050
assert_that(response.status_code).is_equal_to(204)
4151

4252
assert len(mock_execute.call_args_list) == 1
4353
assert mock_execute.call_args.kwargs["query"] == "TRUNCATE TABLE CUSTOMERS"
4454

4555

46-
def test_customers_insert(mock_execute_many: AsyncMock, test_client: TestClient):
56+
def test_customers_insert(
57+
mock_transaction: AsyncMock, mock_execute_many: AsyncMock, test_client: TestClient
58+
):
4759
customer1 = Customer(first_name="fname1", last_name="lname1")
4860
customer2 = Customer(first_name="fname2", last_name="lname2")
4961
input_json = jsonable_encoder([customer1, customer2])
@@ -61,7 +73,9 @@ def test_customers_insert(mock_execute_many: AsyncMock, test_client: TestClient)
6173
assert_that(inserted_rows).contains(jsonable_encoder(customer2))
6274

6375

64-
def test_customers_read(mock_fetch_all: AsyncMock, test_client: TestClient):
76+
def test_customers_read(
77+
mock_transaction: AsyncMock, mock_fetch_all: AsyncMock, test_client: TestClient
78+
):
6579
customer1 = Customer(first_name="fname1", last_name="lname1")
6680
customer2 = Customer(first_name="fname2", last_name="lname2")
6781

0 commit comments

Comments
 (0)