mirror of https://github.com/django/django.git
Used mock in MigrationQuestioner tests
Thanks Andriy Sokolovskiy and Simon Charette for the review.
This commit is contained in:
parent
290ff35e6c
commit
7bb7670ee4
|
@ -553,7 +553,7 @@ class AutodetectorTests(TestCase):
|
||||||
# Use project state to make a new migration change set
|
# Use project state to make a new migration change set
|
||||||
before = self.make_project_state([])
|
before = self.make_project_state([])
|
||||||
after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable, self.third_thing])
|
after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable, self.third_thing])
|
||||||
autodetector = MigrationAutodetector(before, after, MigrationQuestioner(defaults={"ask_initial": True}))
|
autodetector = MigrationAutodetector(before, after, MigrationQuestioner({"ask_initial": True}))
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
# Run through arrange_for_graph
|
# Run through arrange_for_graph
|
||||||
graph = MigrationGraph()
|
graph = MigrationGraph()
|
||||||
|
@ -652,18 +652,16 @@ class AutodetectorTests(TestCase):
|
||||||
self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
|
self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
|
||||||
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
|
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
|
||||||
|
|
||||||
def test_alter_field_to_not_null_with_default(self):
|
@mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration',
|
||||||
|
side_effect=AssertionError("Should not have prompted for not null addition"))
|
||||||
|
def test_alter_field_to_not_null_with_default(self, mocked_ask_method):
|
||||||
"""
|
"""
|
||||||
#23609 - Tests autodetection of nullable to non-nullable alterations.
|
#23609 - Tests autodetection of nullable to non-nullable alterations.
|
||||||
"""
|
"""
|
||||||
class CustomQuestioner(MigrationQuestioner):
|
|
||||||
def ask_not_null_alteration(self, field_name, model_name):
|
|
||||||
raise Exception("Should not have prompted for not null addition")
|
|
||||||
|
|
||||||
# Make state
|
# Make state
|
||||||
before = self.make_project_state([self.author_name_null])
|
before = self.make_project_state([self.author_name_null])
|
||||||
after = self.make_project_state([self.author_name_default])
|
after = self.make_project_state([self.author_name_default])
|
||||||
autodetector = MigrationAutodetector(before, after, CustomQuestioner())
|
autodetector = MigrationAutodetector(before, after)
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
# Right number/type of migrations?
|
# Right number/type of migrations?
|
||||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||||
|
@ -671,42 +669,36 @@ class AutodetectorTests(TestCase):
|
||||||
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
|
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
|
||||||
self.assertOperationFieldAttributes(changes, "testapp", 0, 0, default='Ada Lovelace')
|
self.assertOperationFieldAttributes(changes, "testapp", 0, 0, default='Ada Lovelace')
|
||||||
|
|
||||||
def test_alter_field_to_not_null_without_default(self):
|
@mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration',
|
||||||
|
return_value=models.NOT_PROVIDED)
|
||||||
|
def test_alter_field_to_not_null_without_default(self, mocked_ask_method):
|
||||||
"""
|
"""
|
||||||
#23609 - Tests autodetection of nullable to non-nullable alterations.
|
#23609 - Tests autodetection of nullable to non-nullable alterations.
|
||||||
"""
|
"""
|
||||||
class CustomQuestioner(MigrationQuestioner):
|
|
||||||
def ask_not_null_alteration(self, field_name, model_name):
|
|
||||||
# Ignore for now, and let me handle existing rows with NULL
|
|
||||||
# myself (e.g. adding a RunPython or RunSQL operation in the new
|
|
||||||
# migration file before the AlterField operation)
|
|
||||||
return models.NOT_PROVIDED
|
|
||||||
|
|
||||||
# Make state
|
# Make state
|
||||||
before = self.make_project_state([self.author_name_null])
|
before = self.make_project_state([self.author_name_null])
|
||||||
after = self.make_project_state([self.author_name])
|
after = self.make_project_state([self.author_name])
|
||||||
autodetector = MigrationAutodetector(before, after, CustomQuestioner())
|
autodetector = MigrationAutodetector(before, after)
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
|
self.assertEqual(mocked_ask_method.call_count, 1)
|
||||||
# Right number/type of migrations?
|
# Right number/type of migrations?
|
||||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||||
self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
|
self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
|
||||||
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
|
self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
|
||||||
self.assertOperationFieldAttributes(changes, "testapp", 0, 0, default=models.NOT_PROVIDED)
|
self.assertOperationFieldAttributes(changes, "testapp", 0, 0, default=models.NOT_PROVIDED)
|
||||||
|
|
||||||
def test_alter_field_to_not_null_oneoff_default(self):
|
@mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration',
|
||||||
|
return_value='Some Name')
|
||||||
|
def test_alter_field_to_not_null_oneoff_default(self, mocked_ask_method):
|
||||||
"""
|
"""
|
||||||
#23609 - Tests autodetection of nullable to non-nullable alterations.
|
#23609 - Tests autodetection of nullable to non-nullable alterations.
|
||||||
"""
|
"""
|
||||||
class CustomQuestioner(MigrationQuestioner):
|
|
||||||
def ask_not_null_alteration(self, field_name, model_name):
|
|
||||||
# Provide a one-off default now (will be set on all existing rows)
|
|
||||||
return 'Some Name'
|
|
||||||
|
|
||||||
# Make state
|
# Make state
|
||||||
before = self.make_project_state([self.author_name_null])
|
before = self.make_project_state([self.author_name_null])
|
||||||
after = self.make_project_state([self.author_name])
|
after = self.make_project_state([self.author_name])
|
||||||
autodetector = MigrationAutodetector(before, after, CustomQuestioner())
|
autodetector = MigrationAutodetector(before, after)
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
|
self.assertEqual(mocked_ask_method.call_count, 1)
|
||||||
# Right number/type of migrations?
|
# Right number/type of migrations?
|
||||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||||
self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
|
self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
|
||||||
|
@ -979,9 +971,7 @@ class AutodetectorTests(TestCase):
|
||||||
# Make state
|
# Make state
|
||||||
before = self.make_project_state([self.author_with_db_table_options])
|
before = self.make_project_state([self.author_with_db_table_options])
|
||||||
after = self.make_project_state([self.author_renamed_with_db_table_options])
|
after = self.make_project_state([self.author_renamed_with_db_table_options])
|
||||||
autodetector = MigrationAutodetector(
|
autodetector = MigrationAutodetector(before, after, MigrationQuestioner({"ask_rename_model": True}))
|
||||||
before, after, MigrationQuestioner({"ask_rename_model": True})
|
|
||||||
)
|
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
# Right number/type of migrations?
|
# Right number/type of migrations?
|
||||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||||
|
@ -996,9 +986,7 @@ class AutodetectorTests(TestCase):
|
||||||
# Make state
|
# Make state
|
||||||
before = self.make_project_state([self.author_with_db_table_options])
|
before = self.make_project_state([self.author_with_db_table_options])
|
||||||
after = self.make_project_state([self.author_renamed_with_new_db_table_options])
|
after = self.make_project_state([self.author_renamed_with_new_db_table_options])
|
||||||
autodetector = MigrationAutodetector(
|
autodetector = MigrationAutodetector(before, after, MigrationQuestioner({"ask_rename_model": True}))
|
||||||
before, after, MigrationQuestioner({"ask_rename_model": True})
|
|
||||||
)
|
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
# Right number/type of migrations?
|
# Right number/type of migrations?
|
||||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||||
|
@ -1491,15 +1479,13 @@ class AutodetectorTests(TestCase):
|
||||||
self.assertOperationAttributes(changes, 'testapp', 0, 0, name="publisher")
|
self.assertOperationAttributes(changes, 'testapp', 0, 0, name="publisher")
|
||||||
self.assertOperationAttributes(changes, 'testapp', 0, 1, name="Publisher")
|
self.assertOperationAttributes(changes, 'testapp', 0, 1, name="Publisher")
|
||||||
|
|
||||||
def test_add_many_to_many(self):
|
@mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition',
|
||||||
|
side_effect=AssertionError("Should not have prompted for not null addition"))
|
||||||
|
def test_add_many_to_many(self, mocked_ask_method):
|
||||||
"""#22435 - Adding a ManyToManyField should not prompt for a default."""
|
"""#22435 - Adding a ManyToManyField should not prompt for a default."""
|
||||||
class CustomQuestioner(MigrationQuestioner):
|
|
||||||
def ask_not_null_addition(self, field_name, model_name):
|
|
||||||
raise Exception("Should not have prompted for not null addition")
|
|
||||||
|
|
||||||
before = self.make_project_state([self.author_empty, self.publisher])
|
before = self.make_project_state([self.author_empty, self.publisher])
|
||||||
after = self.make_project_state([self.author_with_m2m, self.publisher])
|
after = self.make_project_state([self.author_with_m2m, self.publisher])
|
||||||
autodetector = MigrationAutodetector(before, after, CustomQuestioner())
|
autodetector = MigrationAutodetector(before, after)
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
# Right number/type of migrations?
|
# Right number/type of migrations?
|
||||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||||
|
@ -2044,19 +2030,18 @@ class AutodetectorTests(TestCase):
|
||||||
self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
|
self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
|
||||||
self.assertMigrationDependencies(changes, 'a', 0, [])
|
self.assertMigrationDependencies(changes, 'a', 0, [])
|
||||||
|
|
||||||
def test_add_blank_textfield_and_charfield(self):
|
@mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition',
|
||||||
|
side_effect=AssertionError("Should not have prompted for not null addition"))
|
||||||
|
def test_add_blank_textfield_and_charfield(self, mocked_ask_method):
|
||||||
"""
|
"""
|
||||||
#23405 - Adding a NOT NULL and blank `CharField` or `TextField`
|
#23405 - Adding a NOT NULL and blank `CharField` or `TextField`
|
||||||
without default should not prompt for a default.
|
without default should not prompt for a default.
|
||||||
"""
|
"""
|
||||||
class CustomQuestioner(MigrationQuestioner):
|
|
||||||
def ask_not_null_addition(self, field_name, model_name):
|
|
||||||
raise Exception("Should not have prompted for not null addition")
|
|
||||||
|
|
||||||
before = self.make_project_state([self.author_empty])
|
before = self.make_project_state([self.author_empty])
|
||||||
after = self.make_project_state([self.author_with_biography_blank])
|
after = self.make_project_state([self.author_with_biography_blank])
|
||||||
autodetector = MigrationAutodetector(before, after, CustomQuestioner())
|
autodetector = MigrationAutodetector(before, after)
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
|
# Right number/type of migrations?
|
||||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||||
self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"])
|
self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"])
|
||||||
self.assertOperationAttributes(changes, 'testapp', 0, 0)
|
self.assertOperationAttributes(changes, 'testapp', 0, 0)
|
||||||
|
@ -2069,11 +2054,10 @@ class AutodetectorTests(TestCase):
|
||||||
"""
|
"""
|
||||||
before = self.make_project_state([self.author_empty])
|
before = self.make_project_state([self.author_empty])
|
||||||
after = self.make_project_state([self.author_with_biography_non_blank])
|
after = self.make_project_state([self.author_with_biography_non_blank])
|
||||||
autodetector = MigrationAutodetector(before, after, MigrationQuestioner())
|
autodetector = MigrationAutodetector(before, after)
|
||||||
changes = autodetector._detect_changes()
|
changes = autodetector._detect_changes()
|
||||||
# need to check for questioner call
|
|
||||||
self.assertTrue(mocked_ask_method.called)
|
|
||||||
self.assertEqual(mocked_ask_method.call_count, 2)
|
self.assertEqual(mocked_ask_method.call_count, 2)
|
||||||
|
# Right number/type of migrations?
|
||||||
self.assertNumberMigrations(changes, 'testapp', 1)
|
self.assertNumberMigrations(changes, 'testapp', 1)
|
||||||
self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"])
|
self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"])
|
||||||
self.assertOperationAttributes(changes, 'testapp', 0, 0)
|
self.assertOperationAttributes(changes, 'testapp', 0, 0)
|
||||||
|
|
Loading…
Reference in New Issue