Skip to content

Commit 75b2751

Browse files
committed
get_by_batchref in abstract+real repo, and a new integration test for it [get_by_batchref]
1 parent 43d7961 commit 75b2751

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/allocation/adapters/repository.py

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

56

@@ -17,6 +18,12 @@ def get(self, sku) -> model.Product:
1718
self.seen.add(product)
1819
return product
1920

21+
def get_by_batchref(self, batchref) -> model.Product:
22+
product = self._get_by_batchref(batchref)
23+
if product:
24+
self.seen.add(product)
25+
return product
26+
2027
@abc.abstractmethod
2128
def _add(self, product: model.Product):
2229
raise NotImplementedError
@@ -25,6 +32,10 @@ def _add(self, product: model.Product):
2532
def _get(self, sku) -> model.Product:
2633
raise NotImplementedError
2734

35+
@abc.abstractmethod
36+
def _get_by_batchref(self, batchref) -> model.Product:
37+
raise NotImplementedError
38+
2839

2940
class SqlAlchemyRepository(AbstractRepository):
3041
def __init__(self, session):
@@ -36,3 +47,11 @@ def _add(self, product):
3647

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

0 commit comments

Comments
 (0)