def test_custom_repository(self): class MyMemoryAggregate(Aggregate): pass reg = self._registry config = Configurator(registry=reg) config.add_memory_repository('test', aggregate_class=MyMemoryAggregate) repo_mgr = config.get_registered_utility(IRepositoryManager) repo = repo_mgr.get('test') config.add_resource(IFoo, FooMember, FooEntity, collection_root_name="foos", repository='test') self.assert_raises(RuntimeError, repo.get_collection, IFoo) self.assert_raises(RuntimeError, repo.get_aggregate, IFoo) repo.initialize() coll = repo.get_collection(IFoo) agg = coll.get_aggregate() self.assert_true(isinstance(agg, MyMemoryAggregate)) entity = FooEntity(id=1) agg.add(entity) self.assert_true(agg.count() == 1) self.assert_equal(list(agg.iterator())[0].id, entity.id) self.assert_equal(agg.get_by_id(1).id, entity.id) self.assert_equal(agg.get_by_slug('1').slug, entity.slug) agg.remove(entity) self.assert_true(agg.count() == 0)
def test_custom_repository(self): class MyMemoryAggregate(Aggregate): pass reg = self._registry config = Configurator(registry=reg) config.add_memory_repository('test', aggregate_class=MyMemoryAggregate) repo_mgr = config.get_registered_utility(IRepositoryManager) repo = repo_mgr.get('test') config.add_resource(IFoo, FooMember, FooEntity, collection_root_name="foos", repository='test') self.assert_raises(RuntimeError, repo.get_collection, IFoo) self.assert_raises(RuntimeError, repo.get_aggregate, IFoo) repo.initialize() coll = repo.get_collection(IFoo) agg = coll.get_aggregate() self.assert_true(isinstance(agg, MyMemoryAggregate)) entity = FooEntity(id=1) agg.add(entity) self.assert_true(agg.count() == 1) self.assert_equal(list(agg.iterator())[0].id, entity.id) self.assert_equal(agg.get_by_id(1).id, entity.id) self.assert_equal(agg.get_by_slug('1').slug, entity.slug) agg.remove(entity) self.assert_true(agg.count() == 0)
def test_custom_repository(self, simple_config): class MyMemoryAggregate(Aggregate): pass reg = simple_config.registry config = Configurator(registry=reg) config.add_memory_repository('test', aggregate_class=MyMemoryAggregate) repo_mgr = config.get_registered_utility(IRepositoryManager) repo = repo_mgr.get('test') config.add_resource(IFoo, FooMember, FooEntity, collection_root_name="foos", repository='test') with pytest.raises(RuntimeError): repo.get_collection(IFoo) with pytest.raises(RuntimeError): repo.get_aggregate(IFoo) repo.initialize() coll = repo.get_collection(IFoo) agg = coll.get_aggregate() assert isinstance(agg, MyMemoryAggregate) entity = FooEntity(id=1) agg.add(entity) assert agg.count() == 1 assert list(agg.iterator())[0].id == entity.id assert agg.get_by_id(1).id == entity.id assert agg.get_by_slug('1').slug == entity.slug agg.remove(entity) assert agg.count() == 0
def test_custom_repository(self, simple_config): class MyMemoryAggregate(Aggregate): pass reg = simple_config.registry config = Configurator(registry=reg) config.add_memory_repository('test', aggregate_class=MyMemoryAggregate) repo_mgr = config.get_registered_utility(IRepositoryManager) repo = repo_mgr.get('test') config.add_resource(IFoo, FooMember, FooEntity, collection_root_name="foos", repository='test') with pytest.raises(RuntimeError): repo.get_collection(IFoo) with pytest.raises(RuntimeError): repo.get_aggregate(IFoo) repo.initialize() coll = repo.get_collection(IFoo) agg = coll.get_aggregate() assert isinstance(agg, MyMemoryAggregate) entity = FooEntity(id=1) agg.add(entity) assert agg.count() == 1 assert list(agg.iterator())[0].id == entity.id assert agg.get_by_id(1).id == entity.id assert agg.get_by_slug('1').slug == entity.slug agg.remove(entity) assert agg.count() == 0