Skip to content

Commit

Permalink
Properly deconstruct custom operations.
Browse files Browse the repository at this point in the history
Operation.deconstruct is actually expected to return only the operation name
which contradicts the documented deconstruct behavior.

Regression introduced by 5d29c71.
  • Loading branch information
charettes committed Apr 29, 2021
1 parent 9bc4f71 commit 6f289f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
12 changes: 0 additions & 12 deletions syzygy/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,6 @@ def describe(self):
)
return "Set field %s of %s NULLable" % (self.name, self.model_name)

def deconstruct(self):
_, args, kwargs = super().deconstruct()
return f"{__name__}.{self.__class__.__name__}", args, kwargs


class AddField(migrations.AddField):
"""
Expand Down Expand Up @@ -170,10 +166,6 @@ def database_forwards(self, app_label, schema_editor, from_state, to_state):
app_label, schema_editor, from_state, to_state
)

def deconstruct(self):
_, args, kwargs = super().deconstruct()
return f"{__name__}.{self.__class__.__name__}", args, kwargs


class PostAddField(migrations.AlterField):
"""
Expand Down Expand Up @@ -224,7 +216,3 @@ def describe(self):
self.name,
self.model_name,
)

def deconstruct(self):
_, args, kwargs = super().deconstruct()
return f"{__name__}.{self.__class__.__name__}", args, kwargs
23 changes: 18 additions & 5 deletions tests/test_operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db import connection, migrations, models
from django.db.migrations.operations.base import Operation
from django.db.migrations.optimizer import MigrationOptimizer
from django.db.migrations.serializer import OperationSerializer
from django.db.migrations.state import ProjectState
from django.db.models.fields import NOT_PROVIDED
from django.test import TestCase
Expand Down Expand Up @@ -85,11 +86,15 @@ def test_deconstruct(self):
self.assertEqual(
operation.deconstruct(),
(
"syzygy.operations.AddField",
"AddField",
[],
{"model_name": model_name, "name": field_name, "field": field},
),
)
serializer = OperationSerializer(operation)
serialized, imports = serializer.serialize()
self.assertTrue(serialized.startswith("syzygy.operations.AddField"))
self.assertIn("import syzygy.operations", imports)


class PostAddFieldTests(OperationTestCase):
Expand Down Expand Up @@ -185,11 +190,15 @@ def test_deconstruct(self):
self.assertEqual(
operation.deconstruct(),
(
"syzygy.operations.PostAddField",
"PostAddField",
[],
{"model_name": model_name, "name": field_name, "field": field},
),
)
serializer = OperationSerializer(operation)
serialized, imports = serializer.serialize()
self.assertTrue(serialized.startswith("syzygy.operations.PostAddField"))
self.assertIn("import syzygy.operations", imports)

def test_reduce(self):
model_name = "TestModel"
Expand Down Expand Up @@ -276,19 +285,23 @@ def test_deconstruct(self):
model_name = "TestModel"
field_name = "foo"
field = models.IntegerField(default=42)
operations = PreRemoveField(
operation = PreRemoveField(
model_name,
field_name,
field,
)
self.assertEqual(
operations.deconstruct(),
operation.deconstruct(),
(
"syzygy.operations.PreRemoveField",
"PreRemoveField",
[],
{"model_name": model_name, "name": field_name, "field": field},
),
)
serializer = OperationSerializer(operation)
serialized, imports = serializer.serialize()
self.assertTrue(serialized.startswith("syzygy.operations.PreRemoveField"))
self.assertIn("import syzygy.operations", imports)

def test_elidable(self):
model_name = "TestModel"
Expand Down

0 comments on commit 6f289f1

Please sign in to comment.