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
1
2
import abc
2
3
from allocation import model
3
4
4
5
class AbstractRepository (abc .ABC ):
5
6
6
- @abc .abstractmethod
7
+ def __init__ (self ):
8
+ self .seen = set () # type: Set[model.Product]
9
+
7
10
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 ):
8
22
raise NotImplementedError
9
23
10
24
@abc .abstractmethod
11
- def get (self , sku ):
25
+ def _get (self , sku ):
12
26
raise NotImplementedError
13
27
14
28
15
29
16
30
class SqlAlchemyRepository (AbstractRepository ):
17
31
18
32
def __init__ (self , session ):
33
+ super ().__init__ ()
19
34
self .session = session
20
- self .seen = set ()
21
35
22
- def add (self , product ):
23
- self .seen .add (product )
36
+ def _add (self , product ):
24
37
self .session .add (product )
25
38
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 ()
31
41
Original file line number Diff line number Diff line change 6
6
class FakeRepository (repository .AbstractRepository ):
7
7
8
8
def __init__ (self , products ):
9
+ super ().__init__ ()
9
10
self ._products = set (products )
10
11
11
- def add (self , product ):
12
+ def _add (self , product ):
12
13
self ._products .add (product )
13
14
14
- def get (self , sku ):
15
+ def _get (self , sku ):
15
16
return next ((p for p in self ._products if p .sku == sku ), None )
16
17
17
18
You can’t perform that action at this time.
0 commit comments