Skip to content

Commit 3be8292

Browse files
committed
get_by_batchref in abstract+real repo, and a new integration test for it [get_by_batchref]
1 parent c0938ac commit 3be8292

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

src/allocation/repository.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from typing import Set
22
import abc
3-
from allocation import model
3+
from allocation import model, orm
4+
45

56
class AbstractRepository(abc.ABC):
67

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

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

35+
@abc.abstractmethod
36+
def _get_by_batchref(self, batchref):
37+
raise NotImplementedError
38+
39+
2840

2941

3042
class SqlAlchemyRepository(AbstractRepository):
@@ -39,3 +51,8 @@ def _add(self, product):
3951
def _get(self, sku):
4052
return self.session.query(model.Product).filter_by(sku=sku).first()
4153

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

0 commit comments

Comments
 (0)