Skip to content

Commit

Permalink
Fixed #18468 testcase to cover SQL Comment
Browse files Browse the repository at this point in the history
  • Loading branch information
KimSoungRyoul committed Jun 13, 2021
1 parent 8192ba0 commit 1a1e4d5
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/backends/sqlite/test_features.py
Expand Up @@ -16,3 +16,6 @@ def test_supports_json_field_operational_error(self):
):
with self.assertRaisesMessage(OperationalError, msg):
connection.features.supports_json_field

def test_no_supports_db_comments(self):
self.assertFalse(connection.features.supports_db_comments)
40 changes: 40 additions & 0 deletions tests/migrations/test_commands.py
Expand Up @@ -3,6 +3,7 @@
import io
import os
import sys
import unittest
from unittest import mock

from django.apps import apps
Expand Down Expand Up @@ -542,6 +543,45 @@ def test_showmigrations_plan_no_migrations(self):
call_command('showmigrations', format='plan', stdout=out, verbosity=2, no_color=True)
self.assertEqual('(no migrations)\n', out.getvalue().lower())

@unittest.skipIf(connection.vendor == 'sqlite', 'sqlite does not support SQL comment')
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_db_comment"})
def test_migrate_db_comment(self):
"""
Tests basic usage of the migrate command.
"""
# No tables are created
self.assertTableNotExists("migrations_author")
self.assertTableNotExists("migrations_tribble")
self.assertTableNotExists("migrations_book")
# Run the migrations to 0001 only
stdout = io.StringIO()
call_command('migrate', 'migrations', '0001', verbosity=2, stdout=stdout, no_color=True)
stdout = stdout.getvalue()
self.assertIn('Add field bool_field to tribble', stdout)
self.assertIn('Alter field silly_field on author', stdout)
# The correct tables exist
self.assertTableExists("migrations_author")
self.assertTableExists("migrations_tribble")
self.assertTableNotExists("migrations_book")
# Run migrations all the way
call_command("migrate", verbosity=0)
# The correct tables exist
self.assertTableExists("migrations_author")
self.assertTableNotExists("migrations_tribble")
self.assertTableExists("migrations_book")
# Unmigrate everything
stdout = io.StringIO()
call_command('migrate', 'migrations', 'zero', verbosity=2, stdout=stdout, no_color=True)
stdout = stdout.getvalue()
self.assertIn('Unapply all migrations: migrations', stdout)
self.assertIn('Unapplying migrations.0002_second... OK', stdout)
self.assertIn('Running pre-migrate handlers for application migrations', stdout)
self.assertIn('Running post-migrate handlers for application migrations', stdout)
# Tables are gone
self.assertTableNotExists("migrations_author")
self.assertTableNotExists("migrations_tribble")
self.assertTableNotExists("migrations_book")

@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed_complex"})
def test_showmigrations_plan_squashed(self):
"""
Expand Down
41 changes: 41 additions & 0 deletions tests/migrations/test_migrations_db_comment/0001_initial.py
@@ -0,0 +1,41 @@
from django.db import migrations, models


class Migration(migrations.Migration):
initial = True

operations = [
migrations.CreateModel(
"Author",
[
("id", models.AutoField(primary_key=True)),
("name", models.CharField(max_length=255, db_comment='db comment to name Field')),
("slug", models.SlugField(null=True)),
("age", models.IntegerField(default=0, db_comment='db comment to age Field')),
("silly_field", models.BooleanField(default=False, db_comment='db comment to bool Field')),
],
options={
"db_table_comment": "I am table Comment",
}
),
migrations.CreateModel(
"Tribble",
[
("id", models.AutoField(primary_key=True)),
("fluffy", models.BooleanField(default=True)),
],
options={
"db_table_comment": "I am tribble table Comment",
}
),
migrations.AddField(
model_name='tribble',
name='bool_field',
field=models.BooleanField(default=False, db_comment='new db comment'),
),
migrations.AlterField(
model_name='author',
name='silly_field',
field=models.BooleanField(default=True, db_comment='changed db comment')
),
]
29 changes: 29 additions & 0 deletions tests/migrations/test_migrations_db_comment/0002_second.py
@@ -0,0 +1,29 @@
from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("migrations", "0001_initial"),
]

operations = [

migrations.DeleteModel("Tribble"),

migrations.RemoveField("Author", "silly_field"),

migrations.AddField("Author", "rating",
models.IntegerField(default=0, db_comment='new comment for rating Column')),

migrations.CreateModel(
"Book",
[
("id", models.AutoField(primary_key=True)),
(
"author",
models.ForeignKey("migrations.Author", models.SET_NULL, db_comment='this is FK', null=True)
),
],
)

]
Empty file.

0 comments on commit 1a1e4d5

Please sign in to comment.