Made assertOptimizesTo() more strict in migrations tests.

A recursive deconstruction is needed because comparing deconstructed
operations doesn't check field attributes. Fixed typo in failing test.
This commit is contained in:
Vytis Banaitis 2019-01-22 00:29:46 +02:00 committed by Tim Graham
parent 8d2dcc47cd
commit d02b2aa11e
1 changed files with 7 additions and 3 deletions

View File

@ -1,6 +1,7 @@
from django.db import migrations, models
from django.db.migrations import operations
from django.db.migrations.optimizer import MigrationOptimizer
from django.db.migrations.serializer import serializer_factory
from django.test import SimpleTestCase
from .models import EmptyManager, UnicodeModel
@ -18,10 +19,13 @@ class OptimizerTests(SimpleTestCase):
optimizer = MigrationOptimizer()
return optimizer.optimize(operations, app_label), optimizer._iterations
def serialize(self, value):
return serializer_factory(value).serialize()[0]
def assertOptimizesTo(self, operations, expected, exact=None, less_than=None, app_label=None):
result, iterations = self.optimize(operations, app_label)
result = [repr(f.deconstruct()) for f in result]
expected = [repr(f.deconstruct()) for f in expected]
result = [self.serialize(f) for f in result]
expected = [self.serialize(f) for f in expected]
self.assertEqual(expected, result)
if exact is not None and iterations != exact:
raise self.failureException(
@ -360,7 +364,7 @@ class OptimizerTests(SimpleTestCase):
('url', models.TextField()),
('foo_fk', models.ForeignKey('migrations.Foo', models.CASCADE)),
]),
migrations.AddField('Foo', 'bar_fk', models.ForeignKey('migrations.Foo', models.CASCADE)),
migrations.AddField('Foo', 'bar_fk', models.ForeignKey('migrations.Bar', models.CASCADE)),
],
)