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
@@ -42,52 +41,54 @@ class TestAddBatch:
4241 def test_for_new_product (self ):
4342 uow = FakeUnitOfWork ()
4443 messagebus .handle (
45- events . BatchCreated ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None ), uow
44+ commands . CreateBatch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None ), uow
4645 )
4746 assert uow .products .get ("CRUNCHY-ARMCHAIR" ) is not None
4847 assert uow .committed
4948
5049 def test_for_existing_product (self ):
5150 uow = FakeUnitOfWork ()
52- messagebus .handle (events . BatchCreated ("b1" , "GARISH-RUG" , 100 , None ), uow )
53- messagebus .handle (events . BatchCreated ("b2" , "GARISH-RUG" , 99 , None ), uow )
51+ messagebus .handle (commands . CreateBatch ("b1" , "GARISH-RUG" , 100 , None ), uow )
52+ messagebus .handle (commands . CreateBatch ("b2" , "GARISH-RUG" , 99 , None ), uow )
5453 assert "b2" in [b .reference for b in uow .products .get ("GARISH-RUG" ).batches ]
5554
5655
5756class TestAllocate :
58- def test_returns_allocation (self ):
57+ def test_allocates (self ):
5958 uow = FakeUnitOfWork ()
6059 messagebus .handle (
61- events . BatchCreated ("batch1" , "COMPLICATED-LAMP" , 100 , None ), uow
60+ commands . CreateBatch ("batch1" , "COMPLICATED-LAMP" , 100 , None ), uow
6261 )
6362 results = messagebus .handle (
64- events . AllocationRequired ("o1" , "COMPLICATED-LAMP" , 10 ), uow
63+ commands . Allocate ("o1" , "COMPLICATED-LAMP" , 10 ), uow
6564 )
6665 assert results .pop (0 ) == "batch1"
66+ [batch ] = uow .products .get ("COMPLICATED-LAMP" ).batches
67+ assert batch .available_quantity == 90
6768
6869 def test_errors_for_invalid_sku (self ):
6970 uow = FakeUnitOfWork ()
70- messagebus .handle (events . BatchCreated ("b1" , "AREALSKU" , 100 , None ), uow )
71+ messagebus .handle (commands . CreateBatch ("b1" , "AREALSKU" , 100 , None ), uow )
7172
7273 with pytest .raises (handlers .InvalidSku , match = "Invalid sku NONEXISTENTSKU" ):
73- messagebus .handle (
74- events .AllocationRequired ("o1" , "NONEXISTENTSKU" , 10 ), uow
75- )
74+ messagebus .handle (commands .Allocate ("o1" , "NONEXISTENTSKU" , 10 ), uow )
7675
7776 def test_commits (self ):
7877 uow = FakeUnitOfWork ()
79- messagebus .handle (events .BatchCreated ("b1" , "OMINOUS-MIRROR" , 100 , None ), uow )
80- messagebus .handle (events .AllocationRequired ("o1" , "OMINOUS-MIRROR" , 10 ), uow )
78+ messagebus .handle (
79+ commands .CreateBatch ("b1" , "OMINOUS-MIRROR" , 100 , None ), uow
80+ )
81+ messagebus .handle (commands .Allocate ("o1" , "OMINOUS-MIRROR" , 10 ), uow )
8182 assert uow .committed
8283
8384 def test_sends_email_on_out_of_stock_error (self ):
8485 uow = FakeUnitOfWork ()
85- messagebus .handle (events .BatchCreated ("b1" , "POPULAR-CURTAINS" , 9 , None ), uow )
86+ messagebus .handle (
87+ commands .CreateBatch ("b1" , "POPULAR-CURTAINS" , 9 , None ), uow
88+ )
8689
8790 with mock .patch ("allocation.adapters.email.send" ) as mock_send_mail :
88- messagebus .handle (
89- events .AllocationRequired ("o1" , "POPULAR-CURTAINS" , 10 ), uow
90- )
91+ messagebus .handle (commands .Allocate ("o1" , "POPULAR-CURTAINS" , 10 ), uow )
9192 assert mock_send_mail .call_args == mock .call (
9293 "stock@made.com" , f"Out of stock for POPULAR-CURTAINS"
9394 )
@@ -97,30 +98,30 @@ class TestChangeBatchQuantity:
9798 def test_changes_available_quantity (self ):
9899 uow = FakeUnitOfWork ()
99100 messagebus .handle (
100- events . BatchCreated ("batch1" , "ADORABLE-SETTEE" , 100 , None ), uow
101+ commands . CreateBatch ("batch1" , "ADORABLE-SETTEE" , 100 , None ), uow
101102 )
102103 [batch ] = uow .products .get (sku = "ADORABLE-SETTEE" ).batches
103104 assert batch .available_quantity == 100
104105
105- messagebus .handle (events . BatchQuantityChanged ("batch1" , 50 ), uow )
106+ messagebus .handle (commands . ChangeBatchQuantity ("batch1" , 50 ), uow )
106107
107108 assert batch .available_quantity == 50
108109
109110 def test_reallocates_if_necessary (self ):
110111 uow = FakeUnitOfWork ()
111- event_history = [
112- events . BatchCreated ("batch1" , "INDIFFERENT-TABLE" , 50 , None ),
113- events . BatchCreated ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()),
114- events . AllocationRequired ("order1" , "INDIFFERENT-TABLE" , 20 ),
115- events . AllocationRequired ("order2" , "INDIFFERENT-TABLE" , 20 ),
112+ history = [
113+ commands . CreateBatch ("batch1" , "INDIFFERENT-TABLE" , 50 , None ),
114+ commands . CreateBatch ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()),
115+ commands . Allocate ("order1" , "INDIFFERENT-TABLE" , 20 ),
116+ commands . Allocate ("order2" , "INDIFFERENT-TABLE" , 20 ),
116117 ]
117- for e in event_history :
118- messagebus .handle (e , uow )
118+ for msg in history :
119+ messagebus .handle (msg , uow )
119120 [batch1 , batch2 ] = uow .products .get (sku = "INDIFFERENT-TABLE" ).batches
120121 assert batch1 .available_quantity == 10
121122 assert batch2 .available_quantity == 50
122123
123- messagebus .handle (events . BatchQuantityChanged ("batch1" , 25 ), uow )
124+ messagebus .handle (commands . ChangeBatchQuantity ("batch1" , 25 ), uow )
124125
125126 # order1 or order2 will be deallocated, so we'll have 25 - 20
126127 assert batch1 .available_quantity == 5
0 commit comments