22from datetime import date
33from unittest import mock
44import pytest
5-
65from allocation .adapters import repository
7- from allocation .domain import events
6+ from allocation .domain import commands , events
87from allocation .service_layer import handlers , messagebus , unit_of_work
98
109
@@ -46,61 +45,63 @@ class TestAddBatch:
4645 def test_for_new_product (self ):
4746 uow = FakeUnitOfWork ()
4847 messagebus .handle (
49- events . BatchCreated ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None ), uow
48+ commands . CreateBatch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None ), uow
5049 )
5150 assert uow .products .get ("CRUNCHY-ARMCHAIR" ) is not None
5251 assert uow .committed
5352
5453
5554 def test_for_existing_product (self ):
5655 uow = FakeUnitOfWork ()
57- messagebus .handle (events . BatchCreated ("b1" , "GARISH-RUG" , 100 , None ), uow )
58- messagebus .handle (events . BatchCreated ("b2" , "GARISH-RUG" , 99 , None ), uow )
56+ messagebus .handle (commands . CreateBatch ("b1" , "GARISH-RUG" , 100 , None ), uow )
57+ messagebus .handle (commands . CreateBatch ("b2" , "GARISH-RUG" , 99 , None ), uow )
5958 assert "b2" in [b .reference for b in uow .products .get ("GARISH-RUG" ).batches ]
6059
6160
6261class TestAllocate :
6362
64- def test_returns_allocation (self ):
63+ def test_allocates (self ):
6564 uow = FakeUnitOfWork ()
6665 messagebus .handle (
67- events . BatchCreated ("batch1" , "COMPLICATED-LAMP" , 100 , None ), uow
66+ commands . CreateBatch ("batch1" , "COMPLICATED-LAMP" , 100 , None ), uow
6867 )
6968 results = messagebus .handle (
70- events . AllocationRequired ("o1" , "COMPLICATED-LAMP" , 10 ), uow
69+ commands . Allocate ("o1" , "COMPLICATED-LAMP" , 10 ), uow
7170 )
7271 assert results .pop (0 ) == "batch1"
72+ [batch ] = uow .products .get ("COMPLICATED-LAMP" ).batches
73+ assert batch .available_quantity == 90
7374
7475
7576 def test_errors_for_invalid_sku (self ):
7677 uow = FakeUnitOfWork ()
77- messagebus .handle (events . BatchCreated ("b1" , "AREALSKU" , 100 , None ), uow )
78+ messagebus .handle (commands . CreateBatch ("b1" , "AREALSKU" , 100 , None ), uow )
7879
7980 with pytest .raises (handlers .InvalidSku , match = "Invalid sku NONEXISTENTSKU" ):
8081 messagebus .handle (
81- events . AllocationRequired ("o1" , "NONEXISTENTSKU" , 10 ), uow
82+ commands . Allocate ("o1" , "NONEXISTENTSKU" , 10 ), uow
8283 )
8384
8485 def test_commits (self ):
8586 uow = FakeUnitOfWork ()
8687 messagebus .handle (
87- events . BatchCreated ("b1" , "OMINOUS-MIRROR" , 100 , None ), uow
88+ commands . CreateBatch ("b1" , "OMINOUS-MIRROR" , 100 , None ), uow
8889 )
8990 messagebus .handle (
90- events . AllocationRequired ("o1" , "OMINOUS-MIRROR" , 10 ), uow
91+ commands . Allocate ("o1" , "OMINOUS-MIRROR" , 10 ), uow
9192 )
9293 assert uow .committed
9394
9495
9596 def test_sends_email_on_out_of_stock_error (self ):
9697 uow = FakeUnitOfWork ()
9798 messagebus .handle (
98- events . BatchCreated ("b1" , "POPULAR-CURTAINS" , 9 , None ), uow
99+ commands . CreateBatch ("b1" , "POPULAR-CURTAINS" , 9 , None ), uow
99100 )
100101
101102 with mock .patch ("allocation.adapters.email.send" ) as mock_send_mail :
102103 messagebus .handle (
103- events . AllocationRequired ("o1" , "POPULAR-CURTAINS" , 10 ), uow
104+ commands . Allocate ("o1" , "POPULAR-CURTAINS" , 10 ), uow
104105 )
105106 assert mock_send_mail .call_args == mock .call (
106107 "stock@made.com" , f"Out of stock for POPULAR-CURTAINS"
@@ -113,31 +114,31 @@ class TestChangeBatchQuantity:
113114 def test_changes_available_quantity (self ):
114115 uow = FakeUnitOfWork ()
115116 messagebus .handle (
116- events . BatchCreated ("batch1" , "ADORABLE-SETTEE" , 100 , None ), uow
117+ commands . CreateBatch ("batch1" , "ADORABLE-SETTEE" , 100 , None ), uow
117118 )
118119 [batch ] = uow .products .get (sku = "ADORABLE-SETTEE" ).batches
119120 assert batch .available_quantity == 100
120121
121- messagebus .handle (events . BatchQuantityChanged ("batch1" , 50 ), uow )
122+ messagebus .handle (commands . ChangeBatchQuantity ("batch1" , 50 ), uow )
122123
123124 assert batch .available_quantity == 50
124125
125126
126127 def test_reallocates_if_necessary (self ):
127128 uow = FakeUnitOfWork ()
128- event_history = [
129- events . BatchCreated ("batch1" , "INDIFFERENT-TABLE" , 50 , None ),
130- events . BatchCreated ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()),
131- events . AllocationRequired ("order1" , "INDIFFERENT-TABLE" , 20 ),
132- events . AllocationRequired ("order2" , "INDIFFERENT-TABLE" , 20 ),
129+ history = [
130+ commands . CreateBatch ("batch1" , "INDIFFERENT-TABLE" , 50 , None ),
131+ commands . CreateBatch ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()),
132+ commands . Allocate ("order1" , "INDIFFERENT-TABLE" , 20 ),
133+ commands . Allocate ("order2" , "INDIFFERENT-TABLE" , 20 ),
133134 ]
134- for e in event_history :
135- messagebus .handle (e , uow )
135+ for msg in history :
136+ messagebus .handle (msg , uow )
136137 [batch1 , batch2 ] = uow .products .get (sku = "INDIFFERENT-TABLE" ).batches
137138 assert batch1 .available_quantity == 10
138139 assert batch2 .available_quantity == 50
139140
140- messagebus .handle (events . BatchQuantityChanged ("batch1" , 25 ), uow )
141+ messagebus .handle (commands . ChangeBatchQuantity ("batch1" , 25 ), uow )
141142
142143 # order1 or order2 will be deallocated, so we'll have 25 - 20
143144 assert batch1 .available_quantity == 5
0 commit comments