11import pytest
22from allocation .adapters import repository
3- from allocation .service_layer import services
3+ from allocation .service_layer import services , unit_of_work
44
55
66class FakeRepository (repository .AbstractRepository ):
@@ -17,38 +17,42 @@ def list(self):
1717 return list (self ._batches )
1818
1919
20- class FakeSession :
21- committed = False
20+ class FakeUnitOfWork (unit_of_work .AbstractUnitOfWork ):
21+ def __init__ (self ):
22+ self .batches = FakeRepository ([])
23+ self .committed = False
2224
2325 def commit (self ):
2426 self .committed = True
2527
28+ def rollback (self ):
29+ pass
30+
2631
2732def test_add_batch ():
28- repo , session = FakeRepository ([]), FakeSession ()
29- services .add_batch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None , repo , session )
30- assert repo .get ("b1" ) is not None
31- assert session .committed
33+ uow = FakeUnitOfWork ()
34+ services .add_batch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None , uow )
35+ assert uow . batches .get ("b1" ) is not None
36+ assert uow .committed
3237
3338
3439def test_allocate_returns_allocation ():
35- repo , session = FakeRepository ([]), FakeSession ()
36- services .add_batch ("batch1" , "COMPLICATED-LAMP" , 100 , None , repo , session )
37- result = services .allocate ("o1" , "COMPLICATED-LAMP" , 10 , repo , session )
40+ uow = FakeUnitOfWork ()
41+ services .add_batch ("batch1" , "COMPLICATED-LAMP" , 100 , None , uow )
42+ result = services .allocate ("o1" , "COMPLICATED-LAMP" , 10 , uow )
3843 assert result == "batch1"
3944
4045
4146def test_allocate_errors_for_invalid_sku ():
42- repo , session = FakeRepository ([]), FakeSession ()
43- services .add_batch ("b1" , "AREALSKU" , 100 , None , repo , session )
47+ uow = FakeUnitOfWork ()
48+ services .add_batch ("b1" , "AREALSKU" , 100 , None , uow )
4449
4550 with pytest .raises (services .InvalidSku , match = "Invalid sku NONEXISTENTSKU" ):
46- services .allocate ("o1" , "NONEXISTENTSKU" , 10 , repo , FakeSession () )
51+ services .allocate ("o1" , "NONEXISTENTSKU" , 10 , uow )
4752
4853
49- def test_commits ():
50- repo , session = FakeRepository ([]), FakeSession ()
51- session = FakeSession ()
52- services .add_batch ("b1" , "OMINOUS-MIRROR" , 100 , None , repo , session )
53- services .allocate ("o1" , "OMINOUS-MIRROR" , 10 , repo , session )
54- assert session .committed is True
54+ def test_allocate_commits ():
55+ uow = FakeUnitOfWork ()
56+ services .add_batch ("b1" , "OMINOUS-MIRROR" , 100 , None , uow )
57+ services .allocate ("o1" , "OMINOUS-MIRROR" , 10 , uow )
58+ assert uow .committed
0 commit comments