File tree Expand file tree Collapse file tree 2 files changed +23
-12
lines changed
Expand file tree Collapse file tree 2 files changed +23
-12
lines changed Original file line number Diff line number Diff line change 1+ from typing import Set
12import abc
23from allocation import model
34
45class 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
1630class 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
Original file line number Diff line number Diff line change 66class 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
You can’t perform that action at this time.
0 commit comments