summaryrefslogtreecommitdiff
path: root/tests/migrations
diff options
context:
space:
mode:
authorkimsoungryoul <kimsoungryoul@gmail.com>2022-10-16 14:59:39 +0900
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2022-12-28 06:28:07 +0100
commit78f163a4fb3937aca2e71786fbdd51a0ef39629e (patch)
tree7e61c2f8d96b9dab60e317d3483460064327d701 /tests/migrations
parent68ef274bc505cd44f305c03cbf84cf08826200a8 (diff)
downloaddjango-78f163a4fb3937aca2e71786fbdd51a0ef39629e.tar.gz
Fixed #18468 -- Added support for comments on columns and tables.
Thanks Jared Chung, Tom Carrick, David Smith, Nick Pope, and Mariusz Felisiak for reviews. Co-authored-by: Mariusz Felisiak <felisiak.mariusz@gmail.com> Co-authored-by: Nick Pope <nick@nickpope.me.uk>
Diffstat (limited to 'tests/migrations')
-rw-r--r--tests/migrations/test_autodetector.py60
-rw-r--r--tests/migrations/test_base.py14
-rw-r--r--tests/migrations/test_operations.py31
3 files changed, 105 insertions, 0 deletions
diff --git a/tests/migrations/test_autodetector.py b/tests/migrations/test_autodetector.py
index d2f9be75f7..82ddb17543 100644
--- a/tests/migrations/test_autodetector.py
+++ b/tests/migrations/test_autodetector.py
@@ -773,6 +773,14 @@ class AutodetectorTests(BaseAutodetectorTests):
"verbose_name": "Authi",
},
)
+ author_with_db_table_comment = ModelState(
+ "testapp",
+ "Author",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ],
+ {"db_table_comment": "Table comment"},
+ )
author_with_db_table_options = ModelState(
"testapp",
"Author",
@@ -2349,6 +2357,58 @@ class AutodetectorTests(BaseAutodetectorTests):
changes, "testapp", 0, 1, name="newauthor", table="author_three"
)
+ def test_alter_db_table_comment_add(self):
+ changes = self.get_changes(
+ [self.author_empty], [self.author_with_db_table_comment]
+ )
+ self.assertNumberMigrations(changes, "testapp", 1)
+ self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"])
+ self.assertOperationAttributes(
+ changes, "testapp", 0, 0, name="author", table_comment="Table comment"
+ )
+
+ def test_alter_db_table_comment_change(self):
+ author_with_new_db_table_comment = ModelState(
+ "testapp",
+ "Author",
+ [
+ ("id", models.AutoField(primary_key=True)),
+ ],
+ {"db_table_comment": "New table comment"},
+ )
+ changes = self.get_changes(
+ [self.author_with_db_table_comment],
+ [author_with_new_db_table_comment],
+ )
+ self.assertNumberMigrations(changes, "testapp", 1)
+ self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"])
+ self.assertOperationAttributes(
+ changes,
+ "testapp",
+ 0,
+ 0,
+ name="author",
+ table_comment="New table comment",
+ )
+
+ def test_alter_db_table_comment_remove(self):
+ changes = self.get_changes(
+ [self.author_with_db_table_comment],
+ [self.author_empty],
+ )
+ self.assertNumberMigrations(changes, "testapp", 1)
+ self.assertOperationTypes(changes, "testapp", 0, ["AlterModelTableComment"])
+ self.assertOperationAttributes(
+ changes, "testapp", 0, 0, name="author", db_table_comment=None
+ )
+
+ def test_alter_db_table_comment_no_changes(self):
+ changes = self.get_changes(
+ [self.author_with_db_table_comment],
+ [self.author_with_db_table_comment],
+ )
+ self.assertNumberMigrations(changes, "testapp", 0)
+
def test_identical_regex_doesnt_alter(self):
from_state = ModelState(
"testapp",
diff --git a/tests/migrations/test_base.py b/tests/migrations/test_base.py
index 3f1559b8d6..f038cd7605 100644
--- a/tests/migrations/test_base.py
+++ b/tests/migrations/test_base.py
@@ -75,6 +75,20 @@ class MigrationTestBase(TransactionTestCase):
def assertColumnCollation(self, table, column, collation, using="default"):
self.assertEqual(self._get_column_collation(table, column, using), collation)
+ def _get_table_comment(self, table, using):
+ with connections[using].cursor() as cursor:
+ return next(
+ t.comment
+ for t in connections[using].introspection.get_table_list(cursor)
+ if t.name == table
+ )
+
+ def assertTableComment(self, table, comment, using="default"):
+ self.assertEqual(self._get_table_comment(table, using), comment)
+
+ def assertTableCommentNotExists(self, table, using="default"):
+ self.assertIn(self._get_table_comment(table, using), [None, ""])
+
def assertIndexExists(
self, table, columns, value=True, using="default", index_type=None
):
diff --git a/tests/migrations/test_operations.py b/tests/migrations/test_operations.py
index 2bcb38feae..129360629d 100644
--- a/tests/migrations/test_operations.py
+++ b/tests/migrations/test_operations.py
@@ -1922,6 +1922,37 @@ class OperationTests(OperationTestBase):
operation.database_forwards(app_label, editor, new_state, project_state)
self.assertColumnExists(rider_table, "pony_id")
+ @skipUnlessDBFeature("supports_comments")
+ def test_alter_model_table_comment(self):
+ app_label = "test_almotaco"
+ project_state = self.set_up_test_model(app_label)
+ pony_table = f"{app_label}_pony"
+ # Add table comment.
+ operation = migrations.AlterModelTableComment("Pony", "Custom pony comment")
+ self.assertEqual(operation.describe(), "Alter Pony table comment")
+ self.assertEqual(operation.migration_name_fragment, "alter_pony_table_comment")
+ new_state = project_state.clone()
+ operation.state_forwards(app_label, new_state)
+ self.assertEqual(
+ new_state.models[app_label, "pony"].options["db_table_comment"],
+ "Custom pony comment",
+ )
+ self.assertTableCommentNotExists(pony_table)
+ with connection.schema_editor() as editor:
+ operation.database_forwards(app_label, editor, project_state, new_state)
+ self.assertTableComment(pony_table, "Custom pony comment")
+ # Reversal.
+ with connection.schema_editor() as editor:
+ operation.database_backwards(app_label, editor, new_state, project_state)
+ self.assertTableCommentNotExists(pony_table)
+ # Deconstruction.
+ definition = operation.deconstruct()
+ self.assertEqual(definition[0], "AlterModelTableComment")
+ self.assertEqual(definition[1], [])
+ self.assertEqual(
+ definition[2], {"name": "Pony", "table_comment": "Custom pony comment"}
+ )
+
def test_alter_field_pk(self):
"""
The AlterField operation on primary keys (things like PostgreSQL's