-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_services.py
More file actions
68 lines (47 loc) · 1.9 KB
/
test_services.py
File metadata and controls
68 lines (47 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import pytest
from allocation.adapters import repository
from allocation.service_layer import services, unit_of_work
class FakeRepository(repository.AbstractRepository):
def __init__(self, batches):
self._batches = set(batches)
self.seen = set()
def add(self, batch):
self._batches.add(batch)
self.seen.add(batch)
def get(self, sku):
return next((b for b in self._batches if b.sku == sku), None)
def list(self):
return list(self._batches)
class FakeUnitOfWork(unit_of_work.AbstractUnitOfWork):
def __init__(self):
self.products = FakeRepository([])
self.committed = False
def _commit(self):
self.committed = True
def rollback(self):
pass
def test_add_batch_for_new_product():
uow = FakeUnitOfWork()
services.add_batch("b1", "CRUNCHY-ARMCHAIR", 100, None, uow)
assert uow.products.get("CRUNCHY-ARMCHAIR") is not None
assert uow.committed
def test_add_batch_for_existing_product():
uow = FakeUnitOfWork()
services.add_batch("b1", "GARISH-RUG", 100, None, uow)
services.add_batch("b2", "GARISH-RUG", 99, None, uow)
assert "b2" in [b.reference for b in uow.products.get("GARISH-RUG").batches]
def test_allocate_returns_allocation():
uow = FakeUnitOfWork()
services.add_batch("batch1", "COMPLICATED-LAMP", 100, None, uow)
result = services.allocate("o1", "COMPLICATED-LAMP", 10, uow)
assert result == "batch1"
def test_allocate_errors_for_invalid_sku():
uow = FakeUnitOfWork()
services.add_batch("b1", "AREALSKU", 100, None, uow)
with pytest.raises(services.InvalidSku, match="Invalid sku NONEXISTENTSKU"):
services.allocate("o1", "NONEXISTENTSKU", 10, uow)
def test_allocate_commits():
uow = FakeUnitOfWork()
services.add_batch("b1", "OMINOUS-MIRROR", 100, None, uow)
services.allocate("o1", "OMINOUS-MIRROR", 10, uow)
assert uow.committed