|
| 1 | +from datetime import datetime |
| 2 | + |
1 | 3 | import pytest
|
| 4 | +from asyncpg import Pool |
2 | 5 |
|
3 |
| -from api.resolvers.mutations import delete_reservation_resolver |
4 | 6 | from api.models import Reservation
|
| 7 | +from api.resolvers.mutations import delete_reservation_resolver |
5 | 8 |
|
6 |
| -from . import mock_db_session, patch_db_to_resolvers |
| 9 | +from . import MOCK_EXECUTION_CONTEXT |
7 | 10 |
|
8 | 11 |
|
9 | 12 | class DescribeDeleteReservationResolver:
|
10 |
| - @pytest.mark.usefixtures("patch_db_to_resolvers") |
11 |
| - def should_delete_reservation(self, mock_db_session): |
12 |
| - mock_db_session.query.return_value.all.return_value = [ |
| 13 | + def find_by_id(self, reservation_id): |
| 14 | + return [ |
| 15 | + reservation |
| 16 | + for reservation in self.reservations |
| 17 | + if reservation.id != reservation_id |
| 18 | + ] |
| 19 | + |
| 20 | + @pytest.mark.asyncio |
| 21 | + async def should_delete_existing_reservation(self, mocker): |
| 22 | + original_reservations = [ |
13 | 23 | Reservation(
|
14 | 24 | id=1,
|
15 | 25 | room_id="room_1",
|
16 |
| - checkin_date="2023-01-01", |
17 |
| - checkout_date="2023-01-03", |
18 |
| - ) |
| 26 | + checkin_date=datetime(2023, 1, 1, 5, 0), |
| 27 | + checkout_date=datetime(2023, 1, 1, 5, 0), |
| 28 | + total_charge=100, |
| 29 | + ), |
| 30 | + Reservation( |
| 31 | + id=2, |
| 32 | + room_id="room_2", |
| 33 | + checkin_date=datetime(2023, 1, 1, 5, 0), |
| 34 | + checkout_date=datetime(2023, 1, 1, 5, 0), |
| 35 | + total_charge=200, |
| 36 | + ), |
19 | 37 | ]
|
20 |
| - mock_execution_context = {"context_key": "context_value"} |
21 | 38 |
|
22 |
| - result = delete_reservation_resolver( |
23 |
| - None, mock_execution_context, reservationId=1 |
| 39 | + existing_reservation_id = 1 |
| 40 | + expected_reservations = [ |
| 41 | + reservation |
| 42 | + for reservation in original_reservations |
| 43 | + if reservation.id != existing_reservation_id |
| 44 | + ] |
| 45 | + |
| 46 | + mock_pool = mocker.AsyncMock(spec=Pool) |
| 47 | + mocker.patch("asyncpg.create_pool", return_value=mock_pool) |
| 48 | + mocker.patch( |
| 49 | + "api.resolvers.mutations.DbSession", |
| 50 | + mocker.AsyncMock(return_value=mock_pool), |
| 51 | + ) |
| 52 | + mock_pool.fetch = mocker.AsyncMock(return_value=expected_reservations) |
| 53 | + |
| 54 | + result = await delete_reservation_resolver( |
| 55 | + None, MOCK_EXECUTION_CONTEXT, reservationId=existing_reservation_id |
24 | 56 | )
|
25 | 57 |
|
26 | 58 | assert result["success"] is True
|
27 |
| - assert result["reservation"] is not None |
| 59 | + assert len(result["reservations"]) == len(original_reservations) - 1 |
| 60 | + assert result["reservations"] == expected_reservations |
| 61 | + |
| 62 | + @pytest.mark.asyncio |
| 63 | + async def test_should_give_reservation_not_found(self, mocker): |
| 64 | + result = await delete_reservation_resolver( |
| 65 | + None, MOCK_EXECUTION_CONTEXT, reservationId=999 |
| 66 | + ) |
| 67 | + |
| 68 | + assert result["success"] is False |
| 69 | + assert "Reservation with id of 999 not found" in result["errors"] |
0 commit comments