11from datetime import date
22from unittest import mock
33import pytest
4-
54from allocation .adapters import repository
6- from allocation .domain import events
5+ from allocation .domain import commands , events
76from allocation .service_layer import handlers , messagebus , unit_of_work
87
98
@@ -39,56 +38,58 @@ def rollback(self):
3938 pass
4039
4140
41+
4242class TestAddBatch :
4343
4444 @staticmethod
4545 def test_for_new_product ():
4646 uow = FakeUnitOfWork ()
47- messagebus .handle (events . BatchCreated ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None ), uow )
47+ messagebus .handle (commands . CreateBatch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None ), uow )
4848 assert uow .products .get ("CRUNCHY-ARMCHAIR" ) is not None
4949 assert uow .committed
5050
5151 @staticmethod
5252 def test_for_existing_product ():
5353 uow = FakeUnitOfWork ()
54- messagebus .handle (events . BatchCreated ("b1" , "GARISH-RUG" , 100 , None ), uow )
55- messagebus .handle (events . BatchCreated ("b2" , "GARISH-RUG" , 99 , None ), uow )
54+ messagebus .handle (commands . CreateBatch ("b1" , "GARISH-RUG" , 100 , None ), uow )
55+ messagebus .handle (commands . CreateBatch ("b2" , "GARISH-RUG" , 99 , None ), uow )
5656 assert "b2" in [b .reference for b in uow .products .get ("GARISH-RUG" ).batches ]
5757
5858
5959
6060class TestAllocate :
6161
6262 @staticmethod
63- def test_returns_allocation ():
63+ def test_allocates ():
6464 uow = FakeUnitOfWork ()
65- messagebus .handle (events .BatchCreated ("b1" , "COMPLICATED-LAMP" , 100 , None ), uow )
66- results = messagebus .handle (events .AllocationRequired ("o1" , "COMPLICATED-LAMP" , 10 ), uow )
67- assert results .pop () == "b1"
65+ messagebus .handle (commands .CreateBatch ("b1" , "COMPLICATED-LAMP" , 100 , None ), uow )
66+ result = messagebus .handle (commands .Allocate ("o1" , "COMPLICATED-LAMP" , 10 ), uow )
67+ assert result == "b1"
68+ [batch ] = uow .products .get ("COMPLICATED-LAMP" ).batches
69+ assert batch .available_quantity == 90
6870
6971 @staticmethod
7072 def test_errors_for_invalid_sku ():
7173 uow = FakeUnitOfWork ()
72- messagebus .handle (events . BatchCreated ("b1" , "AREALSKU" , 100 , None ), uow )
74+ messagebus .handle (commands . CreateBatch ("b1" , "AREALSKU" , 100 , None ), uow )
7375
7476 with pytest .raises (handlers .InvalidSku , match = "Invalid sku NONEXISTENTSKU" ):
75- messagebus .handle (events .AllocationRequired ("o1" , "NONEXISTENTSKU" , 10 ) , uow )
76-
77+ messagebus .handle (commands .Allocate ("o1" , "NONEXISTENTSKU" , 10 ), uow )
7778
7879 @staticmethod
7980 def test_commits ():
8081 uow = FakeUnitOfWork ()
81- messagebus .handle (events . BatchCreated ("b1" , "OMINOUS-MIRROR" , 100 , None ), uow )
82- messagebus .handle (events . AllocationRequired ("o1" , "OMINOUS-MIRROR" , 10 ), uow )
82+ messagebus .handle (commands . CreateBatch ("b1" , "OMINOUS-MIRROR" , 100 , None ), uow )
83+ messagebus .handle (commands . Allocate ("o1" , "OMINOUS-MIRROR" , 10 ), uow )
8384 assert uow .committed
8485
8586 @staticmethod
8687 def test_sends_email_on_out_of_stock_error ():
8788 uow = FakeUnitOfWork ()
88- messagebus .handle (events . BatchCreated ("b1" , "POPULAR-CURTAINS" , 9 , None ), uow )
89+ messagebus .handle (commands . CreateBatch ("b1" , "POPULAR-CURTAINS" , 9 , None ), uow )
8990
9091 with mock .patch ("allocation.adapters.email.send" ) as mock_send_mail :
91- messagebus .handle (events . AllocationRequired ("o1" , "POPULAR-CURTAINS" , 10 ), uow )
92+ messagebus .handle (commands . Allocate ("o1" , "POPULAR-CURTAINS" , 10 ), uow )
9293 assert mock_send_mail .call_args == mock .call (
9394 "stock@made.com" ,
9495 f"Out of stock for POPULAR-CURTAINS" ,
@@ -100,26 +101,26 @@ class TestChangeBatchQuantity:
100101 @staticmethod
101102 def test_changes_available_quantity ():
102103 uow = FakeUnitOfWork ()
103- messagebus .handle (events . BatchCreated ("batch1" , "ADORABLE-SETTEE" , 100 , None ), uow )
104+ messagebus .handle (commands . CreateBatch ("batch1" , "ADORABLE-SETTEE" , 100 , None ), uow )
104105 [batch ] = uow .products .get (sku = "ADORABLE-SETTEE" ).batches
105106 assert batch .available_quantity == 100
106107
107- messagebus .handle (events . BatchQuantityChanged ("batch1" , 50 ), uow )
108+ messagebus .handle (commands . ChangeBatchQuantity ("batch1" , 50 ), uow )
108109
109110 assert batch .available_quantity == 50
110111
111112
112113 @staticmethod
113114 def test_reallocates_if_necessary ():
114115 uow = FakeUnitOfWork ()
115- messagebus .handle (events . BatchCreated ("batch1" , "INDIFFERENT-TABLE" , 50 , None ), uow )
116- messagebus .handle (events . BatchCreated ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()), uow )
117- messagebus .handle (events . AllocationRequired ("order1" , "INDIFFERENT-TABLE" , 20 ), uow )
118- messagebus .handle (events . AllocationRequired ("order2" , "INDIFFERENT-TABLE" , 20 ), uow )
116+ messagebus .handle (commands . CreateBatch ("batch1" , "INDIFFERENT-TABLE" , 50 , None ), uow )
117+ messagebus .handle (commands . CreateBatch ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()), uow )
118+ messagebus .handle (commands . Allocate ("order1" , "INDIFFERENT-TABLE" , 20 ), uow )
119+ messagebus .handle (commands . Allocate ("order2" , "INDIFFERENT-TABLE" , 20 ), uow )
119120 [batch1 , batch2 ] = uow .products .get (sku = "INDIFFERENT-TABLE" ).batches
120121 assert batch1 .available_quantity == 10
121122
122- messagebus .handle (events . BatchQuantityChanged ("batch1" , 25 ), uow )
123+ messagebus .handle (commands . ChangeBatchQuantity ("batch1" , 25 ), uow )
123124
124125 # order1 or order2 will be deallocated, so we"ll have 25 - 20 * 1
125126 assert batch1 .available_quantity == 5
0 commit comments