Total Complexity | 9 |
Total Lines | 44 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | import transaction |
||
|
|||
2 | |||
3 | |||
4 | class Fixture(object): |
||
5 | |||
6 | """ Fixture classes (list) required for this fixtures""" |
||
7 | require = NotImplemented |
||
8 | |||
9 | def __init__(self, session, config): |
||
10 | self._session = session |
||
11 | self._config = config |
||
12 | |||
13 | def insert(self): |
||
14 | raise NotImplementedError() |
||
15 | |||
16 | |||
17 | class FixturesLoader(object): |
||
18 | """ |
||
19 | Fixtures loader. Load each fixture once. |
||
20 | """ |
||
21 | |||
22 | def __init__(self, session, config, loaded=None): |
||
23 | loaded = [] if loaded is None else loaded |
||
24 | self._loaded = loaded |
||
25 | self._session = session |
||
26 | self._config = config |
||
27 | |||
28 | def loads(self, fixtures_classes): |
||
29 | for fixture_class in fixtures_classes: |
||
30 | for required_fixture_class in fixture_class.require: |
||
31 | self._load(required_fixture_class) |
||
32 | self._load(fixture_class) |
||
33 | |||
34 | def _load(self, fixture_class: Fixture): |
||
35 | if fixture_class not in self._loaded: |
||
36 | fixture = fixture_class( |
||
37 | session=self._session, |
||
38 | config=self._config, |
||
39 | ) |
||
40 | fixture.insert() |
||
41 | self._loaded.append(fixture_class) |
||
42 | self._session.flush() |
||
43 | transaction.commit() |
||
44 |
The coding style of this project requires that you add a docstring to this code element. Below, you find an example for methods:
If you would like to know more about docstrings, we recommend to read PEP-257: Docstring Conventions.