11import pytest
2- from allocation import repository , services
2+ from allocation import repository , services , unit_of_work
33
44
55class FakeRepository (repository .AbstractRepository ):
@@ -17,38 +17,44 @@ def list(self):
1717 return list (self ._batches )
1818
1919
20- class FakeSession ():
21- committed = False
20+ class FakeUnitOfWork (unit_of_work .AbstractUnitOfWork ):
21+
22+ def __init__ (self ):
23+ self .batches = FakeRepository ([])
24+ self .committed = False
2225
2326 def commit (self ):
2427 self .committed = True
2528
29+ def rollback (self ):
30+ pass
31+
32+
2633
2734def 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
35+ uow = FakeUnitOfWork ()
36+ services .add_batch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None , uow )
37+ assert uow . batches .get ("b1" ) is not None
38+ assert uow .committed
3239
3340
3441def 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 )
42+ uow = FakeUnitOfWork ()
43+ services .add_batch ("batch1" , "COMPLICATED-LAMP" , 100 , None , uow )
44+ result = services .allocate ("o1" , "COMPLICATED-LAMP" , 10 , uow )
3845 assert result == "batch1"
3946
4047
4148def test_allocate_errors_for_invalid_sku ():
42- repo , session = FakeRepository ([]), FakeSession ()
43- services .add_batch ("b1" , "AREALSKU" , 100 , None , repo , session )
49+ uow = FakeUnitOfWork ()
50+ services .add_batch ("b1" , "AREALSKU" , 100 , None , uow )
4451
4552 with pytest .raises (services .InvalidSku , match = "Invalid sku NONEXISTENTSKU" ):
46- services .allocate ("o1" , "NONEXISTENTSKU" , 10 , repo , FakeSession () )
53+ services .allocate ("o1" , "NONEXISTENTSKU" , 10 , uow )
4754
4855
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
56+ def test_allocate_commits ():
57+ uow = FakeUnitOfWork ()
58+ services .add_batch ("b1" , "OMINOUS-MIRROR" , 100 , None , uow )
59+ services .allocate ("o1" , "OMINOUS-MIRROR" , 10 , uow )
60+ assert uow .committed
0 commit comments