0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Avoid setUpTestData in streamfield migration tests

Setting a StreamField RawDataView as a class attribute of a test case is not valid in Django 4.1, as it doesn't support deep copying. Convert these to setUp methods, so that they're recreated for each test invocation.
This commit is contained in:
Matt Westcott 2022-12-07 17:33:00 +00:00 committed by Matt Westcott
parent 440427f8a7
commit aa3ab79384
4 changed files with 70 additions and 90 deletions

View File

@ -26,8 +26,7 @@ class TestExceptionRaisedInRawData(TestCase):
applied. (There should also be a block in the stream data with the said name for this to happen)
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1="nestedstruct",
@ -49,7 +48,7 @@ class TestExceptionRaisedInRawData(TestCase):
raw_data[1]["value"]["invalid_name2"] = [
{"type": "char1", "value": "foo", "id": "0003"}
]
cls.raw_data = raw_data
self.raw_data = raw_data
def test_rename_invalid_stream_child(self):
"""Test whether Exception is raised in when recursing through stream block data"""
@ -92,17 +91,15 @@ class BadDataMigrationTestCase(TestCase, MigrationTestMixin):
]
app_name = "streamfield_migration_tests"
@classmethod
def create_instance(cls):
def create_instance(self):
instance = factories.SamplePageFactory(
content__0__char1__value="Char Block 1",
content__1="nestedstruct",
)
cls.instance = instance
self.instance = instance
@classmethod
def append_invalid_instance_data(cls):
raw_data = cls.instance.content.raw_data
def append_invalid_instance_data(self):
raw_data = self.instance.content.raw_data
raw_data.extend(
[
{
@ -117,31 +114,29 @@ class BadDataMigrationTestCase(TestCase, MigrationTestMixin):
},
]
)
stream_block = cls.instance.content.stream_block
cls.instance.content = StreamValue(
stream_block = self.instance.content.stream_block
self.instance.content = StreamValue(
stream_block=stream_block, stream_data=raw_data, is_lazy=True
)
cls.instance.save()
self.instance.save()
@classmethod
def create_invalid_revision(cls, delta):
cls.append_invalid_instance_data()
invalid_revision = cls.create_revision(delta)
def create_invalid_revision(self, delta):
self.append_invalid_instance_data()
invalid_revision = self.create_revision(delta)
# remove the invalid data from the instance
raw_data = cls.instance.content.raw_data
raw_data = self.instance.content.raw_data
raw_data = raw_data[:2]
stream_block = cls.instance.content.stream_block
cls.instance.content = StreamValue(
stream_block = self.instance.content.stream_block
self.instance.content = StreamValue(
stream_block=stream_block, stream_data=raw_data, is_lazy=True
)
cls.instance.save()
self.instance.save()
return invalid_revision.id, invalid_revision.created_at
@classmethod
def create_revision(cls, delta):
revision = cls.instance.save_revision()
def create_revision(self, delta):
revision = self.instance.save_revision()
revision.created_at = timezone.now() - datetime.timedelta(days=(delta))
revision.save()
return revision
@ -151,11 +146,10 @@ class TestExceptionRaisedForInstance(BadDataMigrationTestCase):
"""Exception should always be raised when applying migration if it occurs while migrating the
instance data"""
@classmethod
def setUpTestData(cls):
def setUp(self):
with disable_reference_index_auto_update():
cls.create_instance()
cls.append_invalid_instance_data()
self.create_instance()
self.append_invalid_instance_data()
def test_migrate(self):
@ -174,18 +168,17 @@ class TestExceptionRaisedForLatestRevision(BadDataMigrationTestCase):
"""Exception should always be raised when applying migration if it occurs while migrating the
latest revision data"""
@classmethod
def setUpTestData(cls):
def setUp(self):
with disable_reference_index_auto_update():
cls.create_instance()
self.create_instance()
for i in range(4):
cls.create_revision(5 - i)
self.create_revision(5 - i)
(
cls.invalid_revision_id,
cls.invalid_revision_created_at,
) = cls.create_invalid_revision(0)
self.invalid_revision_id,
self.invalid_revision_created_at,
) = self.create_invalid_revision(0)
def test_migrate(self):
with self.assertRaisesMessage(
@ -204,20 +197,19 @@ class TestExceptionRaisedForLiveRevision(BadDataMigrationTestCase):
"""Exception should always be raised when applying migration if it occurs while migrating the
live revision data"""
@classmethod
def setUpTestData(cls):
def setUp(self):
with disable_reference_index_auto_update():
cls.create_instance()
self.create_instance()
(
cls.invalid_revision_id,
cls.invalid_revision_created_at,
) = cls.create_invalid_revision(5)
cls.instance.live_revision_id = cls.invalid_revision_id
cls.instance.save()
self.invalid_revision_id,
self.invalid_revision_created_at,
) = self.create_invalid_revision(5)
self.instance.live_revision_id = self.invalid_revision_id
self.instance.save()
for i in range(1, 5):
cls.create_revision(5 - i)
self.create_revision(5 - i)
def test_migrate(self):
with self.assertRaisesMessage(
@ -238,17 +230,16 @@ class TestExceptionIgnoredForOtherRevisions(BadDataMigrationTestCase):
model = models.SamplePage
@classmethod
def setUpTestData(cls):
def setUp(self):
with disable_reference_index_auto_update():
cls.create_instance()
self.create_instance()
(
cls.invalid_revision_id,
cls.invalid_revision_created_at,
) = cls.create_invalid_revision(5)
self.invalid_revision_id,
self.invalid_revision_created_at,
) = self.create_invalid_revision(5)
for i in range(1, 5):
cls.create_revision(5 - i)
self.create_revision(5 - i)
def test_migrate(self):
with self.assertLogs(level="ERROR") as cm:

View File

@ -24,11 +24,10 @@ class BaseMigrationTest(TestCase, MigrationTestMixin):
]
app_name = None
@classmethod
def setUpTestData(cls):
def setUp(self):
instances = []
instances.append(
cls.factory(
self.factory(
content__0__char1="Test char 1",
content__1__char1="Test char 2",
content__2__char2="Test char 3",
@ -36,27 +35,27 @@ class BaseMigrationTest(TestCase, MigrationTestMixin):
)
)
instances.append(
cls.factory(
self.factory(
content__0__char1="Test char 1",
content__1__char1="Test char 2",
content__2__char2="Test char 3",
)
)
instances.append(
cls.factory(
self.factory(
content__0__char2="Test char 1",
content__1__char2="Test char 2",
content__2__char2="Test char 3",
)
)
cls.original_raw_data = {}
cls.original_revisions = {}
self.original_raw_data = {}
self.original_revisions = {}
for instance in instances:
cls.original_raw_data[instance.id] = instance.content.raw_data
self.original_raw_data[instance.id] = instance.content.raw_data
if cls.has_revisions:
if self.has_revisions:
for i in range(5):
revision = instance.save_revision()
revision.created_at = timezone.now() - datetime.timedelta(
@ -66,7 +65,7 @@ class BaseMigrationTest(TestCase, MigrationTestMixin):
if i == 1:
instance.live_revision = revision
instance.save()
cls.original_revisions[instance.id] = list(
self.original_revisions[instance.id] = list(
instance.revisions.all().order_by("id")
)

View File

@ -16,8 +16,7 @@ class FieldStructStreamChildBlockTest(TestCase):
We use `nestedstruct.simplestream` blocks here.
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1="nestedstruct",
@ -32,7 +31,7 @@ class FieldStructStreamChildBlockTest(TestCase):
content__3__simplestream__0__char1__value="Char Block 1",
content__3__simplestream__1__char2__value="Char Block 2",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_blocks_and_data_not_operated_on_intact(self):
"""Test whether other blocks and data not passed to an operation are intact.
@ -152,8 +151,7 @@ class FieldStructStructChildBlockTest(TestCase):
We use `nestedstruct.simplestruct` blocks here
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1="nestedstruct",
@ -162,7 +160,7 @@ class FieldStructStructChildBlockTest(TestCase):
content__2__nestedstruct__list1__0__value="a",
content__3="simplestruct",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_blocks_and_data_not_operated_on_intact(self):
"""Test whether other blocks and data not passed to an operation are intact.
@ -257,8 +255,7 @@ class FieldStreamStreamChildBlockTest(TestCase):
We use `nestedstream.stream1` blocks here.
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1="nestedstream",
@ -275,7 +272,7 @@ class FieldStreamStreamChildBlockTest(TestCase):
content__3="simplestream",
content__3__simplestream__0__char1__value="Char Block 1",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_blocks_and_data_not_operated_on_intact(self):
"""Test whether other blocks and data not passed to an operation are intact.
@ -397,8 +394,7 @@ class FieldStreamStructChildBlockTest(TestCase):
We use `nestedstream.simplestruct` blocks here.
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1="nestedstream",
@ -410,7 +406,7 @@ class FieldStreamStructChildBlockTest(TestCase):
content__3="simplestream",
content__3__simplestream__0__char1__value="Char Block 1",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_blocks_and_data_not_operated_on_intact(self):
"""Test whether other blocks and data not passed to an operation are intact.
@ -547,8 +543,7 @@ class FieldListStreamChildBlockTest(TestCase):
We use `nestedlist_stream.item` blocks here.
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1="nestedlist_stream",
@ -562,7 +557,7 @@ class FieldListStreamChildBlockTest(TestCase):
content__3__simplestream__0__char1__value="Char Block 1",
content__3__simplestream__1__char2__value="Char Block 2",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_blocks_and_data_not_operated_on_intact(self):
"""Test whether other blocks and data not passed to an operation are intact.
@ -697,8 +692,7 @@ class FieldListStructChildBlockTest(TestCase):
We use `nestedlist_struct.item` blocks here.
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1__nestedlist_struct__0__char1="Nested List Struct 1",
@ -706,7 +700,7 @@ class FieldListStructChildBlockTest(TestCase):
content__2__nestedlist_struct__0__char1="Nested List Struct 3",
content__3="simplestruct",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_blocks_and_data_not_operated_on_intact(self):
"""Test whether other blocks and data not passed to an operation are intact.

View File

@ -18,15 +18,14 @@ from wagtail.test.streamfield_migrations import factories, models
class FieldChildBlockTest(TestCase):
"""Tests involving changes to top level blocks"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1__char2__value="Char Block 2",
content__2__char1__value="Char Block 1",
content__3__char2__value="Char Block 2",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_rename(self):
"""Rename `char1` blocks to `renamed1`
@ -228,15 +227,14 @@ class FieldStructChildBlockTest(TestCase):
We use `simplestruct` blocks as the StructBlocks here.
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1="simplestruct",
content__2="simplestruct",
content__3__char2__value="Char Block 2",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_blocks_and_data_not_operated_on_intact(self):
"""Test whether other blocks and data not passed to an operation are intact.
@ -338,8 +336,7 @@ class FieldStreamChildBlockTest(TestCase):
We use `simplestream` blocks as the StreamBlocks here.
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1="simplestream",
@ -350,7 +347,7 @@ class FieldStreamChildBlockTest(TestCase):
content__2__simplestream__0__char1__value="Char Block 1",
content__3__char2__value="Char Block 2",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_blocks_and_data_not_operated_on_intact(self):
"""Test whether other blocks and data not passed to an operation are intact.
@ -447,15 +444,14 @@ class FieldListChildBlockTest(TestCase):
We use `simplelist` blocks as the ListBlocks here.
"""
@classmethod
def setUpTestData(cls):
def setUp(self):
raw_data = factories.SampleModelFactory(
content__0__char1__value="Char Block 1",
content__1__simplelist__0="Foo 1",
content__1__simplelist__1="Foo 2",
content__2__simplelist__0="Foo 3",
).content.raw_data
cls.raw_data = raw_data
self.raw_data = raw_data
def test_to_structblock(self):
"""Turn each list child into a StructBlock and move value inside as a child named `text`