Skip to content

Commit 46c62d5

Browse files
committed
implement .seen on repository [repository_tracks_seen]
1 parent e1faf88 commit 46c62d5

2 files changed

Lines changed: 22 additions & 12 deletions

File tree

src/allocation/adapters/repository.py

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,35 @@
44

55

66
class AbstractRepository(abc.ABC):
7-
@abc.abstractmethod
7+
def __init__(self):
8+
self.seen = set() # type: Set[model.Product]
9+
810
def add(self, product: model.Product):
11+
self._add(product)
12+
self.seen.add(product)
13+
14+
def get(self, sku) -> model.Product:
15+
product = self._get(sku)
16+
if product:
17+
self.seen.add(product)
18+
return product
19+
20+
@abc.abstractmethod
21+
def _add(self, product: model.Product):
922
raise NotImplementedError
1023

1124
@abc.abstractmethod
12-
def get(self, sku) -> model.Product:
25+
def _get(self, sku) -> model.Product:
1326
raise NotImplementedError
1427

1528

1629
class SqlAlchemyRepository(AbstractRepository):
1730
def __init__(self, session):
31+
super().__init__()
1832
self.session = session
19-
self.seen = set() # type: Set[model.Product]
2033

21-
def add(self, product):
22-
self.seen.add(product)
34+
def _add(self, product):
2335
self.session.add(product)
2436

25-
def get(self, sku):
26-
product = self.session.query(model.Product).filter_by(sku=sku).first()
27-
if product:
28-
self.seen.add(product)
29-
return product
37+
def _get(self, sku):
38+
return self.session.query(model.Product).filter_by(sku=sku).first()

tests/unit/test_services.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66

77
class FakeRepository(repository.AbstractRepository):
88
def __init__(self, products):
9+
super().__init__()
910
self._products = set(products)
1011

11-
def add(self, product):
12+
def _add(self, product):
1213
self._products.add(product)
1314

14-
def get(self, sku):
15+
def _get(self, sku):
1516
return next((p for p in self._products if p.sku == sku), None)
1617

1718

0 commit comments

Comments
 (0)