def test_with_bad_app(self): """Testing RenameModel with application not in signature""" mutation = RenameModel('TestModel', 'DestModel', db_table='tests_destmodel') message = ( 'Cannot rename the model "badapp.TestModel". The application ' 'could not be found in the signature.' ) with self.assertRaisesMessage(SimulationFailure, message): mutation.run_simulation(app_label='badapp', project_sig=ProjectSignature(), database_state=None)
def test_rename_updates_foreign_key_refs_unchanged_db_table(self): """Testing RenameModel updates ForeignKey references in signature and unchanged db_table """ class DestModel(models.Model): char_field = models.CharField(max_length=20) int_field = models.IntegerField() class Meta: db_table = 'tests_testmodel' class RefModel(models.Model): my_ref = models.ForeignKey(RenameModelBaseModel) self.set_base_model(self.default_base_model, pre_extra_models=[('RefModel', RefModel)]) end, end_sig = self.make_end_signatures(DestModel, 'DestModel') end_sig['tests']['RefModel']['fields']['my_ref']['related_model'] = \ 'tests.DestModel' self.perform_evolution_tests(DestModel, [ RenameModel('TestModel', 'DestModel', db_table='tests_testmodel'), ], ("The model tests.TestModel has been deleted\n" "In model tests.RefModel:\n" " In field 'my_ref':\n" " Property 'related_model' has changed"), [ "ChangeField('RefModel', 'my_ref', initial=None," " related_model='tests.DestModel')", "DeleteModel('TestModel')", ], 'RenameModelForeignKeysSameTable', end=end, end_sig=end_sig)
def test_rename_updates_foreign_key_refs(self): """Testing RenameModel updates ForeignKey references in signature""" class DestModel(BaseTestModel): char_field = models.CharField(max_length=20) int_field = models.IntegerField() class RefModel(BaseTestModel): my_ref = models.ForeignKey(RenameModelBaseModel, on_delete=models.CASCADE) self.set_base_model(self.default_base_model, pre_extra_models=[('RefModel', RefModel)]) end, end_sig = self.make_end_signatures(DestModel, 'DestModel') end_field_sig = (end_sig.get_app_sig('tests').get_model_sig( 'RefModel').get_field_sig('my_ref')) end_field_sig.related_model = 'tests.DestModel' self.perform_evolution_tests(DestModel, [ RenameModel('TestModel', 'DestModel', db_table='tests_destmodel'), ], ("The model tests.TestModel has been deleted\n" "In model tests.RefModel:\n" " In field 'my_ref':\n" " Property 'related_model' has changed"), [ "ChangeField('RefModel', 'my_ref', initial=None," " related_model='tests.DestModel')", "DeleteModel('TestModel')", ], 'RenameModelForeignKeys', end=end, end_sig=end_sig)
def test_with_bad_model(self): """Testing RenameModel with model not in signature""" mutation = RenameModel('TestModel', 'DestModel', db_table='tests_destmodel') project_sig = ProjectSignature() project_sig.add_app_sig(AppSignature(app_id='tests')) message = ( 'Cannot rename the model "tests.TestModel". The model could ' 'not be found in the signature.' ) with self.assertRaisesMessage(SimulationFailure, message): mutation.run_simulation(app_label='tests', project_sig=project_sig, database_state=None)
def test_rename_rename_model(self): """Testing pre-processing RenameModel + RenameModel""" class DestModel(models.Model): my_id = models.AutoField(primary_key=True) char_field = models.CharField(max_length=20) class Meta: db_table = 'tests_testmodel' self.perform_evolution_tests( DestModel, [ RenameModel( 'TestModel', 'TempModel', db_table='tests_testmodel'), RenameModel( 'TempModel', 'DestModel', db_table='tests_testmodel'), ], "The model tests.TestModel has been deleted", [ "DeleteModel('TestModel')", ], 'noop', model_name='DestModel')
def test_add_rename_field_rename_model(self): """Testing pre-processing AddField + RenameField + RenameModel""" class RenamedReffedPreprocModel(models.Model): value = models.IntegerField() class Meta: db_table = 'tests_reffedpreprocmodel' class DestModel(models.Model): my_id = models.AutoField(primary_key=True) char_field = models.CharField(max_length=20) renamed_field = models.ForeignKey(RenamedReffedPreprocModel, null=True) self.set_base_model(self.default_base_model, extra_models=[('ReffedPreprocModel', ReffedPreprocModel)]) # Prepare the renamed model in the end signature. end, end_sig = self.make_end_signatures(DestModel, 'TestModel') end_tests_sig = end_sig['tests'] end_tests_sig['RenamedReffedPreprocModel'] = \ end_tests_sig.pop('ReffedPreprocModel') fields_sig = end_tests_sig['TestModel']['fields'] fields_sig['renamed_field']['related_model'] = \ 'tests.RenamedReffedPreprocModel' self.perform_evolution_tests(DestModel, [ AddField('TestModel', 'added_field', models.ForeignKey, null=True, related_model='tests.ReffedPreprocModel'), RenameField('TestModel', 'added_field', 'renamed_field'), RenameModel('ReffedPreprocModel', 'RenamedReffedPreprocModel', db_table='tests_reffedpreprocmodel'), ], ("The model tests.ReffedPreprocModel has been deleted\n" "In model tests.TestModel:\n" " Field 'renamed_field' has been added"), [ "AddField('TestModel', 'renamed_field', models.ForeignKey," " null=True, related_model='tests.RenamedReffedPreprocModel')", "DeleteModel('ReffedPreprocModel')", ], 'add_rename_field_rename_model', end=end, end_sig=end_sig)
def test_with_rename_model(self): """Testing get_app_pending_mutations always includes RenameModel mutations """ self.ensure_evolution_models() mutations = [ RenameModel('Version', 'ProjectVersion', db_table='django_project_version'), ] pending_mutations = get_app_pending_mutations( app=get_app('django_evolution'), mutations=mutations) self.assertEqual(pending_mutations, mutations)
def test_rename(self): """Testing RenameModel with changed db_table""" class DestModel(models.Model): char_field = models.CharField(max_length=20) int_field = models.IntegerField() self.perform_evolution_tests( DestModel, [ RenameModel( 'TestModel', 'DestModel', db_table='tests_destmodel'), ], "The model tests.TestModel has been deleted", [ "DeleteModel('TestModel')", ], 'RenameModel', model_name='DestModel')
def test_rename_unchanged_db_table(self): """Testing RenameModel with unchanged db_table""" class DestModel(BaseTestModel): char_field = models.CharField(max_length=20) int_field = models.IntegerField() class Meta(BaseTestModel.Meta): db_table = 'tests_testmodel' self.perform_evolution_tests( DestModel, [ RenameModel( 'TestModel', 'DestModel', db_table='tests_testmodel'), ], "The model tests.TestModel has been deleted", [ "DeleteModel('TestModel')", ], 'RenameModelSameTable', model_name='DestModel')
def test_rename_updates_m2m_refs_unchanged_db_table(self): """Testing RenameModel updates ManyToManyField references in signature and unchanged db_table """ class DestModel(BaseTestModel): char_field = models.CharField(max_length=20) int_field = models.IntegerField() class Meta(BaseTestModel.Meta): db_table = 'tests_testmodel' class RefModel(BaseTestModel): my_ref = models.ManyToManyField(RenameModelBaseModel) self.set_base_model(self.default_base_model, pre_extra_models=[('RefModel', RefModel)]) end, end_sig = self.make_end_signatures(DestModel, 'DestModel') end_field_sig = (end_sig.get_app_sig('tests').get_model_sig( 'RefModel').get_field_sig('my_ref')) end_field_sig.related_model = 'tests.DestModel' self.perform_evolution_tests(DestModel, [ RenameModel('TestModel', 'DestModel', db_table='tests_testmodel'), ], ("The model tests.TestModel has been deleted\n" "In model tests.RefModel:\n" " In field 'my_ref':\n" " Property 'related_model' has changed"), [ "ChangeField('RefModel', 'my_ref', initial=None," " related_model='tests.DestModel')", "DeleteModel('TestModel')", ], 'RenameModelManyToManyFieldSameTable', end=end, end_sig=end_sig)
from __future__ import unicode_literals from django_evolution.mutations import AddField, RenameField, RenameModel from django.db import models MUTATIONS = [ RenameModel('FileDiffData', 'LegacyFileDiffData', db_table='diffviewer_filediffdata'), RenameField('FileDiff', 'diff_hash', 'legacy_diff_hash', db_column='diff_hash_id'), RenameField('FileDiff', 'parent_diff_hash', 'legacy_parent_diff_hash', db_column='parent_diff_hash_id'), AddField('FileDiff', 'diff_hash', models.ForeignKey, null=True, db_column='raw_diff_hash_id', related_model='diffviewer.RawFileDiffData'), AddField('FileDiff', 'parent_diff_hash', models.ForeignKey, null=True, db_column='raw_parent_diff_hash_id', related_model='diffviewer.RawFileDiffData'), ]