def test_mutable_model_base(self): another_model_def = ModelDefinition.objects.create( app_label='mutant', object_name='AnotherModel') another_model_class = another_model_def.model_class() auto_pk_column = another_model_class._meta.pk.get_attname_column()[1] self.assertModelTablesColumnExists(another_model_class, auto_pk_column) with self.assertChecksumChange(): CharFieldDefinition.objects.create(model_def=self.model_def, name='f1', max_length=25) model_class = self.model_def.model_class() with self.assertChecksumChange(another_model_def): base_definition = BaseDefinition(model_def=another_model_def) base_definition.base = model_class base_definition.save() self.assertModelTablesColumnDoesntExists(another_model_class, auto_pk_column) self.assertEqual(model_class.anothermodel.related.related_model, another_model_class) remove_from_app_cache(another_model_class).mark_as_obsolete() self.assertFalse(hasattr(model_class, 'anothermodel')) another_model = another_model_class.objects.create(f1='Martinal') self.assertTrue(hasattr(model_class, 'anothermodel')) self.assertTrue(another_model_class.objects.exists()) with self.assertChecksumChange(): with self.assertChecksumChange(another_model_def): CharFieldDefinition.objects.create(model_def=self.model_def, name='f2', max_length=25, null=True) another_model = another_model_class.objects.get(pk=another_model.pk) self.assertIsNone(another_model.f2) another_model.f2 = 'Placebo' another_model.save()
def test_mutable_model_base(self): another_model_def = ModelDefinition.objects.create( app_label='mutant', object_name='AnotherModel' ) another_model_class = another_model_def.model_class() auto_pk_column = another_model_class._meta.pk.get_attname_column()[1] self.assertModelTablesColumnExists(another_model_class, auto_pk_column) with self.assertChecksumChange(): CharFieldDefinition.objects.create( model_def=self.model_def, name='f1', max_length=25 ) model_class = self.model_def.model_class() with self.assertChecksumChange(another_model_def): base_definition = BaseDefinition(model_def=another_model_def) base_definition.base = model_class base_definition.save() self.assertModelTablesColumnDoesntExists(another_model_class, auto_pk_column) self.assertEqual(model_class.anothermodel.related.model, another_model_class) remove_from_app_cache(another_model_class).mark_as_obsolete() self.assertFalse(hasattr(model_class, 'anothermodel')) another_model = another_model_class.objects.create(f1='Martinal') self.assertTrue(hasattr(model_class, 'anothermodel')) self.assertTrue(another_model_class.objects.exists()) with self.assertChecksumChange(): with self.assertChecksumChange(another_model_def): CharFieldDefinition.objects.create( model_def=self.model_def, name='f2', max_length=25, null=True ) another_model = another_model_class.objects.get(pk=another_model.pk) self.assertIsNone(another_model.f2) another_model.f2 = 'Placebo' another_model.save()
def test_mutable_model_base(self): another_model_def = ModelDefinition.objects.create( app_label='app', object_name='AnotherModel') AnotherModel = another_model_def.model_class() auto_pk_column = AnotherModel._meta.pk.get_attname_column()[1] self.assertModelTablesColumnExists(AnotherModel, auto_pk_column) with self.assertChecksumChange(): CharFieldDefinition.objects.create(model_def=self.model_def, name='f1', max_length=25) with self.assertChecksumChange(another_model_def): base_definition = BaseDefinition(model_def=another_model_def) base_definition.base = self.model_def.model_class() base_definition.save() self.assertModelTablesColumnDoesntExists(AnotherModel, auto_pk_column) another_model = AnotherModel.objects.create(f1='Martinal') self.assertTrue(AnotherModel.objects.exists()) with self.assertChecksumChange(): with self.assertChecksumChange(another_model_def): CharFieldDefinition.objects.create(model_def=self.model_def, name='f2', max_length=25, null=True) another_model = AnotherModel.objects.get(pk=another_model.pk) self.assertIsNone(another_model.f2) another_model.f2 = 'Placebo' another_model.save()
def test_mutable_model_base(self): another_model_def = ModelDefinition.objects.create( app_label='app', object_name='AnotherModel' ) AnotherModel = another_model_def.model_class() auto_pk_column = AnotherModel._meta.pk.get_attname_column()[1] self.assertModelTablesColumnExists(AnotherModel, auto_pk_column) with self.assertChecksumChange(): CharFieldDefinition.objects.create( model_def=self.model_def, name='f1', max_length=25 ) with self.assertChecksumChange(another_model_def): base_definition = BaseDefinition(model_def=another_model_def) base_definition.base = self.model_def.model_class() base_definition.save() self.assertModelTablesColumnDoesntExists(AnotherModel, auto_pk_column) another_model = AnotherModel.objects.create(f1='Martinal') self.assertTrue(AnotherModel.objects.exists()) with self.assertChecksumChange(): with self.assertChecksumChange(another_model_def): CharFieldDefinition.objects.create( model_def=self.model_def, name='f2', max_length=25, null=True ) another_model = AnotherModel.objects.get(pk=another_model.pk) self.assertIsNone(another_model.f2) another_model.f2 = 'Placebo' another_model.save()
def test_mutable_model_base(self): another_model_def = ModelDefinition.objects.create(app_label='app', object_name='AnotherModel') AnotherModel = another_model_def.model_class() CharFieldDefinition.objects.create(model_def=self.model_def, name='f1', max_length=25) base_definition = BaseDefinition(model_def=another_model_def) base_definition.base = self.model_def.model_class() base_definition.save() another_model = AnotherModel.objects.create(f1='Martinal') self.assertTrue(AnotherModel.objects.exists()) CharFieldDefinition.objects.create(model_def=self.model_def, name='f2', max_length=25, null=True) another_model = AnotherModel.objects.get(pk=another_model.pk) self.assertIsNone(another_model.f2) another_model.f2 = 'Placebo' another_model.save()
def test_bases_creation(self): mixin_base = BaseDefinition(base=Mixin) abstract_base = BaseDefinition(base=AbstractModel) abstract_concrete_base = BaseDefinition( base=AbstractConcreteModelSubclass) model_def = ModelDefinition.objects.create( app_label='mutant', object_name='OtherModel', bases=[mixin_base, abstract_base, abstract_concrete_base], ) model = model_def.model_class() self.assertModelTablesColumnDoesntExists(model, 'id') self.assertModelTablesColumnExists(model, 'concretemodel_ptr_id') self.assertModelTablesColumnExists(model, 'abstract_model_field') self.assertModelTablesColumnDoesntExists(model, 'concrete_model_field') self.assertModelTablesColumnExists( model, 'abstract_concrete_model_subclass_field')
def test_get_or_create(self): """ Make sure bases and fields defaults are reaching the model initializer. """ field = CharFieldDefinition(name='name', max_length=32) base = BaseDefinition(base=AbstractModel) ModelDefinition.objects.get_or_create(app_label='mutant', object_name='OtherModel', defaults={ 'bases': [base], 'fields': [field] }) self.assertIsNotNone(field.pk) self.assertIsNotNone(base.pk)
def test_clean(self): bd = BaseDefinition(model_def=self.model_def) # Base must be a class bd.base = BaseDefinitionTest.test_clean self.assertRaisesMessage( ValidationError, _('Base must be a class.'), bd.clean ) # Subclasses of MutableModel are valid bases bd.base = ModelDefinition.objects.create( app_label='mutant', object_name='AnotherModel' ).model_class() try: bd.clean() except ValidationError: self.fail('MutableModel subclasses are valid bases.') # But model definition can't be bases of themselves bd.base = self.model_def.model_class() self.assertRaisesMessage( ValidationError, _("A model definition can't be a base of itself."), bd.clean ) # Mixin objets are valid bases bd.base = Mixin try: bd.clean() except ValidationError: self.fail('Mixin objets are valid bases.') # Abstract model subclasses are valid bases bd.base = AbstractModel try: bd.clean() except ValidationError: self.fail('Abstract Model are valid bases') # Proxy model are not valid bases bd.base = ProxyModel self.assertRaisesMessage( ValidationError, _("Base can't be a proxy model."), bd.clean )
def test_clean(self): bd = BaseDefinition(model_def=self.model_def) # Base must be a class bd.base = BaseDefinitionTest.test_clean self.assertRaisesMessage(ValidationError, _('Base must be a class.'), bd.clean) # Subclasses of MutableModel are valid bases bd.base = ModelDefinition.objects.create( app_label='mutant', object_name='AnotherModel').model_class() try: bd.clean() except ValidationError: self.fail('MutableModel subclasses are valid bases.') # But model definition can't be bases of themselves bd.base = self.model_def.model_class() self.assertRaisesMessage( ValidationError, _("A model definition can't be a base of itself."), bd.clean) # Mixin objets are valid bases bd.base = Mixin try: bd.clean() except ValidationError: self.fail('Mixin objets are valid bases.') # Abstract model subclasses are valid bases bd.base = AbstractModel try: bd.clean() except ValidationError: self.fail('Abstract Model are valid bases') # Proxy model are not valid bases bd.base = ProxyModel self.assertRaisesMessage(ValidationError, _("Base can't be a proxy model."), bd.clean)