Ejemplo n.º 1
0
    def test_add_change_change_field(self):
        """Testing pre-processing AddField + ChangeField + ChangeField"""
        class DestModel(models.Model):
            my_id = models.AutoField(primary_key=True)
            char_field = models.CharField(max_length=20)
            added_field = models.CharField(max_length=50, null=True)

        self.perform_evolution_tests(DestModel, [
            AddField('TestModel',
                     'added_field',
                     models.CharField,
                     initial='foo',
                     max_length=20),
            ChangeField('TestModel',
                        'added_field',
                        null=True,
                        initial='bar',
                        max_length=30),
            ChangeField(
                'TestModel', 'added_field', initial='bar', max_length=50),
        ], ("In model tests.TestModel:\n"
            "    Field 'added_field' has been added"), [
                "AddField('TestModel', 'added_field', models.CharField,"
                " max_length=50, null=True)",
            ], 'add_change_field')
Ejemplo n.º 2
0
    def test_with_bad_app(self):
        """Testing ChangeField with application not in signature"""
        mutation = ChangeField('TestModel', 'char_field1')

        message = (
            'Cannot change the field "char_field1" on 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_with_bad_app(self):
        """Testing ChangeField with application not in signature"""
        mutation = ChangeField('TestModel', 'char_field1')

        message = (
            'Cannot change the field "char_field1" on 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)
Ejemplo n.º 4
0
    def test_with_bad_model(self):
        """Testing ChangeField with model not in signature"""
        mutation = ChangeField('TestModel', 'char_field1')

        project_sig = ProjectSignature()
        project_sig.add_app_sig(AppSignature(app_id='tests'))

        message = ('Cannot change the field "char_field1" on 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_get_evolution_content(self):
        """Testing EvolveAppTask.get_evolution_content"""
        evolver = Evolver()
        task = EvolveAppTask(evolver=evolver,
                             app=evo_test,
                             evolutions=[
                                 {
                                     'label':
                                     'my_evolution1',
                                     'mutations': [
                                         ChangeField('TestModel',
                                                     'value',
                                                     max_length=100),
                                     ],
                                 },
                             ])
        task.prepare(hinted=False)

        content = task.get_evolution_content()
        self.assertEqual(
            content, "from __future__ import unicode_literals\n"
            "\n"
            "from django_evolution.mutations import ChangeField\n"
            "\n"
            "\n"
            "MUTATIONS = [\n"
            "    ChangeField('TestModel', 'value', initial=None,"
            " max_length=100),\n"
            "]")
Ejemplo n.º 6
0
    def test_change_with_custom_database(self):
        """Testing ChangeField with custom database"""
        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=False)
            char_field2 = models.CharField(max_length=30, null=False)
            m2m_field1 = models.ManyToManyField(
                ChangeAnchor1, db_table='change_field_non-default_m2m_table')

        self.perform_evolution_tests(DestModel, [
            ChangeField(
                'TestModel', 'char_field1', null=False, initial="abc's xyz"),
        ], ("In model tests.TestModel:\n"
            "    In field 'char_field1':\n"
            "        Property 'null' has changed"), [
                "ChangeField('TestModel', 'char_field1',"
                " initial=<<USER VALUE REQUIRED>>, null=False)",
            ],
                                     'SetNotNullChangeModelWithConstant',
                                     db_name='db_multi')
Ejemplo n.º 7
0
    def test_set_unique_false(self):
        """Testing ChangeField with setting unique=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=False)
            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')

        self.assertIsNotNone(
            self.database_state.find_index(table_name='tests_testmodel',
                                           columns=['int_field3'],
                                           unique=True))

        self.perform_evolution_tests(DestModel, [
            ChangeField('TestModel', 'int_field3', initial=None, unique=False),
        ], ("In model tests.TestModel:\n"
            "    In field 'int_field3':\n"
            "        Property 'unique' has changed"), [
                "ChangeField('TestModel', 'int_field3', initial=None,"
                " unique=False)",
            ], 'RemoveUniqueChangeModel')

        self.assertIsNone(
            self.test_database_state.find_index(table_name='tests_testmodel',
                                                columns=['int_field3'],
                                                unique=True))
Ejemplo n.º 8
0
    def test_change_m2m_db_table(self):
        """Testing ChangeField with setting db_table on ManyToManyField"""
        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='custom_m2m_db_table_name')

        self.perform_evolution_tests(DestModel, [
            ChangeField('TestModel',
                        'm2m_field1',
                        initial=None,
                        db_table='custom_m2m_db_table_name'),
        ], ("In model tests.TestModel:\n"
            "    In field 'm2m_field1':\n"
            "        Property 'db_table' has changed"), [
                "ChangeField('TestModel', 'm2m_field1',"
                " db_table='custom_m2m_db_table_name', initial=None)",
            ], 'M2MDBTableChangeModel')
Ejemplo n.º 9
0
    def test_set_null_true_when_true_noop(self):
        """Testing ChangeField with setting null=True when already True
        is noop
        """
        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')

        self.perform_evolution_tests(
            DestModel, [
                ChangeField('TestModel', 'char_field1', null=True),
            ],
            None, [
                "ChangeField('TestModel', 'char_field1', null=True)",
            ],
            'NoOpChangeModel',
            expect_noop=True)
    def test_prepare_with_hinted_false(self):
        """Testing EvolveAppTask.prepare with hinted=False"""
        from django_evolution.compat.apps import register_app_models
        register_app_models('tests', [('TestModel', EvolverTestModel)],
                            reset=True)

        evolver = Evolver()
        task = EvolveAppTask(evolver=evolver,
                             app=evo_test,
                             evolutions=[
                                 {
                                     'label':
                                     'my_evolution1',
                                     'mutations': [
                                         ChangeField('TestModel',
                                                     'value',
                                                     max_length=100),
                                     ],
                                 },
                             ])
        task.prepare(hinted=False)

        self.assertTrue(task.evolution_required)
        self.assertTrue(task.can_simulate)
        self.assertEqual('\n'.join(task.sql),
                         self.get_sql_mapping('evolve_app_task'))
        self.assertEqual(len(task.new_evolutions), 1)

        evolution = task.new_evolutions[0]
        self.assertEqual(evolution.app_label, 'tests')
        self.assertEqual(evolution.label, 'my_evolution1')
Ejemplo n.º 11
0
    def test_set_null_false_and_initial_callable(self):
        """Testing ChangeField with setting null=False and initial callable"""
        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=False)
            char_field2 = models.CharField(max_length=30, null=False)
            m2m_field1 = models.ManyToManyField(
                ChangeAnchor1, db_table='change_field_non-default_m2m_table')

        self.perform_evolution_tests(DestModel, [
            ChangeField(
                'TestModel',
                'char_field1',
                null=False,
                initial=ChangeSequenceFieldInitial('SetNotNullChangeModel')),
        ], ("In model tests.TestModel:\n"
            "    In field 'char_field1':\n"
            "        Property 'null' has changed"), [
                "ChangeField('TestModel', 'char_field1',"
                " initial=<<USER VALUE REQUIRED>>, null=False)",
            ], 'SetNotNullChangeModelWithCallable')
Ejemplo n.º 12
0
    def test_excludes_unchanged_models(self):
        """Testing get_app_pending_mutations excludes mutations for unchanged
        models
        """
        self.ensure_evolution_models()

        latest_version = Version.objects.current_version()
        old_project_sig = latest_version.signature

        # Make a change in the new signature for Evolution, so it will be
        # considered for pending mutations.
        new_project_sig = old_project_sig.clone()
        model_sig = (
            new_project_sig
            .get_app_sig('django_evolution')
            .get_model_sig('Evolution')
        )
        model_sig.get_field_sig('app_label').field_attrs['max_length'] = 500

        # Only the first mutation should match.
        mutations = [
            ChangeField('Evolution', 'app_label', models.CharField,
                        max_length=500),
            AddField('Version', 'new_field', models.BooleanField,
                     default=True),
        ]

        pending_mutations = get_app_pending_mutations(
            app=get_app('django_evolution'),
            old_project_sig=old_project_sig,
            project_sig=new_project_sig,
            mutations=mutations)

        self.assertEqual(pending_mutations, [mutations[0]])
Ejemplo n.º 13
0
    def test_set_null_false_and_null_initial_value_raises_exception(self):
        """Testing ChangeField with setting null=False and null initial
        value
        """
        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=False)
            char_field2 = models.CharField(max_length=30, null=False)
            m2m_field1 = models.ManyToManyField(
                ChangeAnchor1, db_table='change_field_non-default_m2m_table')

        message = ('Cannot change the field "char_field1" on model '
                   '"tests.TestModel". A non-null initial value needs to be '
                   'specified in the mutation.')

        with self.assertRaisesMessage(SimulationFailure, message):
            self.perform_evolution_tests(DestModel, [
                ChangeField(
                    'TestModel', 'char_field1', null=False, initial=None),
            ], ("In model tests.TestModel:\n"
                "    In field 'char_field1':\n"
                "        Property 'null' has changed"), [
                    "ChangeField('TestModel', 'char_field1',"
                    " initial=<<USER VALUE REQUIRED>>, null=False)",
                ])
Ejemplo n.º 14
0
    def test_set_null_false_without_initial_value_raises_exception(self):
        """Testing ChangeField with setting null=False without initial value"""
        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=False)
            char_field2 = models.CharField(max_length=30, null=False)
            m2m_field1 = models.ManyToManyField(
                ChangeAnchor1, db_table='change_field_non-default_m2m_table')

        self.assertRaisesMessage(
            SimulationFailure,
            ("Cannot change column 'char_field1' on 'tests.TestModel'"
             " without a non-null initial value"),
            lambda: self.perform_evolution_tests(DestModel, [
                ChangeField('TestModel', 'char_field1', null=False),
            ], ("In model tests.TestModel:\n"
                "    In field 'char_field1':\n"
                "        Property 'null' has changed"), [
                    "ChangeField('TestModel', 'char_field1',"
                    " initial=<<USER VALUE REQUIRED>>, null=False)",
                ]))
Ejemplo n.º 15
0
    def test_set_db_index_false(self):
        """Testing ChangeField with setting db_index=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=False)
            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')

        self.assertTrue(
            has_index_with_columns(self.database_sig, 'tests_testmodel',
                                   ['int_field1']))

        self.perform_evolution_tests(DestModel, [
            ChangeField(
                'TestModel', 'int_field1', initial=None, db_index=False),
        ], ("In model tests.TestModel:\n"
            "    In field 'int_field1':\n"
            "        Property 'db_index' has changed"), [
                "ChangeField('TestModel', 'int_field1', initial=None,"
                " db_index=False)",
            ], 'RemoveDBIndexChangeModel')

        self.assertFalse(
            has_index_with_columns(self.test_database_sig, 'tests_testmodel',
                                   ['int_field1']))
Ejemplo n.º 16
0
    def test_change_multiple_attrs_one_field(self):
        """Testing ChangeField with multiple attributes on one field"""
        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=35, null=True)
            m2m_field1 = models.ManyToManyField(
                ChangeAnchor1, db_table='change_field_non-default_m2m_table')

        self.perform_evolution_tests(DestModel, [
            ChangeField('TestModel',
                        'char_field2',
                        initial=None,
                        max_length=35,
                        null=True),
        ], ("In model tests.TestModel:\n"
            "    In field 'char_field2':\n"
            "        Property 'max_length' has changed\n"
            "        Property 'null' has changed"), [
                "ChangeField('TestModel', 'char_field2', initial=None,"
                " max_length=35, null=True)",
            ], 'MultiAttrSingleFieldChangeModel')
Ejemplo n.º 17
0
    def test_increase_max_length(self):
        """Testing ChangeField with increasing max_length of CharField"""
        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=45)
            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')

        self.perform_evolution_tests(DestModel, [
            ChangeField('TestModel', 'char_field', initial=None,
                        max_length=45),
        ], ("In model tests.TestModel:\n"
            "    In field 'char_field':\n"
            "        Property 'max_length' has changed"), [
                "ChangeField('TestModel', 'char_field',"
                " initial=None, max_length=45)",
            ], 'IncreasingMaxLengthChangeModel')
Ejemplo n.º 18
0
    def evolution(self):
        "Generate an evolution that would neutralize the diff"
        mutations = {}

        for app_label, app_changes in self.changed.items():
            for model_name, change in app_changes.get('changed', {}).items():
                for field_name in change.get('added',{}):
                    field_sig = self.current_sig[app_label][model_name]['fields'][field_name]
                    add_params = [(key,field_sig[key])
                                    for key in field_sig.keys()
                                    if key in ATTRIBUTE_DEFAULTS.keys()]
                    add_params.append(('field_type', field_sig['field_type']))

                    if (field_sig['field_type'] != models.ManyToManyField and
                        not field_sig.get('null', ATTRIBUTE_DEFAULTS['null'])):
                        add_params.append(
                            ('initial',
                             get_initial_value(app_label, model_name,
                                               field_name)))

                    if 'related_model' in field_sig:
                        add_params.append(('related_model',
                                           '%s' % field_sig['related_model']))

                    mutations.setdefault(app_label,[]).append(
                        AddField(model_name, field_name, **dict(add_params)))

                for field_name in change.get('deleted',[]):
                    mutations.setdefault(app_label,[]).append(
                        DeleteField(model_name, field_name))

                for field_name,field_change in change.get('changed',{}).items():
                    changed_attrs = {}
                    current_field_sig = self.current_sig[app_label][model_name]['fields'][field_name]

                    for prop in field_change:
                        if prop == 'related_model':
                            changed_attrs[prop] = current_field_sig[prop]
                        else:
                            changed_attrs[prop] = \
                                current_field_sig.get(prop,
                                                      ATTRIBUTE_DEFAULTS[prop])

                    if (changed_attrs.has_key('null') and
                        current_field_sig['field_type'] !=
                            models.ManyToManyField and
                        not current_field_sig.get('null',
                                                  ATTRIBUTE_DEFAULTS['null'])):
                        changed_attrs['initial'] = \
                            get_initial_value(app_label, model_name, field_name)

                    mutations.setdefault(app_label,[]).append(
                        ChangeField(model_name, field_name,current_field_sig, **changed_attrs)) #j----------add

            for model_name in app_changes.get('deleted',{}):
                mutations.setdefault(app_label,[]).append(
                    DeleteModel(model_name))

        return mutations
    def test_with_bad_model(self):
        """Testing ChangeField with model not in signature"""
        mutation = ChangeField('TestModel', 'char_field1')

        project_sig = ProjectSignature()
        project_sig.add_app_sig(AppSignature(app_id='tests'))

        message = (
            'Cannot change the field "char_field1" on 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_evolve(self):
        """Testing Evolver.evolve"""
        model_sig = ModelSignature.from_model(EvolverTestModel)
        model_sig.get_field_sig('value').field_attrs['max_length'] = 50

        app_sig = AppSignature(app_id='tests')
        app_sig.add_model_sig(model_sig)

        orig_version = Version.objects.current_version()
        orig_version.signature.add_app_sig(app_sig)
        orig_version.save()

        with ensure_test_db(model_entries=[('TestModel', EvolverTestModel)]):
            evolver = Evolver()
            evolver.queue_task(
                EvolveAppTask(evolver=evolver,
                              app=evo_test,
                              evolutions=[
                                  {
                                      'label':
                                      'my_evolution1',
                                      'mutations': [
                                          ChangeField('TestModel',
                                                      'value',
                                                      max_length=200),
                                      ],
                                  },
                                  {
                                      'label':
                                      'my_evolution2',
                                      'mutations': [
                                          AddField('TestModel',
                                                   'new_field',
                                                   models.BooleanField,
                                                   null=True),
                                      ],
                                  },
                              ]))
            evolver.evolve()

        self.assertTrue(evolver.evolved)

        version = Version.objects.current_version()
        self.assertNotEqual(version, orig_version)
        self.assertFalse(version.is_hinted())

        evolutions = list(version.evolutions.all())
        self.assertEqual(len(evolutions), 2)
        self.assertEqual(evolutions[0].app_label, 'tests')
        self.assertEqual(evolutions[0].label, 'my_evolution1')
        self.assertEqual(evolutions[1].app_label, 'tests')
        self.assertEqual(evolutions[1].label, 'my_evolution2')

        model_sig = (
            version.signature.get_app_sig('tests').get_model_sig('TestModel'))
        self.assertEqual(
            model_sig.get_field_sig('value').field_attrs['max_length'], 200)
        self.assertIsNotNone(model_sig.get_field_sig('new_field'))
Ejemplo n.º 21
0
    def test_redundant_attributes(self):
        """Testing ChangeField with redundant attributes"""
        class DestModel(models.Model):
            my_id = models.AutoField(primary_key=True)
            alt_pk = models.IntegerField()
            int_field = models.IntegerField(db_column='custom_db_column3')
            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=35)
            char_field1 = models.CharField(max_length=25, null=True)
            char_field2 = models.CharField(max_length=30, null=True)
            m2m_field1 = models.ManyToManyField(
                ChangeAnchor1, db_table='change_field_non-default_m2m_table')

        self.perform_evolution_tests(DestModel, [
            ChangeField('TestModel',
                        'char_field2',
                        initial=None,
                        max_length=30,
                        null=True),
            ChangeField('TestModel',
                        'int_field',
                        initial=None,
                        db_column='custom_db_column3',
                        unique=False,
                        db_index=False),
            ChangeField('TestModel', 'char_field', initial=None,
                        max_length=35),
        ], ("In model tests.TestModel:\n"
            "    In field 'int_field':\n"
            "        Property 'db_column' has changed\n"
            "    In field 'char_field':\n"
            "        Property 'max_length' has changed\n"
            "    In field 'char_field2':\n"
            "        Property 'null' has changed"), [
                "ChangeField('TestModel', 'int_field',"
                " db_column='custom_db_column3', initial=None)",
                "ChangeField('TestModel', 'char_field', initial=None,"
                " max_length=35)",
                "ChangeField('TestModel', 'char_field2', initial=None,"
                " null=True)",
            ], 'RedundantAttrsChangeModel')
Ejemplo n.º 22
0
    def test_rename_change_rename_change_field(self):
        """Testing pre-processing RenameField + ChangeField + RenameField +
        ChangeField
        """
        class DestModel(models.Model):
            my_id = models.AutoField(primary_key=True)
            renamed_field = models.CharField(max_length=50, null=True)

        self.perform_evolution_tests(DestModel, [
            RenameField('TestModel', 'char_field', 'foo_field'),
            ChangeField('TestModel', 'foo_field', max_length=30, null=True),
            RenameField('TestModel', 'foo_field', 'renamed_field'),
            ChangeField('TestModel', 'renamed_field', max_length=50),
        ], ("In model tests.TestModel:\n"
            "    Field 'renamed_field' has been added\n"
            "    Field 'char_field' has been deleted"), [
                "AddField('TestModel', 'renamed_field', models.CharField,"
                " max_length=50, null=True)",
                "DeleteField('TestModel', 'char_field')",
            ], 'rename_change_rename_change_field')
Ejemplo n.º 23
0
    def test_change_delete_field(self):
        """Testing pre-processing ChangeField + DeleteField"""
        class DestModel(models.Model):
            my_id = models.AutoField(primary_key=True)

        self.perform_evolution_tests(DestModel, [
            ChangeField('TestModel', 'char_field', null=True),
            DeleteField('TestModel', 'char_field'),
        ], ("In model tests.TestModel:\n"
            "    Field 'char_field' has been deleted"), [
                "DeleteField('TestModel', 'char_field')",
            ], 'delete_char_field')
Ejemplo n.º 24
0
    def test_excludes_new_models(self):
        """Testing get_app_pending_mutations excludes mutations for new models
        """
        self.ensure_evolution_models()

        latest_version = Version.objects.current_version()
        old_project_sig = latest_version.signature

        # Remove the model signature from the old project signature.
        old_project_sig.get_app_sig('django_evolution').remove_model_sig(
            'Evolution')

        # Make a change in the new signature for Evolution, so it will be
        # considered for pending mutations.
        new_project_sig = old_project_sig.clone()

        model_sig = (
            new_project_sig
            .get_app_sig('django_evolution')
            .get_model_sig('Version')
        )
        model_sig.get_field_sig('when').field_attrs['null'] = False

        # Only the first mutation should match.
        mutations = [
            ChangeField('Evolution', 'field1', models.CharField,
                        max_length=500),
            ChangeField('Version', 'when', models.DateTimeField,
                        null=True),
        ]

        pending_mutations = get_app_pending_mutations(
            app=get_app('django_evolution'),
            old_project_sig=old_project_sig,
            project_sig=new_project_sig,
            mutations=mutations)

        self.assertEqual(pending_mutations, [mutations[1]])
Ejemplo n.º 25
0
    def test_set_db_index_true_and_existing_index(self):
        """Testing ChangeField with setting db_index=True and existing index
        in the database
        """
        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=True)
            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')

        evolver = EvolutionOperationsMulti('default',
                                           self.database_sig).get_evolver()
        index_name = evolver.get_default_index_name(
            'tests_testmodel', DestModel._meta.get_field('int_field2'))

        self.database_sig['tests_testmodel']['indexes'] = {
            index_name: {
                'unique': False,
                'columns': ['int_field2'],
            }
        }

        self.assertTrue(
            has_index_with_columns(self.database_sig, 'tests_testmodel',
                                   ['int_field2']))

        self.perform_evolution_tests(DestModel, [
            ChangeField('TestModel', 'int_field2', initial=None,
                        db_index=True),
        ], ("In model tests.TestModel:\n"
            "    In field 'int_field2':\n"
            "        Property 'db_index' has changed"), [
                "ChangeField('TestModel', 'int_field2', initial=None,"
                " db_index=True)",
            ],
                                     'AddDBIndexNoOpChangeModel',
                                     rescan_indexes=False)

        self.assertTrue(
            has_index_with_columns(self.test_database_sig, 'tests_testmodel',
                                   ['int_field2']))
Ejemplo n.º 26
0
    def test_add_change_delete_field(self):
        """Testing pre-processing AddField + ChangeField + DeleteField"""
        class DestModel(models.Model):
            my_id = models.AutoField(primary_key=True)
            char_field = models.CharField(max_length=20)

        self.perform_evolution_tests(DestModel, [
            AddField('TestModel',
                     'added_field',
                     models.CharField,
                     initial='foo',
                     max_length=20),
            ChangeField('TestModel', 'added_field', null=True),
            DeleteField('TestModel', 'added_field'),
        ],
                                     '', [],
                                     'noop',
                                     expect_noop=True)
Ejemplo n.º 27
0
    def test_with_unregistered_app_sig(self):
        """Testing get_app_pending_mutations with unregistered app signature
        """
        self.ensure_evolution_models()

        latest_version = Version.objects.current_version()
        self.assertIsNone(
            latest_version.signature.get_app_sig('evolutions_app'))

        mutations = [
            ChangeField('EvolutionsAppTestModel', 'char_field',
                        models.CharField, max_length=10),
            AddField('EvolutionsAppTestModel', 'new_field',
                     models.IntegerField, default=42,
                     null=True),
        ]

        pending_mutations = get_app_pending_mutations(
            app=get_app('evolutions_app'),
            mutations=mutations)

        self.assertEqual(pending_mutations, mutations)
Ejemplo n.º 28
0
    def test_set_db_index_false_and_no_existing_index(self):
        """Testing ChangeField with setting db_index=False without an
        existing index in the database
        """
        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=False)
            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')

        self.database_state.clear_indexes('tests_testmodel')

        self.assertIsNone(
            self.database_state.find_index(table_name='tests_testmodel',
                                           columns=['int_field1']))

        self.perform_evolution_tests(DestModel, [
            ChangeField(
                'TestModel', 'int_field1', initial=None, db_index=False),
        ], ("In model tests.TestModel:\n"
            "    In field 'int_field1':\n"
            "        Property 'db_index' has changed"), [
                "ChangeField('TestModel', 'int_field1', db_index=False,"
                " initial=None)",
            ],
                                     'RemoveDBIndexNoOpChangeModel',
                                     rescan_indexes=False)

        self.assertIsNone(
            self.test_database_state.find_index(table_name='tests_testmodel',
                                                columns=['int_field1']))
Ejemplo n.º 29
0
    def test_set_null_true(self):
        """Testing ChangeField with setting null=True"""
        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=True)
            m2m_field1 = models.ManyToManyField(
                ChangeAnchor1, db_table='change_field_non-default_m2m_table')

        self.perform_evolution_tests(DestModel, [
            ChangeField('TestModel', 'char_field2', initial=None, null=True),
        ], ("In model tests.TestModel:\n"
            "    In field 'char_field2':\n"
            "        Property 'null' has changed"), [
                "ChangeField('TestModel', 'char_field2',"
                " initial=None, null=True)",
            ], 'SetNullChangeModel')
    def test_execute(self):
        """Testing EvolveAppTask.execute"""
        saw = set()

        @receiver(applying_evolution)
        def _on_applying(sender, **kwargs):
            saw.add('on_applying')
            self.assertIs(sender, evolver)
            self.assertIs(kwargs['task'], task)

        @receiver(applied_evolution)
        def _on_applied(sender, **kwargs):
            saw.add('on_applied')
            self.assertIs(sender, evolver)
            self.assertIs(kwargs['task'], task)

        evolver = Evolver()
        task = EvolveAppTask(evolver=evolver,
                             app=evo_test,
                             evolutions=[
                                 {
                                     'label':
                                     'my_evolution1',
                                     'mutations': [
                                         ChangeField('TestModel',
                                                     'value',
                                                     max_length=100),
                                     ],
                                 },
                             ])
        task.prepare(hinted=False)

        with ensure_test_db(model_entries=[('TestModel', EvolverTestModel)]):
            task.execute(connections['default'].cursor())

        self.assertEqual(saw, set(['on_applying', 'on_applied']))
Ejemplo n.º 31
0
from __future__ import unicode_literals

from django_evolution.mutations import ChangeField

MUTATIONS = [
    ChangeField('ReviewRequest', 'repository', initial=None, null=True)
]
Ejemplo n.º 32
0
from django.db import models

from django_evolution.mutations import ChangeField

MUTATIONS = [
    ChangeField('Session', 'expire_date', initial=None, db_index=True)
]