11from datetime import date
22from unittest import mock
33import pytest
4- from allocation import events , exceptions , messagebus , repository , unit_of_work
4+ from allocation import commands , exceptions , messagebus , repository , unit_of_work
55
66
77class FakeRepository (repository .AbstractRepository ):
@@ -36,56 +36,58 @@ def rollback(self):
3636 pass
3737
3838
39+
3940class TestAddBatch :
4041
4142 @staticmethod
4243 def test_for_new_product ():
4344 uow = FakeUnitOfWork ()
44- messagebus .handle (events . BatchCreated ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None ), uow )
45+ messagebus .handle (commands . CreateBatch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None ), uow )
4546 assert uow .products .get ("CRUNCHY-ARMCHAIR" ) is not None
4647 assert uow .committed
4748
4849 @staticmethod
4950 def test_for_existing_product ():
5051 uow = FakeUnitOfWork ()
51- messagebus .handle (events . BatchCreated ("b1" , "GARISH-RUG" , 100 , None ), uow )
52- messagebus .handle (events . BatchCreated ("b2" , "GARISH-RUG" , 99 , None ), uow )
52+ messagebus .handle (commands . CreateBatch ("b1" , "GARISH-RUG" , 100 , None ), uow )
53+ messagebus .handle (commands . CreateBatch ("b2" , "GARISH-RUG" , 99 , None ), uow )
5354 assert "b2" in [b .reference for b in uow .products .get ("GARISH-RUG" ).batches ]
5455
5556
5657
5758class TestAllocate :
5859
5960 @staticmethod
60- def test_returns_allocation ():
61+ def test_allocates ():
6162 uow = FakeUnitOfWork ()
62- messagebus .handle (events .BatchCreated ("b1" , "COMPLICATED-LAMP" , 100 , None ), uow )
63- results = messagebus .handle (events .AllocationRequired ("o1" , "COMPLICATED-LAMP" , 10 ), uow )
64- assert results .pop () == "b1"
63+ messagebus .handle (commands .CreateBatch ("b1" , "COMPLICATED-LAMP" , 100 , None ), uow )
64+ result = messagebus .handle (commands .Allocate ("o1" , "COMPLICATED-LAMP" , 10 ), uow )
65+ assert result == "b1"
66+ [batch ] = uow .products .get ("COMPLICATED-LAMP" ).batches
67+ assert batch .available_quantity == 90
6568
6669 @staticmethod
6770 def test_errors_for_invalid_sku ():
6871 uow = FakeUnitOfWork ()
69- messagebus .handle (events . BatchCreated ("b1" , "AREALSKU" , 100 , None ), uow )
72+ messagebus .handle (commands . CreateBatch ("b1" , "AREALSKU" , 100 , None ), uow )
7073
7174 with pytest .raises (exceptions .InvalidSku , match = "Invalid sku NONEXISTENTSKU" ):
72- messagebus .handle (events .AllocationRequired ("o1" , "NONEXISTENTSKU" , 10 ) , uow )
73-
75+ messagebus .handle (commands .Allocate ("o1" , "NONEXISTENTSKU" , 10 ) , uow )
7476
7577 @staticmethod
7678 def test_commits ():
7779 uow = FakeUnitOfWork ()
78- messagebus .handle (events . BatchCreated ("b1" , "OMINOUS-MIRROR" , 100 , None ), uow )
79- messagebus .handle (events . AllocationRequired ("o1" , "OMINOUS-MIRROR" , 10 ), uow )
80+ messagebus .handle (commands . CreateBatch ("b1" , "OMINOUS-MIRROR" , 100 , None ), uow )
81+ messagebus .handle (commands . Allocate ("o1" , "OMINOUS-MIRROR" , 10 ), uow )
8082 assert uow .committed
8183
8284 @staticmethod
8385 def test_sends_email_on_out_of_stock_error ():
8486 uow = FakeUnitOfWork ()
85- messagebus .handle (events . BatchCreated ("b1" , "POPULAR-CURTAINS" , 9 , None ), uow )
87+ messagebus .handle (commands . CreateBatch ("b1" , "POPULAR-CURTAINS" , 9 , None ), uow )
8688
8789 with mock .patch ("allocation.email.send" ) as mock_send_mail :
88- messagebus .handle (events . AllocationRequired ("o1" , "POPULAR-CURTAINS" , 10 ), uow )
90+ messagebus .handle (commands . Allocate ("o1" , "POPULAR-CURTAINS" , 10 ), uow )
8991 assert mock_send_mail .call_args == mock .call (
90929193 f"Out of stock for POPULAR-CURTAINS" ,
@@ -97,26 +99,26 @@ class TestChangeBatchQuantity:
9799 @staticmethod
98100 def test_changes_available_quantity ():
99101 uow = FakeUnitOfWork ()
100- messagebus .handle (events . BatchCreated ("batch1" , "ADORABLE-SETTEE" , 100 , None ), uow )
102+ messagebus .handle (commands . CreateBatch ("batch1" , "ADORABLE-SETTEE" , 100 , None ), uow )
101103 [batch ] = uow .products .get (sku = "ADORABLE-SETTEE" ).batches
102104 assert batch .available_quantity == 100
103105
104- messagebus .handle (events . BatchQuantityChanged ("batch1" , 50 ), uow )
106+ messagebus .handle (commands . ChangeBatchQuantity ("batch1" , 50 ), uow )
105107
106108 assert batch .available_quantity == 50
107109
108110
109111 @staticmethod
110112 def test_reallocates_if_necessary ():
111113 uow = FakeUnitOfWork ()
112- messagebus .handle (events . BatchCreated ("batch1" , "INDIFFERENT-TABLE" , 50 , None ), uow )
113- messagebus .handle (events . BatchCreated ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()), uow )
114- messagebus .handle (events . AllocationRequired ("order1" , "INDIFFERENT-TABLE" , 20 ), uow )
115- messagebus .handle (events . AllocationRequired ("order2" , "INDIFFERENT-TABLE" , 20 ), uow )
114+ messagebus .handle (commands . CreateBatch ("batch1" , "INDIFFERENT-TABLE" , 50 , None ), uow )
115+ messagebus .handle (commands . CreateBatch ("batch2" , "INDIFFERENT-TABLE" , 50 , date .today ()), uow )
116+ messagebus .handle (commands . Allocate ("order1" , "INDIFFERENT-TABLE" , 20 ), uow )
117+ messagebus .handle (commands . Allocate ("order2" , "INDIFFERENT-TABLE" , 20 ), uow )
116118 [batch1 , batch2 ] = uow .products .get (sku = "INDIFFERENT-TABLE" ).batches
117119 assert batch1 .available_quantity == 10
118120
119- messagebus .handle (events . BatchQuantityChanged ("batch1" , 25 ), uow )
121+ messagebus .handle (commands . ChangeBatchQuantity ("batch1" , 25 ), uow )
120122
121123 # order1 or order2 will be deallocated, so we"ll have 25 - 20 * 1
122124 assert batch1 .available_quantity == 5
0 commit comments