22import pytest
33from domain import model
44from adapters import repository
5+ from typing import Callable
6+ from sqlalchemy .ext .asyncio import AsyncSession
7+ from sqlalchemy import text
8+
9+ AsyncSessionMaker = Callable [[], AsyncSession ]
510
611
712@pytest .mark .asyncio
8- async def test_repository_can_save_a_batch (session_maker ) :
13+ async def test_repository_can_save_a_batch (session_maker : AsyncSessionMaker ) -> None :
914 batch = model .Batch ("batch1" , "RUSTY-SOAPDISH" , 100 , eta = None )
1015
1116 async with session_maker () as session :
@@ -15,57 +20,77 @@ async def test_repository_can_save_a_batch(session_maker):
1520 async with session_maker () as session :
1621 async with session .begin ():
1722 rows = await session .execute (
18- 'SELECT ref, sku, _purchased_quantity, eta FROM "batches"'
23+ text ( 'SELECT ref, sku, _purchased_quantity, eta FROM "batches"' )
1924 )
2025 assert list (rows ) == [("batch1" , "RUSTY-SOAPDISH" , 100 , None )]
2126
22- if False :
23- def insert_order_line (session ):
24- session .execute (
27+
28+ async def insert_order_line (session : AsyncSession ) -> int :
29+ await session .execute (
30+ text (
2531 "INSERT INTO order_lines (orderid, sku, qty)"
2632 ' VALUES ("order1", "GENERIC-SOFA", 12)'
2733 )
28- [[orderline_id ]] = session .execute (
29- "SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku" ,
30- dict (orderid = "order1" , sku = "GENERIC-SOFA" ),
31- )
32- return orderline_id
34+ )
35+ [[orderline_id ]] = await session .execute (
36+ text ("SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku" ),
37+ dict (orderid = "order1" , sku = "GENERIC-SOFA" ),
38+ )
39+ return int (orderline_id )
3340
3441
35- def insert_batch (session , batch_id ):
36- session .execute (
37- "INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
38- ' VALUES (:batch_id, "GENERIC-SOFA", 100, null)' ,
39- dict (batch_id = batch_id ),
40- )
41- [[batch_id ]] = session .execute (
42- 'SELECT id FROM batches WHERE reference=:batch_id AND sku="GENERIC-SOFA"' ,
43- dict (batch_id = batch_id ),
44- )
45- return batch_id
42+ async def insert_batch (session : AsyncSession , batch_ref : str ) -> int :
43+ await session .execute (
44+ text (
45+ "INSERT INTO batches (ref, sku, _purchased_quantity, eta)"
46+ ' VALUES (:batch_ref, "GENERIC-SOFA", 100, null)'
47+ ),
48+ dict (batch_ref = batch_ref ),
49+ )
50+ [[batch_id ]] = await session .execute (
51+ text (
52+ 'SELECT id FROM batches WHERE ref=:batch_ref AND sku="GENERIC-SOFA"'
53+ ),
54+ dict (batch_ref = batch_ref ),
55+ )
56+ return int (batch_id )
4657
4758
48- def insert_allocation (session , orderline_id , batch_id ):
49- session .execute (
59+ async def insert_allocation (
60+ session : AsyncSession , orderline_id : int , batch_id : int
61+ ) -> None :
62+ await session .execute (
63+ text (
5064 "INSERT INTO allocations (orderline_id, batch_id)"
5165 " VALUES (:orderline_id, :batch_id)" ,
52- dict (orderline_id = orderline_id , batch_id = batch_id ),
53- )
66+ ),
67+ dict (orderline_id = orderline_id , batch_id = batch_id ),
68+ )
5469
5570
56- def test_repository_can_retrieve_a_batch_with_allocations (session ):
57- orderline_id = insert_order_line (session )
58- batch1_id = insert_batch (session , "batch1" )
59- insert_batch (session , "batch2" )
60- insert_allocation (session , orderline_id , batch1_id )
71+ @pytest .mark .asyncio
72+ async def test_repository_can_retrieve_a_batch_with_allocations (
73+ session_maker : AsyncSessionMaker ,
74+ ) -> None :
75+ async with session_maker () as session :
76+ async with session .begin ():
77+ orderline_id = await insert_order_line (session )
78+ batch1_id = await insert_batch (session , "batch1" )
79+ await insert_batch (session , "batch2" )
80+ await insert_allocation (session , orderline_id , batch1_id )
81+
82+ async with session_maker () as session :
83+ rows = await session .execute ("SELECT * from batches" )
84+ print (list (rows ))
6185
86+ async with session_maker () as session :
6287 repo = repository .SqlAlchemyRepository (session )
63- retrieved = repo .get ("batch1" )
64-
65- expected = model .Batch ("batch1" , "GENERIC-SOFA" , 100 , eta = None )
66- assert retrieved == expected # Batch.__eq__ only compares reference
67- assert retrieved .sku == expected .sku
68- assert retrieved ._purchased_quantity == expected ._purchased_quantity
69- assert retrieved ._allocations == {
70- model .OrderLine ("order1" , "GENERIC-SOFA" , 12 ),
71- }
88+ retrieved = await repo .get ("batch1" )
89+
90+ expected = model .Batch ("batch1" , "GENERIC-SOFA" , 100 , eta = None )
91+ assert retrieved == expected # Batch.__eq__ only compares ref
92+ assert retrieved .sku == expected .sku
93+ assert retrieved ._purchased_quantity == expected ._purchased_quantity
94+ assert retrieved ._allocations == {
95+ model .OrderLine ("order1" , "GENERIC-SOFA" , 12 ),
96+ }
0 commit comments