11import pytest
2- from allocation .domain import model
32from allocation .adapters import repository
4- from allocation .service_layer import services
3+ from allocation .service_layer import services , unit_of_work
54
65
76class FakeRepository (repository .AbstractRepository ):
@@ -19,38 +18,44 @@ def list(self):
1918 return list (self ._batches )
2019
2120
22- class FakeSession ():
23- committed = False
21+ class FakeUnitOfWork (unit_of_work .AbstractUnitOfWork ):
22+
23+ def __init__ (self ):
24+ self .batches = FakeRepository ([])
25+ self .committed = False
2426
2527 def commit (self ):
2628 self .committed = True
2729
30+ def rollback (self ):
31+ pass
32+
33+
2834
2935def test_add_batch ():
30- repo , session = FakeRepository ([]), FakeSession ()
31- services .add_batch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None , repo , session )
32- assert repo .get ("b1" ) is not None
33- assert session .committed
36+ uow = FakeUnitOfWork ()
37+ services .add_batch ("b1" , "CRUNCHY-ARMCHAIR" , 100 , None , uow )
38+ assert uow . batches .get ("b1" ) is not None
39+ assert uow .committed
3440
3541
3642def test_allocate_returns_allocation ():
37- repo , session = FakeRepository ([]), FakeSession ()
38- services .add_batch ("batch1" , "COMPLICATED-LAMP" , 100 , None , repo , session )
39- result = services .allocate ("o1" , "COMPLICATED-LAMP" , 10 , repo , session )
43+ uow = FakeUnitOfWork ()
44+ services .add_batch ("batch1" , "COMPLICATED-LAMP" , 100 , None , uow )
45+ result = services .allocate ("o1" , "COMPLICATED-LAMP" , 10 , uow )
4046 assert result == "batch1"
4147
4248
4349def test_allocate_errors_for_invalid_sku ():
44- repo , session = FakeRepository ([]), FakeSession ()
45- services .add_batch ("b1" , "AREALSKU" , 100 , None , repo , session )
50+ uow = FakeUnitOfWork ()
51+ services .add_batch ("b1" , "AREALSKU" , 100 , None , uow )
4652
4753 with pytest .raises (services .InvalidSku , match = "Invalid sku NONEXISTENTSKU" ):
48- services .allocate ("o1" , "NONEXISTENTSKU" , 10 , repo , FakeSession () )
54+ services .allocate ("o1" , "NONEXISTENTSKU" , 10 , uow )
4955
5056
51- def test_commits ():
52- repo , session = FakeRepository ([]), FakeSession ()
53- session = FakeSession ()
54- services .add_batch ("b1" , "OMINOUS-MIRROR" , 100 , None , repo , session )
55- services .allocate ("o1" , "OMINOUS-MIRROR" , 10 , repo , session )
56- assert session .committed is True
57+ def test_allocate_commits ():
58+ uow = FakeUnitOfWork ()
59+ services .add_batch ("b1" , "OMINOUS-MIRROR" , 100 , None , uow )
60+ services .allocate ("o1" , "OMINOUS-MIRROR" , 10 , uow )
61+ assert uow .committed
0 commit comments