Skip to content

Commit a097126

Browse files
committed
get_by_batchref in abstract+real repo, and a new integration test for it [get_by_batchref]
1 parent 2020b26 commit a097126

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎src/allocation/adapters/repository.py‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import abc
22
from typing import Set
3+
from allocation.adapters import orm
34
from allocation.domain import model
45

56

7+
68
class AbstractRepository(abc.ABC):
79

810
def __init__(self):
@@ -18,6 +20,12 @@ def get(self, sku) -> model.Product:
1820
self.seen.add(product)
1921
return product
2022

23+
def get_by_batchref(self, batchref) -> model.Product:
24+
product = self._get_by_batchref(batchref)
25+
if product:
26+
self.seen.add(product)
27+
return product
28+
2129
@abc.abstractmethod
2230
def _add(self, product: model.Product):
2331
raise NotImplementedError
@@ -26,6 +34,11 @@ def _add(self, product: model.Product):
2634
def _get(self, sku) -> model.Product:
2735
raise NotImplementedError
2836

37+
@abc.abstractmethod
38+
def _get_by_batchref(self, batchref) -> model.Product:
39+
raise NotImplementedError
40+
41+
2942

3043

3144
class SqlAlchemyRepository(AbstractRepository):
@@ -39,3 +52,8 @@ def _add(self, product):
3952

4053
def _get(self, sku):
4154
return self.session.query(model.Product).filter_by(sku=sku).first()
55+
56+
def _get_by_batchref(self, batchref):
57+
return self.session.query(model.Product).join(model.Batch).filter(
58+
orm.batches.c.reference == batchref,
59+
).first()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from allocation.adapters import repository
2+
from allocation.domain import model
3+
4+
def test_get_by_batchref(session):
5+
repo = repository.SqlAlchemyRepository(session)
6+
b1 = model.Batch(ref='b1', sku='sku1', qty=100, eta=None)
7+
b2 = model.Batch(ref='b2', sku='sku1', qty=100, eta=None)
8+
b3 = model.Batch(ref='b3', sku='sku2', qty=100, eta=None)
9+
p1 = model.Product(sku='sku1', batches=[b1, b2])
10+
p2 = model.Product(sku='sku2', batches=[b3])
11+
repo.add(p1)
12+
repo.add(p2)
13+
assert repo.get_by_batchref('b2') == p1
14+
assert repo.get_by_batchref('b3') == p2

0 commit comments

Comments
 (0)