__init__.py 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import transaction
  2. class Fixture(object):
  3. """ Fixture classes (list) required for this fixtures"""
  4. require = NotImplemented
  5. def __init__(self, session, config):
  6. self._session = session
  7. self._config = config
  8. def insert(self):
  9. raise NotImplementedError()
  10. class FixturesLoader(object):
  11. """
  12. Fixtures loader. Load each fixture once.
  13. """
  14. def __init__(self, session, config, loaded=None):
  15. loaded = [] if loaded is None else loaded
  16. self._loaded = loaded
  17. self._session = session
  18. self._config = config
  19. def loads(self, fixtures_classes):
  20. for fixture_class in fixtures_classes:
  21. for required_fixture_class in fixture_class.require:
  22. self._load(required_fixture_class)
  23. self._load(fixture_class)
  24. def _load(self, fixture_class: Fixture):
  25. if fixture_class not in self._loaded:
  26. fixture = fixture_class(
  27. session=self._session,
  28. config=self._config,
  29. )
  30. fixture.insert()
  31. self._loaded.append(fixture_class)
  32. self._session.flush()
  33. transaction.commit()