def test_init_ok(): event = Event(**{'aggregate_id': True, 'var1': 1, 'var2': '2'}) assert event.aggregate_id assert event.sequence == 0 assert not event._is_dirty assert event.var1 == 1 assert event.var2 == '2'
def create_snapshot(self): fields = {} # iterate fields for field_name, field in self._schema.items(): # get previous value fields[field_name] = getattr(self, field_name) # get latest sequence fields['sequence'] = self._sequence_offset + len(self._events) # create snapshot event snapshot = Event(**fields) # persist it self.Meta.backend.save_snapshot(snapshot, model=self.Meta.snapshot_model)
def test_to_object(): event_dict = { 'aggregate_id': True, 'sequence': 0, 'field1': 1, 'field2': '2' } event = Event(**event_dict) with pytest.raises(ValueError): _ = SQLAlchemyBackend.to_object(event) event_model = SQLAlchemyBackend.to_object(event, model=TestEvent) assert isinstance(event_model, TestEvent) for k in event_dict.keys(): assert hasattr(event_model, k) assert getattr(event_model, k) == event_dict[k]
def test_clean(): event_dict = {'aggregate_id': True, 'sequence': 0, 'var1': 1, '_var2': 2} event = Event(**event_dict) assert event._clean() == {'aggregate_id': True, 'sequence': 0, 'var1': 1}
def test_init_error(): with pytest.raises(ValueError): event = Event(**{'var1': 1, 'var2': '2'})
def to_object(event: Event, **kwargs) -> object: model = kwargs.get('model') if not model or not isinstance(model, DeclarativeMeta): raise ValueError('No SQLAlchemy model provided') return model(**event._clean())
def to_event(obj: object, **kwargs) -> Event: return Event(**{k: v for k, v in obj.__dict__.items() if not k.startswith('_')})