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='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 dump_model_data(self): # Make sure to remove the model from the app cache because we're # actually testing it's correctly loaded. output = StringIO() remove_from_app_cache(self.model_cls) call_command( 'dumpdata', str(self.model_def), stdout=output, commit=False ) output.seek(0) return json.load(output)
def test_load_mutable_models(self): """ Makes sure mutable models instances are correctly loaded when calling `loaddata`. """ instance = self.model_cls(pk=1) # Make sure to remove the model from the app cache because we're # actually testing it's correctly loaded. remove_from_app_cache(self.model_cls) with NamedTemporaryFile(suffix='.json') as stream: self.serializer.serialize([instance], stream=stream) stream.seek(0) call_command( 'loaddata', stream.name, stdout=StringIO(), commit=False ) self.assertTrue(self.model_cls.objects.filter(pk=instance.pk).exists())