def mark_dirty(self, entity): """ Marks the given entity for the given class as DIRTY. :raises ValueError: If the given entity does not hold state. """ EntityState.get_state(entity).status = ENTITY_STATUS.DIRTY
def mark_deleted(self, entity): """ Marks the given entity as DELETED. :raises ValueError: If the given entity does not hold state. """ EntityState.get_state(entity).status = ENTITY_STATUS.DELETED
def mark_dirty(self, entity): """ Marks the given entity for the given class as DIRTY. :raises ValueError: If the given entity does not hold state. """ EntityState.get_state(entity).status = ENTITY_STATUS.DIRTY
def mark_deleted(self, entity): """ Marks the given entity as DELETED. :raises ValueError: If the given entity does not hold state. """ EntityState.get_state(entity).status = ENTITY_STATUS.DELETED
def mark_new(self, entity): """ Marks the given entity as NEW. This is done when an entity is re-associated with a session after having been removed before. """ EntityState.get_state(entity).status = ENTITY_STATUS.NEW
def mark_pending(self, entity): """ Sets the flag indicating if the state of the given entity has been persisted to `False`. :note: The persistency flag is orthogonal to the status flag. """ EntityState.get_state(entity).is_persisted = False
def mark_new(self, entity): """ Marks the given entity as NEW. This is done when an entity is re-associated with a session after having been removed before. """ EntityState.get_state(entity).status = ENTITY_STATUS.NEW
def mark_pending(self, entity): """ Sets the flag indicating if the state of the given entity has been persisted to `False`. :note: The persistency flag is orthogonal to the status flag. """ EntityState.get_state(entity).is_persisted = False
def register_clean(self, entity_class, entity): """ Registers the given entity for the given class as CLEAN. :returns: Cloned entity. """ EntityState.manage(entity, self) EntityState.get_state(entity).status = ENTITY_STATUS.CLEAN self.__entity_set_map[entity_class].add(entity)
def is_marked_pending(self, entity): """ Checks if the flag indicating that the state for the given entity has been persisted is `False`. :note: Use only after checking if the given entity is registered. :raises ValueError: If the entity has no state information. """ return not EntityState.get_state(entity).is_persisted
def __object_iterator(self, status, ent_cls): if ent_cls is None: ent_clss = self.__entity_set_map.keys() else: ent_clss = [ent_cls] for ent_cls in ent_clss: for ent in self.__entity_set_map[ent_cls]: if EntityState.get_state(ent).status == status: yield ent
def register_clean(self, entity_class, entity): """ Registers the given entity for the given class as CLEAN. :returns: Cloned entity. """ EntityState.manage(entity, self) EntityState.get_state(entity).status = ENTITY_STATUS.CLEAN self.__entity_set_map[entity_class].add(entity)
def iterator(self): """ Returns an iterator over all entity states held by this Unit Of Work. """ # FIXME: There is no dependency tracking; objects are iterated in # random order. for ent_cls in list(self.__entity_set_map.keys()): for ent in self.__entity_set_map[ent_cls]: yield EntityState.get_state(ent)
def is_marked_pending(self, entity): """ Checks if the flag indicating that the state for the given entity has been persisted is `False`. :note: Use only after checking if the given entity is registered. :raises ValueError: If the entity has no state information. """ return not EntityState.get_state(entity).is_persisted
def __object_iterator(self, status, ent_cls): if ent_cls is None: ent_clss = self.__entity_set_map.keys() else: ent_clss = [ent_cls] for ent_cls in ent_clss: for ent in self.__entity_set_map[ent_cls]: if EntityState.get_state(ent).status == status: yield ent
def iterator(self): """ Returns an iterator over all entity states held by this Unit Of Work. """ # FIXME: There is no dependency tracking; objects are iterated in # random order. for ent_cls in list(self.__entity_set_map.keys()): for ent in self.__entity_set_map[ent_cls]: yield EntityState.get_state(ent)
def mark_clean(self, entity): """ Marks the given entity as CLEAN. This is done when an entity is loaded fresh from the repository or after a commit. """ state = EntityState.get_state(entity) state.status = ENTITY_STATUS.CLEAN state.is_persisted = True
def test_state_data(self): data = dict(text='FOO', number=-1, parent=MyEntityParent(), children=[MyEntityChild()]) entity = MyEntity(**data) # We don't want to test the required unit of work here. uow = MagicMock() self.assert_raises(ValueError, EntityState.get_state, entity) entity.__everest__ = EntityState(entity, uow) state_data = EntityState.get_state(entity).data for attr, value in state_data.items(): if attr.resource_attr == 'number': number_attr = attr elif attr.resource_attr == 'parent': parent_attr = attr elif attr.resource_attr == 'parent_text': parent_text_attr = attr if attr.resource_attr in data: self.assert_equal(value, data[attr.resource_attr]) new_number = -2 state_data[number_attr] = new_number EntityState.get_state(entity).data = state_data self.assert_equal(entity.number, new_number) new_entity = MyEntity() self.assert_not_equal(new_entity.number, new_number) new_entity.__everest__ = EntityState(new_entity, uow) EntityState.transfer_state_data(entity, new_entity) self.assert_equal(new_entity.number, new_number) # Make setting invalid attribute fail. invalid_number_attr = terminal_attribute(str, 'grmbl') del state_data[number_attr] state_data[invalid_number_attr] = -2 with self.assert_raises(ValueError) as cm: EntityState.get_state(entity).data = state_data self.assert_true(cm.exception.args[0].startswith('Can not set')) # Make set nested attribute fail. entity.parent = None del state_data[invalid_number_attr] del state_data[parent_attr] state_data[parent_text_attr] = 'FOO PARENT' state = EntityState.get_state(entity) self.assert_raises(AttributeError, setattr, state, 'data', state_data)
def is_marked_new(self, entity): """ Checks if the given entity is marked with status NEW. Returns `False` if the entity has no state information. """ try: result = EntityState.get_state(entity).status == ENTITY_STATUS.NEW except ValueError: result = False return result
def mark_clean(self, entity): """ Marks the given entity as CLEAN. This is done when an entity is loaded fresh from the repository or after a commit. """ state = EntityState.get_state(entity) state.status = ENTITY_STATUS.CLEAN state.is_persisted = True
def register_deleted(self, entity_class, entity): """ Registers the given entity for the given class as DELETED. :raises ValueError: If the given entity already holds state that was created by another Unit Of Work. """ EntityState.manage(entity, self) EntityState.get_state(entity).status = ENTITY_STATUS.DELETED self.__entity_set_map[entity_class].add(entity)
def is_marked_new(self, entity): """ Checks if the given entity is marked with status NEW. Returns `False` if the entity has no state information. """ try: result = EntityState.get_state(entity).status == ENTITY_STATUS.NEW except ValueError: result = False return result
def test_state_data(self): data = dict(text='FOO', number=-1, parent=MyEntityParent(), children=[MyEntityChild()]) entity = MyEntity(**data) # We don't want to test the required unit of work here. uow = MagicMock() self.assert_raises(ValueError, EntityState.get_state, entity) entity.__everest__ = EntityState(entity, uow) state_data = EntityState.get_state(entity).data for attr, value in state_data.items(): if attr.resource_attr == 'number': number_attr = attr elif attr.resource_attr == 'parent': parent_attr = attr elif attr.resource_attr == 'parent_text': parent_text_attr = attr if attr.resource_attr in data: self.assert_equal(value, data[attr.resource_attr]) new_number = -2 state_data[number_attr] = new_number EntityState.get_state(entity).data = state_data self.assert_equal(entity.number, new_number) new_entity = MyEntity() self.assert_not_equal(new_entity.number, new_number) new_entity.__everest__ = EntityState(new_entity, uow) EntityState.transfer_state_data(entity, new_entity) self.assert_equal(new_entity.number, new_number) # Make setting invalid attribute fail. invalid_number_attr = terminal_attribute(str, 'grmbl') del state_data[number_attr] state_data[invalid_number_attr] = -2 with self.assert_raises(ValueError) as cm: EntityState.get_state(entity).data = state_data self.assert_true(cm.exception.args[0].startswith('Can not set')) # Make set nested attribute fail. entity.parent = None del state_data[invalid_number_attr] del state_data[parent_attr] state_data[parent_text_attr] = 'FOO PARENT' state = EntityState.get_state(entity) self.assert_raises(AttributeError, setattr, state, 'data', state_data)
def register_deleted(self, entity_class, entity): """ Registers the given entity for the given class as DELETED. :raises ValueError: If the given entity already holds state that was created by another Unit Of Work. """ EntityState.manage(entity, self) EntityState.get_state(entity).status = ENTITY_STATUS.DELETED self.__entity_set_map[entity_class].add(entity)
def is_marked_pending(self, entity): """ Checks if the flag indicating that the state for the given entity has been persisted is `False`. Returns `False` if the entity has no state information. """ try: result = not EntityState.get_state(entity).is_persisted except ValueError: result = False return result
def test_basics(self): ent = _MyEntity(id=0) self._uow.register_new(_MyEntity, ent) self.assert_equal(EntityState.get_state(ent).status, ENTITY_STATUS.NEW) self.assert_equal([item.entity for item in self._uow.iterator()], [ent]) self.assert_equal(list(self._uow.get_new(_MyEntity)), [ent]) self._uow.mark_clean(ent) self.assert_equal(list(self._uow.get_clean(_MyEntity)), [ent]) self._uow.mark_dirty(ent) self.assert_equal(list(self._uow.get_dirty(_MyEntity)), [ent]) self._uow.mark_deleted(ent) self.assert_equal(list(self._uow.get_deleted(_MyEntity)), [ent]) self._uow.unregister(_MyEntity, ent) self.assert_equal(list(self._uow.iterator()), []) self._uow.reset()
def test_basics(self): ent = _MyEntity(id=0) self._uow.register_new(_MyEntity, ent) self.assert_equal(EntityState.get_state(ent).status, ENTITY_STATUS.NEW) self.assert_equal([item.entity for item in self._uow.iterator()], [ent]) self.assert_equal(list(self._uow.get_new(_MyEntity)), [ent]) self._uow.mark_clean(ent) self.assert_equal(list(self._uow.get_clean(_MyEntity)), [ent]) self._uow.mark_dirty(ent) self.assert_equal(list(self._uow.get_dirty(_MyEntity)), [ent]) self._uow.mark_deleted(ent) self.assert_equal(list(self._uow.get_deleted(_MyEntity)), [ent]) self._uow.unregister(_MyEntity, ent) self.assert_equal(list(self._uow.iterator()), []) self._uow.reset()
def test_get_state_unregistered_fails(self): ent = _MyEntity() with self.assert_raises(ValueError) as cm: EntityState.get_state(ent) msg = 'Trying to obtain state for un-managed entity' self.assert_true(cm.exception.args[0].startswith(msg))
def test_get_state_unregistered_fails(self): ent = _MyEntity() with self.assert_raises(ValueError) as cm: EntityState.get_state(ent) msg = 'Trying to obtain state for un-managed entity' self.assert_true(cm.exception.args[0].startswith(msg))