Skip to content

Commit 651f813

Browse files
committed
implement .seen on repository [repository_tracks_seen]
1 parent b037c86 commit 651f813

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/allocation/repository.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,41 @@
1+
from typing import Set
12
import abc
23
from allocation import model
34

45
class AbstractRepository(abc.ABC):
56

6-
@abc.abstractmethod
7+
def __init__(self):
8+
self.seen = set() # type: Set[model.Product]
9+
710
def add(self, product):
11+
self._add(product)
12+
self.seen.add(product)
13+
14+
def get(self, sku):
15+
p = self._get(sku)
16+
if p:
17+
self.seen.add(p)
18+
return p
19+
20+
@abc.abstractmethod
21+
def _add(self, product):
822
raise NotImplementedError
923

1024
@abc.abstractmethod
11-
def get(self, sku):
25+
def _get(self, sku):
1226
raise NotImplementedError
1327

1428

1529

1630
class SqlAlchemyRepository(AbstractRepository):
1731

1832
def __init__(self, session):
33+
super().__init__()
1934
self.session = session
20-
self.seen = set()
2135

22-
def add(self, product):
23-
self.seen.add(product)
36+
def _add(self, product):
2437
self.session.add(product)
2538

26-
def get(self, sku):
27-
p = self.session.query(model.Product).filter_by(sku=sku).first()
28-
if p:
29-
self.seen.add(p)
30-
return p
39+
def _get(self, sku):
40+
return self.session.query(model.Product).filter_by(sku=sku).first()
3141

tests/unit/test_services.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
class FakeRepository(repository.AbstractRepository):
77

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)