def test_duplicate_id_raises_error(self, class_entity_repo): session = class_entity_repo.session_factory() ent_id = new_entity_id() ent1 = MyEntity(id=ent_id) session.add(MyEntity, ent1) ent2 = MyEntity(id=ent_id) with pytest.raises(ValueError): session.add(MyEntity, ent2)
def _initialize_cache(self, ent_cls): cache = self.__cache_map[ent_cls] = EntityCache() # If we did not receive a cache loader at initialization, we use the # one the repository provides as a default. loader = \ self.__loader or self.__repository.configuration['cache_loader'] if not loader is None: for ent in loader(ent_cls): if ent.id is None: ent.id = new_entity_id() cache.add(ent) return cache
def __load_entities(self, entity_class, is_top_level): # Check if we have an entity loader configured. loader = self.configuration['cache_loader'] if not loader is None: cache = self.__cache_map[entity_class] for ent in loader(entity_class): if ent.id is None: ent.id = new_entity_id() cache.add(ent) # To fully initialize the cache, we also need to load collections # that are not linked to from any of the entities just loaded. if is_top_level: for reg_rc in self.registered_resources: reg_ent_cls = get_entity_class(reg_rc) if not reg_ent_cls in self.__cache_map: self.__load_entities(reg_ent_cls, False)
def add(self, entity_class, entity): """ Adds the given entity of the given entity class to the session. At the point an entity is added, it must not have an ID or a slug of another entity that is already in the session. However, both the ID and the slug may be ``None`` values. """ cache = self.__cache_mgr[entity_class] if not entity.id is None and cache.has_id(entity.id): raise ValueError('Duplicate entity ID "%s".' % entity.id) if not entity.slug is None and cache.has_slug(entity.slug): raise ValueError('Duplicate entity slug "%s".' % entity.slug) if self.__need_datamanager_setup: self.__setup_datamanager() if entity.id is None: entity.id = new_entity_id() self.__unit_of_work.register_new(entity_class, entity) cache.add(entity)
def __persist(self, state): source_entity = state.entity cache = self.__get_cache(type(source_entity)) status = state.status if status == ENTITY_STATUS.NEW: # Autogenerate new ID. if source_entity.id is None: source_entity.id = new_entity_id() cache.add(source_entity) else: target_entity = cache.get_by_id(source_entity.id) if target_entity is None: raise ValueError('Could not persist data - target entity not ' 'found (ID used for lookup: %s).' % source_entity.id) if status == ENTITY_STATUS.DELETED: cache.remove(target_entity) elif status == ENTITY_STATUS.DIRTY: cache.update(state.data, target_entity)
def test_duplicate_id_raises_error(self): ent_id = new_entity_id() ent1 = _MyEntity(id=ent_id) self._session.add(_MyEntity, ent1) ent2 = _MyEntity(id=ent_id) self.assert_raises(ValueError, self._session.add, _MyEntity, ent2)