Ticket #25005: ticket_25005.patch

File ticket_25005.patch, 2.9 KB (added by Andriy Sokolovskiy, 9 years ago)

Tests to reproduce the issue

  • tests/migrations/test_autodetector.py

    diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
    index e649731..b2dc562 100644
    a b class AutodetectorTests(TestCase):  
    5555        ("id", models.AutoField(primary_key=True)),
    5656        ("name", models.CharField(max_length=200, default='Ada Lovelace')),
    5757    ])
     58    author_date_of_birth_autonow = ModelState("testapp", "Author", [
     59        ("id", models.AutoField(primary_key=True)),
     60        ("date_of_birth", models.DateField(auto_now=True)),
     61    ])
     62    author_datetime_of_birth_autonow = ModelState("testapp", "Author", [
     63        ("id", models.AutoField(primary_key=True)),
     64        ("date_of_birth", models.DateTimeField(auto_now=True)),
     65    ])
    5866    author_name_deconstructable_1 = ModelState("testapp", "Author", [
    5967        ("id", models.AutoField(primary_key=True)),
    6068        ("name", models.CharField(max_length=200, default=DeconstructableObject())),
    class AutodetectorTests(TestCase):  
    628636        self.assertOperationTypes(changes, 'testapp', 0, ["AddField"])
    629637        self.assertOperationAttributes(changes, "testapp", 0, 0, name="name")
    630638
     639    def test_add_date_field_with_auto_now_not_asking_for_default(self):
     640        class CustomQuestioner(MigrationQuestioner):
     641            def ask_not_null_addition(self, field_name, model_name):
     642                raise Exception("Should not have prompted for not null addition")
     643
     644        # Make state
     645        before = self.make_project_state([self.author_empty])
     646        after = self.make_project_state([self.author_date_of_birth_autonow])
     647        autodetector = MigrationAutodetector(before, after, CustomQuestioner())
     648        changes = autodetector._detect_changes()
     649        # Right number/type of migrations?
     650        self.assertNumberMigrations(changes, 'testapp', 1)
     651        self.assertOperationTypes(changes, 'testapp', 0, ["AddField"])
     652        self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now=True)
     653
     654    def test_add_datetime_field_with_auto_now_not_asking_for_default(self):
     655        class CustomQuestioner(MigrationQuestioner):
     656            def ask_not_null_addition(self, field_name, model_name):
     657                raise Exception("Should not have prompted for not null addition")
     658
     659        # Make state
     660        before = self.make_project_state([self.author_empty])
     661        after = self.make_project_state([self.author_datetime_of_birth_autonow])
     662        autodetector = MigrationAutodetector(before, after, CustomQuestioner())
     663        changes = autodetector._detect_changes()
     664        # Right number/type of migrations?
     665        self.assertNumberMigrations(changes, 'testapp', 1)
     666        self.assertOperationTypes(changes, 'testapp', 0, ["AddField"])
     667        self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now=True)
     668
    631669    def test_remove_field(self):
    632670        """Tests autodetection of removed fields."""
    633671        # Make state
Back to Top