def _perform_delete_app_test(self, sql_name, database=None): # Simulate deletion of the app. self.set_base_model(self.default_base_model, extra_models=self.default_extra_models, db_name=database) end_sig = self.start_sig.clone() end_sig.remove_app_sig('tests') d = Diff(self.start_sig, end_sig) self.assertEqual(sorted(six.iterkeys(d.deleted)), ['tests']) self.assertEqual(d.deleted['tests'], [ 'TestModel', 'AppDeleteAnchor1', 'AppDeleteAnchor2', 'CustomTestModel' ]) mutation = DeleteApplication() self.perform_simulations([mutation], end_sig, ignore_apps=True, db_name=database) test_database_state = self.database_state.clone() test_sig = self.start_sig.clone() app_mutator = AppMutator(app_label='tests', project_sig=test_sig, database_state=test_database_state, database=database) app_mutator.run_mutation(mutation) sql = app_mutator.to_sql() self.assertSQLMappingEqual(sql, sql_name, database=database)
def _perform_delete_app_test(self, sql_name, database=None): # Simulate deletion of the app. self.set_base_model( self.default_base_model, extra_models=self.default_extra_models, db_name=database) end_sig = self.start_sig.clone() end_sig.remove_app_sig('tests') d = Diff(self.start_sig, end_sig) self.assertEqual(sorted(six.iterkeys(d.deleted)), ['tests']) self.assertEqual(d.deleted['tests'], ['TestModel', 'AppDeleteAnchor1', 'AppDeleteAnchor2', 'CustomTestModel']) mutation = DeleteApplication() self.perform_simulations([mutation], end_sig, ignore_apps=True, db_name=database) test_database_state = self.database_state.clone() test_sig = self.start_sig.clone() app_mutator = AppMutator(app_label='tests', project_sig=test_sig, database_state=test_database_state, database=database) app_mutator.run_mutation(mutation) sql = app_mutator.to_sql() self.assertEqual('\n'.join(sql), self.get_sql_mapping(sql_name, database))
def _perform_delete_app_test(self, sql_name, database=None): # Simulate deletion of the app. self.set_base_model(self.default_base_model, extra_models=self.default_extra_models, db_name=database) end_sig = self.copy_sig(self.start_sig) end_sig.pop('tests') d = Diff(self.start_sig, end_sig) self.assertEqual(sorted(d.deleted.keys()), ['tests']) self.assertEqual(d.deleted['tests'], [ 'TestModel', 'AppDeleteAnchor1', 'AppDeleteAnchor2', 'CustomTestModel' ]) mutation = DeleteApplication() self.perform_simulations([mutation], end_sig, ignore_apps=True, db_name=database) test_database_sig = self.copy_sig(self.database_sig) test_sig = self.copy_sig(self.start_sig) app_mutator = AppMutator('tests', test_sig, test_database_sig, database) app_mutator.run_mutation(mutation) sql = app_mutator.to_sql() self.assertEqual('\n'.join(sql), self.get_sql_mapping(sql_name, database))
def run_mutations(): if rescan_indexes: self.test_database_state.rescan_tables() app_mutator = AppMutator(app_label=app_label, project_sig=test_sig, database_state=self.test_database_state, database=db_name) app_mutator.run_mutations(evolutions) return app_mutator.to_sql()
def run_mutations(): if rescan_indexes: self.test_database_state.rescan_indexes() app_mutator = AppMutator(app_label='tests', project_sig=test_sig, database_state=self.test_database_state, database=db_name) app_mutator.run_mutations(evolutions) return app_mutator.to_sql()
def setUp(self): super(ModelMutatorTests, self).setUp() database_state = self.database_state project_sig = self.start_sig app_mutator = AppMutator(app_label='tests', legacy_app_label='old_tests', project_sig=project_sig, database_state=database_state, database=self.default_database_name) self.model_mutator = ModelMutator(app_mutator=app_mutator, model_name=self.default_model_name) self.app_sig = self.start_sig.get_app_sig('tests') self.model_sig = self.app_sig.get_model_sig(self.default_model_name)
def prepare(self, **kwargs): """Prepare state for this task. This will determine if the app's tables need to be deleted from the database, and prepare the SQL for doing so. Args: **kwargs (dict, unused): Keyword arguments passed for task preparation. """ evolver = self.evolver mutation = DeleteApplication() if self.is_mutation_mutable(mutation, app_label=self.app_label): app_mutator = AppMutator.from_evolver( evolver=evolver, app_label=self.app_label) app_mutator.run_mutation(mutation) self.evolution_required = True self.sql = app_mutator.to_sql() self.can_simulate = True self.new_evolutions = []
def test_change_with_add_same_name_other_model(self): """Testing ChangeField with same field name as that added in another model """ class OtherModel(models.Model): int_field = models.IntegerField() test_field = models.CharField(max_length=32, null=True) class OtherDestModel(models.Model): int_field = models.IntegerField() test_field = models.CharField(max_length=32, null=False) class DestModel(models.Model): my_id = models.AutoField(primary_key=True) alt_pk = models.IntegerField() int_field = models.IntegerField(db_column='custom_db_column') int_field1 = models.IntegerField(db_index=True) int_field2 = models.IntegerField(db_index=False) int_field3 = models.IntegerField(unique=True) int_field4 = models.IntegerField(unique=False) char_field = models.CharField(max_length=20) char_field1 = models.CharField(max_length=25, null=True) char_field2 = models.CharField(max_length=30, null=False) m2m_field1 = models.ManyToManyField( ChangeAnchor1, db_table='change_field_non-default_m2m_table') test_field = models.CharField(max_length=32, null=False) self.set_base_model(self.default_base_model, pre_extra_models=[('OtherModel', OtherModel), ('ChangeAnchor1', ChangeAnchor1) ]) end, end_sig = self.make_end_signatures(DestModel, 'TestModel') end2, end_sig2 = self.make_end_signatures(OtherDestModel, 'OtherModel') end.update(end2) end_app_sig = end_sig.get_app_sig('tests') end_app_sig2 = end_sig2.get_app_sig('tests') for model_sig in end_app_sig2.model_sigs: end_app_sig.add_model_sig(model_sig.clone()) d = self.perform_diff_test( end_sig, ("In model tests.OtherModel:\n" " In field 'test_field':\n" " Property 'null' has changed\n" "In model tests.TestModel:\n" " Field 'test_field' has been added"), [ "ChangeField('OtherModel', 'test_field'," " initial=<<USER VALUE REQUIRED>>, null=False)", "AddField('TestModel', 'test_field', models.CharField," " initial=<<USER VALUE REQUIRED>>, max_length=32)", ]) test_sig = self.start_sig.clone() app_mutator = AppMutator(app_label='tests', project_sig=test_sig, database_state=self.database_state) evolutions = d.evolution()['tests'] app_mutator.run_mutations(evolutions) d = Diff(self.start_sig, test_sig) self.assertEqual(str(d), ("In model tests.OtherModel:\n" " In field 'test_field':\n" " Property 'null' has changed\n" "In model tests.TestModel:\n" " Field 'test_field' has been added"))
def run_mutations(): app_mutator = AppMutator('tests', test_sig, self.test_database_sig, db_name) app_mutator.run_mutations(evolutions) return app_mutator.to_sql()
def test_change_with_add_same_name_other_model(self): """Testing ChangeField with same field name as that added in another model """ class OtherModel(models.Model): int_field = models.IntegerField() test_field = models.CharField(max_length=32, null=True) class OtherDestModel(models.Model): int_field = models.IntegerField() test_field = models.CharField(max_length=32, null=False) class DestModel(models.Model): my_id = models.AutoField(primary_key=True) alt_pk = models.IntegerField() int_field = models.IntegerField(db_column='custom_db_column') int_field1 = models.IntegerField(db_index=True) int_field2 = models.IntegerField(db_index=False) int_field3 = models.IntegerField(unique=True) int_field4 = models.IntegerField(unique=False) char_field = models.CharField(max_length=20) char_field1 = models.CharField(max_length=25, null=True) char_field2 = models.CharField(max_length=30, null=False) m2m_field1 = models.ManyToManyField( ChangeAnchor1, db_table='change_field_non-default_m2m_table') test_field = models.CharField(max_length=32, null=False) self.set_base_model( self.default_base_model, pre_extra_models=[ ('OtherModel', OtherModel), ('ChangeAnchor1', ChangeAnchor1) ]) end, end_sig = self.make_end_signatures(DestModel, 'TestModel') end2, end_sig2 = self.make_end_signatures(OtherDestModel, 'OtherModel') end.update(end2) end_sig['tests'].update(end_sig2['tests']) d = self.perform_diff_test( end_sig, ("In model tests.TestModel:\n" " Field 'test_field' has been added\n" "In model tests.OtherModel:\n" " In field 'test_field':\n" " Property 'null' has changed"), [ "AddField('TestModel', 'test_field', models.CharField," " initial=<<USER VALUE REQUIRED>>, max_length=32)", "ChangeField('OtherModel', 'test_field'," " initial=<<USER VALUE REQUIRED>>, null=False)", ]) test_sig = self.copy_sig(self.start_sig) app_mutator = AppMutator('tests', test_sig, self.database_sig) evolutions = d.evolution()['tests'] app_mutator.run_mutations(evolutions) d = Diff(self.start_sig, test_sig) self.assertEqual( str(d), ("In model tests.TestModel:\n" " Field 'test_field' has been added\n" "In model tests.OtherModel:\n" " In field 'test_field':\n" " Property 'null' has changed"))