11from datetime import date
22from unittest import mock
33import pytest
4- from allocation import events , handlers , exceptions , repository , unit_of_work
4+ from allocation import events , exceptions , messagebus , repository , unit_of_work
55
66
77class FakeRepository (repository .AbstractRepository ):
@@ -42,15 +42,17 @@ class TestAddBatch:
4242 @staticmethod
4343 def test_for_new_product ():
4444 uow = FakeUnitOfWork ()
45- handlers . add_batch ( "b1" , "CRUNCHY-ARMCHAIR" , 100 , None , uow )
45+ messagebus . handle ([ events . BatchCreated ( "b1" , "CRUNCHY-ARMCHAIR" , 100 , None )] , uow )
4646 assert uow .products .get ("CRUNCHY-ARMCHAIR" ) is not None
4747 assert uow .committed
4848
4949 @staticmethod
5050 def test_for_existing_product ():
5151 uow = FakeUnitOfWork ()
52- handlers .add_batch ("b1" , "GARISH-RUG" , 100 , None , uow )
53- handlers .add_batch ("b2" , "GARISH-RUG" , 99 , None , uow )
52+ messagebus .handle ([
53+ events .BatchCreated ("b1" , "GARISH-RUG" , 100 , None ),
54+ events .BatchCreated ("b2" , "GARISH-RUG" , 99 , None ),
55+ ], uow )
5456 assert "b2" in [b .reference for b in uow .products .get ("GARISH-RUG" ).batches ]
5557
5658
@@ -67,32 +69,41 @@ class TestAllocate:
6769 @staticmethod
6870 def test_returns_allocation ():
6971 uow = FakeUnitOfWork ()
70- handlers .add_batch ("batch1" , "COMPLICATED-LAMP" , 100 , None , uow )
71- result = handlers .allocate ("o1" , "COMPLICATED-LAMP" , 10 , uow )
72- assert result == "batch1"
72+ result = messagebus .handle ([
73+ events .BatchCreated ("b1" , "COMPLICATED-LAMP" , 100 , None ),
74+ events .AllocationRequest ("o1" , "COMPLICATED-LAMP" , 10 )
75+ ], uow )
76+ assert result == "b1"
7377
7478 @staticmethod
7579 def test_errors_for_invalid_sku ():
7680 uow = FakeUnitOfWork ()
77- handlers . add_batch ( "b1" , "AREALSKU" , 100 , None , uow )
81+ messagebus . handle ([ events . BatchCreated ( "b1" , "AREALSKU" , 100 , None )] , uow )
7882
7983 with pytest .raises (exceptions .InvalidSku , match = "Invalid sku NONEXISTENTSKU" ):
80- handlers .allocate ("o1" , "NONEXISTENTSKU" , 10 , uow )
84+ messagebus .handle ([
85+ events .AllocationRequest ("o1" , "NONEXISTENTSKU" , 10 )
86+ ], uow )
87+
8188
8289 @staticmethod
8390 def test_commits ():
8491 uow = FakeUnitOfWork ()
85- handlers .add_batch ("b1" , "OMINOUS-MIRROR" , 100 , None , uow )
86- handlers .allocate ("o1" , "OMINOUS-MIRROR" , 10 , uow )
92+ messagebus .handle ([
93+ events .BatchCreated ("b1" , "OMINOUS-MIRROR" , 100 , None ),
94+ events .AllocationRequest ("o1" , "OMINOUS-MIRROR" , 10 ),
95+ ], uow )
8796 assert uow .committed
8897
8998 @staticmethod
9099 def test_sends_email_on_out_of_stock_error ():
91100 uow = FakeUnitOfWork ()
92- handlers . add_batch ( "b1" , "POPULAR-CURTAINS" , 9 , None , uow )
101+ messagebus . handle ([ events . BatchCreated ( "b1" , "POPULAR-CURTAINS" , 9 , None )] , uow )
93102
94- with mock .patch ("allocation.email.send_mail" ) as mock_send_mail :
95- handlers .allocate ("o1" , "POPULAR-CURTAINS" , 10 , uow )
103+ with mock .patch ("allocation.email.send" ) as mock_send_mail :
104+ messagebus .handle ([
105+ events .AllocationRequest ("o1" , "POPULAR-CURTAINS" , 10 )
106+ ], uow )
96107 assert mock_send_mail .call_args == mock .call (
97108 "stock@made.com" ,
98109 f"Out of stock for POPULAR-CURTAINS" ,
@@ -104,26 +115,28 @@ class TestChangeBatchQuantity:
104115 @staticmethod
105116 def test_changes_available_quantity ():
106117 uow = FakeUnitOfWork ()
107- handlers . add_batch ( "batch1" , "ADORABLE-SETTEE" , 100 , None , uow )
118+ messagebus . handle ([ events . BatchCreated ( "batch1" , "ADORABLE-SETTEE" , 100 , None )] , uow )
108119 [batch ] = uow .products .get (sku = "ADORABLE-SETTEE" ).batches
109120 assert batch .available_quantity == 100
110121
111- handlers . change_batch_quantity ( "batch1" , 50 , uow )
122+ messagebus . handle ([ events . BatchQuantityChanged ( "batch1" , 50 )] , uow )
112123
113124 assert batch .available_quantity == 50
114125
115126
116127 @staticmethod
117128 def test_reallocates_if_necessary ():
118129 uow = FakeUnitOfWork ()
119- handlers .add_batch ("batch1" , "INDIFFERENT-TABLE" , 50 , None , uow )
120- handlers .add_batch ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today (), uow )
121- handlers .allocate ("order1" , "INDIFFERENT-TABLE" , 20 , uow )
122- handlers .allocate ("order2" , "INDIFFERENT-TABLE" , 20 , uow )
123- [batch1 , batch2 ] = uow .products .get (sku = "sku1" ).batches
130+ messagebus .handle ([
131+ events .BatchCreated ("batch1" , "INDIFFERENT-TABLE" , 50 , None ),
132+ events .BatchCreated ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()),
133+ events .AllocationRequest ("order1" , "INDIFFERENT-TABLE" , 20 ),
134+ events .AllocationRequest ("order2" , "INDIFFERENT-TABLE" , 20 ),
135+ ], uow )
136+ [batch1 , batch2 ] = uow .products .get (sku = "INDIFFERENT-TABLE" ).batches
124137 assert batch1 .available_quantity == 10
125138
126- handlers . change_batch_quantity ( "batch1" , 25 , uow )
139+ messagebus . handle ([ events . BatchQuantityChanged ( "batch1" , 25 )] , uow )
127140
128141 # order1 or order2 will be deallocated, so we"ll have 25 - 20 * 1
129142 assert batch1 .available_quantity == 5
0 commit comments