Skip to content

Commit

Permalink
Test edits.
Browse files Browse the repository at this point in the history
  • Loading branch information
felixxm committed Dec 27, 2022
1 parent 807f8d2 commit 417288d
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 398 deletions.
2 changes: 1 addition & 1 deletion django/db/backends/base/schema.py
Expand Up @@ -641,7 +641,7 @@ def alter_db_table_comment(self, model, old_db_table_comment, new_db_table_comme
self.sql_alter_table_comment
% {
"table": self.quote_name(model._meta.db_table),
"comment": self.quote_value(new_db_table_comment),
"comment": self.quote_value(new_db_table_comment or ""),
}
)

Expand Down
2 changes: 1 addition & 1 deletion django/db/migrations/operations/models.py
Expand Up @@ -560,7 +560,7 @@ def database_backwards(self, app_label, schema_editor, from_state, to_state):
return self.database_forwards(app_label, schema_editor, from_state, to_state)

def describe(self):
return f"Alter {self.name_lower} comment"
return f"Alter {self.name} table comment"

@property
def migration_name_fragment(self):
Expand Down
39 changes: 14 additions & 25 deletions tests/migrations/test_base.py
Expand Up @@ -75,6 +75,20 @@ def _get_column_collation(self, table, column, using):
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
):
Expand Down Expand Up @@ -152,31 +166,6 @@ def assertFKExists(self, table, columns, to, value=True, using="default"):
def assertFKNotExists(self, table, columns, to):
return self.assertFKExists(table, columns, to, False)

def _get_column_db_comment(self, table, column, using):
return [
c.comment
for c in self.get_table_description(table, using=using)
if c.name == column
][0]

def assertColumnCommentEquals(self, table, column, column_comment, using="default"):
self.assertEqual(
self._get_column_db_comment(table, column, using), column_comment
)

def assertColumnCommentNotEquals(
self, table, column, column_comment, using="default"
):
self.assertNotEqual(
self._get_column_db_comment(table, column, using), column_comment
)

def assertColumnCommentExists(self, table, column, using="default"):
self.assertTrue(self._get_column_db_comment(table, column, using))

def assertColumnCommentNotExists(self, table, column, using="default"):
self.assertFalse(self._get_column_db_comment(table, column, using))

@contextmanager
def temporary_migration_module(self, app_label="migrations", module=None):
"""
Expand Down
64 changes: 0 additions & 64 deletions tests/migrations/test_commands.py
Expand Up @@ -690,70 +690,6 @@ def test_showmigrations_plan_no_migrations(self):
)
self.assertEqual("(no migrations)\n", out.getvalue().lower())

@skipUnlessDBFeature("supports_comments")
@override_settings(
MIGRATION_MODULES={"migrations": "migrations.test_migrations_db_comment"}
)
def test_migrate_db_comment(self):
stdout = io.StringIO()
call_command(
"migrate", "migrations", "0001", verbosity=2, stdout=stdout, no_color=True
)

# The correct tables exist
self.assertColumnCommentEquals(
"migrations_author", "name", "db comment to name Field"
)
self.assertColumnCommentNotExists("migrations_author", "slug")
self.assertColumnCommentEquals(
"migrations_author", "age", "db comment to age Field"
)
self.assertColumnCommentEquals(
"migrations_author", "silly_field", "changed db comment"
)

self.assertColumnCommentNotExists("migrations_tribble", "id")
self.assertColumnCommentNotExists("migrations_tribble", "fluffy")
self.assertColumnCommentEquals(
"migrations_tribble", "bool_field", "new db comment"
)

# Run migrations all the way
call_command("migrate", verbosity=0)
# The correct tables exist
self.assertColumnCommentNotExists("migrations_author", "silly_field")
self.assertColumnExists("migrations_author", "rating")
self.assertColumnCommentEquals(
"migrations_author", "rating", "new comment for rating Column"
)
self.assertColumnCommentEquals("migrations_book", "author_id", "this is FK")

# Unmigrate 0002
stdout = io.StringIO()
call_command(
"migrate", "migrations", "0001", verbosity=2, stdout=stdout, no_color=True
)
# The correct db comment
self.assertColumnCommentEquals(
"migrations_author", "name", "db comment to name Field"
)
self.assertColumnCommentNotExists("migrations_author", "slug")
self.assertColumnCommentEquals(
"migrations_author", "age", "db comment to age Field"
)
self.assertColumnCommentEquals(
"migrations_author", "silly_field", "changed db comment"
)

self.assertColumnCommentNotExists("migrations_tribble", "id")
self.assertColumnCommentNotExists("migrations_tribble", "fluffy")
self.assertColumnCommentEquals(
"migrations_tribble", "bool_field", "new db comment"
)
# Unmigrate everything
call_command("migrate", "migrations", "zero", verbosity=0)
call_command("migrate", "migrations", "zero", verbosity=0, database="other")

@override_settings(
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed_complex"}
)
Expand Down
56 changes: 0 additions & 56 deletions tests/migrations/test_migrations_db_comment/0001_initial.py

This file was deleted.

50 changes: 0 additions & 50 deletions tests/migrations/test_migrations_db_comment/0002_second.py

This file was deleted.

This file was deleted.

This file was deleted.

Empty file.
31 changes: 31 additions & 0 deletions tests/migrations/test_operations.py
Expand Up @@ -1922,6 +1922,37 @@ def test_alter_field_add_db_column_noop(self):
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", "db_table_comment": "Custom pony comment"}
)

def test_alter_field_pk(self):
"""
The AlterField operation on primary keys (things like PostgreSQL's
Expand Down

0 comments on commit 417288d

Please sign in to comment.