Add (failing) tests cases for makeing a column not null with a Django default value
This commit is contained in:
1 parent
d411ea2f6b
commit
f21ff2c278
16 files changed
+174
No files matched your search
@@ -11,3 +11,5 @@ ALTER_COLUMN_DROP_NOT_NULL = "app_alter_column_drop_not_null"
|
|||||||
DROP_UNIQUE_TOGETHER = "app_unique_together"
|
DROP_UNIQUE_TOGETHER = "app_unique_together"
|
||||||
ADD_MANYTOMANY_FIELD = "app_add_manytomany_field"
|
ADD_MANYTOMANY_FIELD = "app_add_manytomany_field"
|
||||||
DATA_MIGRATIONS = "app_data_migrations"
|
DATA_MIGRATIONS = "app_data_migrations"
|
||||||
|
MAKE_NOT_NULL_WITH_DJANGO_DEFAULT = "app_make_not_null_with_django_default"
|
||||||
|
MAKE_NOT_NULL_WITH_LIB_DEFAULT = "app_make_not_null_with_lib_default"
|
||||||
@@ -44,6 +44,14 @@ class BaseBackwardCompatibilityDetection(object):
|
|||||||
app = fixtures.ADD_NOT_NULL_COLUMN
|
app = fixtures.ADD_NOT_NULL_COLUMN
|
||||||
self._test_linter_finds_errors(app)
|
self._test_linter_finds_errors(app)
|
||||||
|
|
||||||
|
def test_detect_make_column_not_null_with_django_default(self):
|
||||||
|
app = fixtures.MAKE_NOT_NULL_WITH_DJANGO_DEFAULT
|
||||||
|
self._test_linter_finds_errors(app)
|
||||||
|
|
||||||
|
def test_detect_make_column_not_null_with_lib_default(self):
|
||||||
|
app = fixtures.MAKE_NOT_NULL_WITH_LIB_DEFAULT
|
||||||
|
self._test_linter_finds_no_errors(app)
|
||||||
|
|
||||||
def test_detect_drop_column(self):
|
def test_detect_drop_column(self):
|
||||||
app = fixtures.DROP_COLUMN
|
app = fixtures.DROP_COLUMN
|
||||||
self._test_linter_finds_errors(app)
|
self._test_linter_finds_errors(app)
|
||||||
@@ -97,6 +105,15 @@ class SqliteBackwardCompatibilityDetectionTestCase(
|
|||||||
app = fixtures.ADD_NOT_NULL_COLUMN_FOLLOWED_BY_DEFAULT
|
app = fixtures.ADD_NOT_NULL_COLUMN_FOLLOWED_BY_DEFAULT
|
||||||
self._test_linter_finds_errors(app)
|
self._test_linter_finds_errors(app)
|
||||||
|
|
||||||
|
def test_detect_make_column_not_null_with_django_default(self):
|
||||||
|
app = fixtures.MAKE_NOT_NULL_WITH_DJANGO_DEFAULT
|
||||||
|
self._test_linter_finds_errors(app)
|
||||||
|
|
||||||
|
def test_detect_make_column_not_null_with_lib_default(self):
|
||||||
|
# The 'django-add-default-value' doesn't handle sqlite correctly
|
||||||
|
app = fixtures.MAKE_NOT_NULL_WITH_LIB_DEFAULT
|
||||||
|
self._test_linter_finds_errors(app)
|
||||||
|
|
||||||
|
|
||||||
class MySqlBackwardCompatibilityDetectionTestCase(
|
class MySqlBackwardCompatibilityDetectionTestCase(
|
||||||
BaseBackwardCompatibilityDetection, unittest.TestCase
|
BaseBackwardCompatibilityDetection, unittest.TestCase
|
||||||
|
|||||||
Whitespace-only changes.
@@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class AppMakeNotNullWithDjangoDefaultConfig(AppConfig):
|
||||||
|
name = "app_make_not_null_with_django_default"
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# Generated by Django 2.2 on 2020-06-20 15:17
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = []
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="A",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.AutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("col", models.CharField(max_length=10, null=True)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
+18
@@ -0,0 +1,18 @@
|
|||||||
|
# Generated by Django 2.2 on 2020-06-20 15:17
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("app_make_not_null_with_django_default", "0001_initial"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="a",
|
||||||
|
name="col",
|
||||||
|
field=models.CharField(default="empty", max_length=10),
|
||||||
|
),
|
||||||
|
]
|
||||||
Whitespace-only changes.
@@ -0,0 +1,5 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class A(models.Model):
|
||||||
|
col = models.CharField(max_length=10, null=False, default="empty")
|
||||||
Whitespace-only changes.
@@ -0,0 +1,5 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class AppMakeNotNullWithLibDefaultConfig(AppConfig):
|
||||||
|
name = "app_make_not_null_with_lib_default"
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
# Generated by Django 2.2 on 2020-06-20 15:23
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = []
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name="A",
|
||||||
|
fields=[
|
||||||
|
(
|
||||||
|
"id",
|
||||||
|
models.AutoField(
|
||||||
|
auto_created=True,
|
||||||
|
primary_key=True,
|
||||||
|
serialize=False,
|
||||||
|
verbose_name="ID",
|
||||||
|
),
|
||||||
|
),
|
||||||
|
("col", models.CharField(max_length=10, null=True)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
+21
@@ -0,0 +1,21 @@
|
|||||||
|
# Generated by Django 2.2 on 2020-06-20 15:23
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
from django_add_default_value import AddDefaultValue
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
("app_make_not_null_with_lib_default", "0001_initial"),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name="a",
|
||||||
|
name="col",
|
||||||
|
field=models.CharField(default="avalue", max_length=10),
|
||||||
|
),
|
||||||
|
AddDefaultValue(model_name="a", name="col", value="avalue"),
|
||||||
|
]
|
||||||
Whitespace-only changes.
@@ -0,0 +1,5 @@
|
|||||||
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
|
class A(models.Model):
|
||||||
|
col = models.CharField(max_length=10, null=False, default="avalue")
|
||||||
@@ -54,6 +54,8 @@ INSTALLED_APPS = [
|
|||||||
"tests.test_project.app_rename_table",
|
"tests.test_project.app_rename_table",
|
||||||
"tests.test_project.app_unique_together",
|
"tests.test_project.app_unique_together",
|
||||||
"tests.test_project.app_data_migrations",
|
"tests.test_project.app_data_migrations",
|
||||||
|
"tests.test_project.app_make_not_null_with_django_default",
|
||||||
|
"tests.test_project.app_make_not_null_with_lib_default",
|
||||||
]
|
]
|
||||||
|
|
||||||
MIDDLEWARE = [
|
MIDDLEWARE = [
|
||||||
|
|||||||
@@ -64,6 +64,25 @@ class MySqlAnalyserTestCase(SqlAnalyserTestCase):
|
|||||||
]
|
]
|
||||||
self.assertValidSql(sql)
|
self.assertValidSql(sql)
|
||||||
|
|
||||||
|
def test_make_column_not_null_with_django_default(self):
|
||||||
|
sql = [
|
||||||
|
"ALTER TABLE `app_drop_default_a` ALTER COLUMN `col` SET DEFAULT 'empty';",
|
||||||
|
"UPDATE `app_drop_default_a` SET `col` = 'empty' WHERE `col` IS NULL;",
|
||||||
|
"ALTER TABLE `app_drop_default_a` MODIFY `col` varchar(10) NOT NULL;",
|
||||||
|
"ALTER TABLE `app_drop_default_a` ALTER COLUMN `col` DROP DEFAULT;",
|
||||||
|
]
|
||||||
|
self.assertBackwardIncompatibleSql(sql)
|
||||||
|
|
||||||
|
def test_make_column_not_null_with_lib_default(self):
|
||||||
|
sql = [
|
||||||
|
"ALTER TABLE `app_drop_default_a` ALTER COLUMN `col` SET DEFAULT 'empty';",
|
||||||
|
"UPDATE `app_drop_default_a` SET `col` = 'empty' WHERE `col` IS NULL;",
|
||||||
|
"ALTER TABLE `app_drop_default_a` MODIFY `col` varchar(10) NOT NULL;",
|
||||||
|
"ALTER TABLE `app_drop_default_a` ALTER COLUMN `col` DROP DEFAULT;",
|
||||||
|
"ALTER TABLE `app_drop_default_a` ALTER COLUMN `col` SET DEFAULT 'empty';",
|
||||||
|
]
|
||||||
|
self.assertValidSql(sql)
|
||||||
|
|
||||||
|
|
||||||
class SqliteAnalyserTestCase(SqlAnalyserTestCase):
|
class SqliteAnalyserTestCase(SqlAnalyserTestCase):
|
||||||
database_vendor = "sqlite"
|
database_vendor = "sqlite"
|
||||||
@@ -165,3 +184,22 @@ class PostgresqlAnalyserTestCase(SqlAnalyserTestCase):
|
|||||||
'CREATE INDEX "app_add_manytomany_field_b_many_to_many_a_id_4b44832a" ON "app_add_manytomany_field_b_many_to_many"("a_id");',
|
'CREATE INDEX "app_add_manytomany_field_b_many_to_many_a_id_4b44832a" ON "app_add_manytomany_field_b_many_to_many"("a_id");',
|
||||||
]
|
]
|
||||||
self.assertValidSql(sql)
|
self.assertValidSql(sql)
|
||||||
|
|
||||||
|
def test_make_column_not_null_with_django_default(self):
|
||||||
|
sql = [
|
||||||
|
'ALTER TABLE "app_drop_default_a" ALTER COLUMN "col" SET DEFAULT \'empty\';',
|
||||||
|
'UPDATE "app_drop_default_a" SET "col" = \'empty\' WHERE "col" IS NULL;',
|
||||||
|
'ALTER TABLE "app_drop_default_a" ALTER COLUMN "col" SET NOT NULL;',
|
||||||
|
'ALTER TABLE "app_drop_default_a" ALTER COLUMN "col" DROP DEFAULT;',
|
||||||
|
]
|
||||||
|
self.assertBackwardIncompatibleSql(sql)
|
||||||
|
|
||||||
|
def test_make_column_not_null_with_lib_default(self):
|
||||||
|
sql = [
|
||||||
|
'ALTER TABLE "app_drop_default_a" ALTER COLUMN "col" SET DEFAULT \'empty\';',
|
||||||
|
'UPDATE "app_drop_default_a" SET "col" = \'empty\' WHERE "col" IS NULL;',
|
||||||
|
'ALTER TABLE "app_drop_default_a" ALTER COLUMN "col" SET NOT NULL;',
|
||||||
|
'ALTER TABLE "app_drop_default_a" ALTER COLUMN "col" DROP DEFAULT;',
|
||||||
|
'ALTER TABLE "app_drop_default_a" ALTER COLUMN "col" SET DEFAULT `\'empty\';',
|
||||||
|
]
|
||||||
|
self.assertValidSql(sql)
|
||||||
Reference in new issue
Block a user