1+ from sqlalchemy import text , select
12from allocation .domain import model
23from datetime import date
34
45
56def test_orderline_mapper_can_load_lines (session ):
67 session .execute (
7- "INSERT INTO order_lines (orderid, sku, qty) VALUES "
8- '("order1", "RED-CHAIR", 12),'
9- '("order1", "RED-TABLE", 13),'
10- '("order2", "BLUE-LIPSTICK", 14)'
8+ text (
9+ "INSERT INTO order_lines (orderid, sku, qty) VALUES "
10+ '("order1", "RED-CHAIR", 12),'
11+ '("order1", "RED-TABLE", 13),'
12+ '("order2", "BLUE-LIPSTICK", 14)'
13+ )
1114 )
1215 expected = [
1316 model .OrderLine ("order1" , "RED-CHAIR" , 12 ),
1417 model .OrderLine ("order1" , "RED-TABLE" , 13 ),
1518 model .OrderLine ("order2" , "BLUE-LIPSTICK" , 14 ),
1619 ]
17- assert session .query ( model .OrderLine ).all () == expected
20+ assert session .scalars ( select ( model .OrderLine ) ).all () == expected
1821
1922
2023def test_orderline_mapper_can_save_lines (session ):
2124 new_line = model .OrderLine ("order1" , "DECORATIVE-WIDGET" , 12 )
2225 session .add (new_line )
2326 session .commit ()
2427
25- rows = list (session .execute ('SELECT orderid, sku, qty FROM "order_lines"' ))
28+ rows = list (session .execute (text ( 'SELECT orderid, sku, qty FROM "order_lines"' ) ))
2629 assert rows == [("order1" , "DECORATIVE-WIDGET" , 12 )]
2730
2831
2932def test_retrieving_batches (session ):
3033 session .execute (
31- "INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
32- ' VALUES ("batch1", "sku1", 100, null)'
34+ text (
35+ "INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
36+ ' VALUES ("batch1", "sku1", 100, null)'
37+ )
3338 )
3439 session .execute (
35- "INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
36- ' VALUES ("batch2", "sku2", 200, "2011-04-11")'
40+ text (
41+ "INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
42+ ' VALUES ("batch2", "sku2", 200, "2011-04-11")'
43+ )
3744 )
3845 expected = [
3946 model .Batch ("batch1" , "sku1" , 100 , eta = None ),
4047 model .Batch ("batch2" , "sku2" , 200 , eta = date (2011 , 4 , 11 )),
4148 ]
4249
43- assert session .query ( model .Batch ).all () == expected
50+ assert session .scalars ( select ( model .Batch ) ).all () == expected
4451
4552
4653def test_saving_batches (session ):
4754 batch = model .Batch ("batch1" , "sku1" , 100 , eta = None )
4855 session .add (batch )
4956 session .commit ()
5057 rows = session .execute (
51- 'SELECT reference, sku, _purchased_quantity, eta FROM "batches"'
58+ text ( 'SELECT reference, sku, _purchased_quantity, eta FROM "batches"' )
5259 )
5360 assert list (rows ) == [("batch1" , "sku1" , 100 , None )]
5461
@@ -59,31 +66,33 @@ def test_saving_allocations(session):
5966 batch .allocate (line )
6067 session .add (batch )
6168 session .commit ()
62- rows = list (session .execute ('SELECT orderline_id, batch_id FROM "allocations"' ))
69+ rows = list (session .execute (text ( 'SELECT orderline_id, batch_id FROM "allocations"' ) ))
6370 assert rows == [(batch .id , line .id )]
6471
6572
6673def test_retrieving_allocations (session ):
6774 session .execute (
68- 'INSERT INTO order_lines (orderid, sku, qty) VALUES ("order1", "sku1", 12)'
75+ text ( 'INSERT INTO order_lines (orderid, sku, qty) VALUES ("order1", "sku1", 12)' )
6976 )
7077 [[olid ]] = session .execute (
71- "SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku" ,
78+ text ( "SELECT id FROM order_lines WHERE orderid=:orderid AND sku=:sku" ) ,
7279 dict (orderid = "order1" , sku = "sku1" ),
7380 )
7481 session .execute (
75- "INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
76- ' VALUES ("batch1", "sku1", 100, null)'
82+ text (
83+ "INSERT INTO batches (reference, sku, _purchased_quantity, eta)"
84+ ' VALUES ("batch1", "sku1", 100, null)'
85+ )
7786 )
7887 [[bid ]] = session .execute (
79- "SELECT id FROM batches WHERE reference=:ref AND sku=:sku" ,
88+ text ( "SELECT id FROM batches WHERE reference=:ref AND sku=:sku" ) ,
8089 dict (ref = "batch1" , sku = "sku1" ),
8190 )
8291 session .execute (
83- "INSERT INTO allocations (orderline_id, batch_id) VALUES (:olid, :bid)" ,
92+ text ( "INSERT INTO allocations (orderline_id, batch_id) VALUES (:olid, :bid)" ) ,
8493 dict (olid = olid , bid = bid ),
8594 )
8695
87- batch = session .query ( model .Batch ).one ()
96+ batch = session .scalars ( select ( model .Batch ) ).one ()
8897
8998 assert batch ._allocations == {model .OrderLine ("order1" , "sku1" , 12 )}
0 commit comments