def test_listen_sqlalchemy(self): def method(): pass mm = ModelMapper(Model.System.Model, 'before_insert') mm.listen(method) assert method.is_an_sqlalchemy_event_listener assert method.sqlalchemy_listener is mm
def test_listen_sqlalchemy(self): def method(): pass mm = ModelMapper(Model.System.Model, 'before_insert') mm.listen(method) self.assertTrue(method.is_an_sqlalchemy_event_listener) self.assertEqual(method.sqlalchemy_listener, mm)
def test_listen_anyblok(self): def method(): pass mm = ModelMapper(Model.System.Model, 'event') mm.listen(method) assert method.is_an_event_listener assert method.model == 'Model.System.Model' assert method.event == 'event'
def test_listen_anyblok(self): def method(): pass mm = ModelMapper(Model.System.Model, 'event') mm.listen(method) self.assertTrue(method.is_an_event_listener) self.assertEqual(method.model, 'Model.System.Model') self.assertEqual(method.event, 'event')
def after_model_construction(self, base, namespace, transformation_properties): """Add the sqlalchemy event :param base: the Model class :param namespace: the namespace of the model :param transformation_properties: the properties of the model """ namespaces = [namespace] namespaces.extend(list(base.__depends__)) fields = [] for ns in namespaces: for c in self.registry.get(ns).loaded_columns: f = self.registry.loaded_namespaces_first_step[namespace].get( c) if isinstance(f, DateTime) and f.auto_update: # TimeStamp inherit of DateTime so it works too fields.append(c) if fields: e = ModelMapper(namespace, 'after_update') def auto_update_listen(mapper, connection, target): now = datetime.now() for field in fields: setattr(target, field, now) self.registry._sqlalchemy_known_events.append( (e, namespace, auto_update_listen))
def after_model_construction(self, base, namespace, transformation_properties): for eventtype in ('before_insert', 'after_insert', 'before_update', 'after_update', 'before_delete', 'after_delete'): attr = eventtype + '_orm_event' if hasattr(base, attr): if not hasattr(getattr(base, attr), '__self__'): raise ORMEventException("On %s %s is not a classmethod" % (base, attr)) self.registry._sqlalchemy_known_events.append( (ModelMapper(base, eventtype), namespace, ModelAttribute(namespace, attr)))
def after_model_construction(self, base, namespace, transformation_properties): """Add the sqlalchemy event :param base: the Model class :param namespace: the namespace of the model :param transformation_properties: the properties of the model """ if transformation_properties['auto_update_field_datetime']: e = ModelMapper(namespace, 'after_update') fields = transformation_properties['auto_update_field_datetime'] def auto_update_listen(mapper, connection, target): now = datetime.now() for field in fields: setattr(target, field, now) self.registry._sqlalchemy_known_events.append( (e, namespace, auto_update_listen))
def test_get_mapper(self): registry = self.init_registry(None) mm = ModelMapper(Model.System.Model, 'even') assert mm.mapper(registry, None) is registry.System.Model
def test_model_repr(self): assert ModelMapper(ModelRepr('Model.System.Model'), None)
def test_capable_by_declaration(self): self.assertTrue(ModelMapper.capable(Model.System.Model))
def test_not_capable(self): self.assertFalse(ModelMapper.capable(None))
def test_by_declaration(self): mm = ModelMapper(Model.System.Model, 'even') assert isinstance(mm.model, ModelRepr) assert mm.model.model_name, 'Model.System.Model'
def test_by_registry_name(self): mm = ModelMapper('Model.System.Model', 'event') assert isinstance(mm.model, ModelRepr) assert mm.model.model_name, 'Model.System.Model'
def test_capable_by_declaration(self): assert ModelMapper.capable(Model.System.Model)
def test_capable_by_registry_name(self): assert ModelMapper.capable('Model.System.Model')
def test_not_capable(self): assert not (ModelMapper.capable(None))
def test_capable_by_registry_name(self): self.assertTrue(ModelMapper.capable('Model.System.Model'))
def test_capable_by_model_repr(self): assert ModelMapper.capable(ModelRepr('Model.System.Model'))
def test_mapper_self(self, registry_blok): mm = ModelMapper('SELF', None) assert mm.mapper(registry_blok, 'Model.System.Blok') is registry_blok.System.Blok
def test_get_mapper(self): registry = self.init_registry(None) mm = ModelMapper(Model.System.Model, 'even') self.assertIs(mm.mapper(registry, None), registry.System.Model)