Skip to content

Commit 0cda60b

Browse files
author
Andy Babic
committed
Always use lists of dicts for body values
1 parent 099efef commit 0cda60b

File tree

6 files changed

+48
-76
lines changed

6 files changed

+48
-76
lines changed

tests/test_migration_outcomes.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from django.test import TestCase
2-
from testapp.constants import COMPLEX_MODIFIED_BODY_VALUE, COMPLEX_ORIGINAL_BODY_VALUE
2+
from testapp.constants import ORIGINAL_BODY_VALUE, MODIFIED_BODY_VALUE
33
from testapp.models import TestPage, TestSnippet
44

55

@@ -21,14 +21,16 @@ def test_original_pages_and_snippets_have_retained_their_original_body_values(se
2121
- Updating their title in `0008_modify_title_values_with_bulk_update` via a bulk update
2222
"""
2323
self.assertEqual(self.page_1.title, "Modified Test Page")
24+
self.assertEqual(len(self.page_1.body.raw_data), 3)
2425
for i, block in enumerate(self.page_1.body.raw_data):
25-
compare_to = COMPLEX_ORIGINAL_BODY_VALUE[i]
26+
compare_to = ORIGINAL_BODY_VALUE[i]
2627
self.assertEqual(block["type"], compare_to["type"])
2728
self.assertEqual(block["value"], compare_to["value"])
2829

2930
self.assertEqual(self.snippet_1.title, "Modified Test Snippet")
31+
self.assertEqual(len(self.snippet_1.body.raw_data), 3)
3032
for i, block in enumerate(self.snippet_1.body.raw_data):
31-
compare_to = COMPLEX_ORIGINAL_BODY_VALUE[i]
33+
compare_to = ORIGINAL_BODY_VALUE[i]
3234
self.assertEqual(block["type"], compare_to["type"])
3335
self.assertEqual(block["value"], compare_to["value"])
3436

@@ -45,14 +47,16 @@ def test_modified_original_pages_and_snippets_have_retained_their_modified_body_
4547
test_snippet = self.snippet_2
4648

4749
self.assertEqual(test_page.title, "Modified Test Page Deux")
50+
self.assertEqual(len(test_page.body.raw_data), 3)
4851
for i, block in enumerate(test_page.body.raw_data):
49-
compare_to = COMPLEX_MODIFIED_BODY_VALUE[i]
52+
compare_to = MODIFIED_BODY_VALUE[i]
5053
self.assertEqual(block["type"], compare_to["type"])
5154
self.assertEqual(block["value"], compare_to["value"])
5255

5356
self.assertEqual(test_snippet.title, "Modified Test Snippet Deux")
57+
self.assertEqual(len(test_snippet.body.raw_data), 3)
5458
for i, block in enumerate(test_snippet.body.raw_data):
55-
compare_to = COMPLEX_MODIFIED_BODY_VALUE[i]
59+
compare_to = MODIFIED_BODY_VALUE[i]
5660
self.assertEqual(block["type"], compare_to["type"])
5761
self.assertEqual(block["value"], compare_to["value"])
5862

@@ -68,14 +72,15 @@ def test_newer_pages_and_snippets_have_retained_their_original_body_values(self)
6872
test_snippet = self.snippet_3
6973

7074
self.assertEqual(test_page.title, "Bulk Modified Test Page Tres")
75+
self.assertEqual(len(test_page.body.raw_data), 3)
7176
for i, block in enumerate(test_page.body.raw_data):
72-
compare_to = COMPLEX_ORIGINAL_BODY_VALUE[i]
77+
compare_to = ORIGINAL_BODY_VALUE[i]
7378
self.assertEqual(block["type"], compare_to["type"])
7479
self.assertEqual(block["value"], compare_to["value"])
7580

7681
self.assertEqual(test_snippet.title, "Bulk Modified Test Snippet Tres")
7782
for i, block in enumerate(test_snippet.body.raw_data):
78-
compare_to = COMPLEX_ORIGINAL_BODY_VALUE[i]
83+
compare_to = ORIGINAL_BODY_VALUE[i]
7984
self.assertEqual(block["type"], compare_to["type"])
8085
self.assertEqual(block["value"], compare_to["value"])
8186

@@ -94,13 +99,15 @@ def test_modified_newer_pages_and_snippets_have_retained_their_modified_body_val
9499
test_snippet = self.snippet_4
95100

96101
self.assertEqual(test_page.title, "Bulk Modified Test Page Cuatro")
102+
self.assertEqual(len(test_page.body.raw_data), 3)
97103
for i, block in enumerate(test_page.body.raw_data):
98-
compare_to = COMPLEX_MODIFIED_BODY_VALUE[i]
104+
compare_to = MODIFIED_BODY_VALUE[i]
99105
self.assertEqual(block["type"], compare_to["type"])
100106
self.assertEqual(block["value"], compare_to["value"])
101107

102108
self.assertEqual(test_snippet.title, "Bulk Modified Test Snippet Cuatro")
109+
self.assertEqual(len(test_snippet.body.raw_data), 3)
103110
for i, block in enumerate(test_snippet.body.raw_data):
104-
compare_to = COMPLEX_MODIFIED_BODY_VALUE[i]
111+
compare_to = MODIFIED_BODY_VALUE[i]
105112
self.assertEqual(block["type"], compare_to["type"])
106113
self.assertEqual(block["value"], compare_to["value"])

tests/testapp/constants.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,11 @@
1-
from .utils import convert_simple_streamfield_value_to_dicts
2-
3-
4-
SIMPLE_ORIGINAL_BODY_VALUE = [
5-
("text", "Hello World"),
6-
("integer", "123"),
7-
("date", "2024-12-25"),
1+
ORIGINAL_BODY_VALUE = [
2+
{"type": "text", "value": "Hello World!", "id": "813c03ab-e647-4bb0-9c7a-b3c2cb8ea10c"},
3+
{"type": "integer", "value": "123", "id": "e1e72d7d-ce60-45dd-bec7-5a229c2bfb67"},
4+
{"type": "date", "value": "2024-12-25", "id": "550719d5-8fb8-4233-a5ea-51a757708c92"},
85
]
96

10-
COMPLEX_ORIGINAL_BODY_VALUE = convert_simple_streamfield_value_to_dicts(
11-
SIMPLE_ORIGINAL_BODY_VALUE, add_ids=True
12-
)
13-
14-
15-
SIMPLE_MODIFIED_BODY_VALUE = [
16-
("text", "Goodbye Galaxy!"),
17-
("integer", "321"),
18-
("date", "3024-12-25"),
7+
MODIFIED_BODY_VALUE = [
8+
{"type": "text", "value": "Goodbye Galaxy!", "id": "813c03ab-e647-4bb0-9c7a-b3c2cb8ea10c"},
9+
{"type": "integer", "value": "321", "id": "e1e72d7d-ce60-45dd-bec7-5a229c2bfb67"},
10+
{"type": "date", "value": "3024-12-25", "id": "550719d5-8fb8-4233-a5ea-51a757708c92"},
1911
]
20-
21-
COMPLEX_MODIFIED_BODY_VALUE = convert_simple_streamfield_value_to_dicts(
22-
SIMPLE_MODIFIED_BODY_VALUE
23-
)

tests/testapp/migrations/0002_create_test_objects.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
from django.db import migrations
22

3-
from tests.testapp.constants import (
4-
COMPLEX_ORIGINAL_BODY_VALUE,
5-
SIMPLE_ORIGINAL_BODY_VALUE,
6-
)
3+
from tests.testapp.constants import ORIGINAL_BODY_VALUE
74

85

96
"""
@@ -42,7 +39,7 @@ def migrate_forwards(apps, schema_editor):
4239
draft_title="Test Page",
4340
slug="test-page",
4441
content_type=content_type,
45-
body=SIMPLE_ORIGINAL_BODY_VALUE,
42+
body=ORIGINAL_BODY_VALUE,
4643
depth=3,
4744
locale_id=1,
4845
path="000100010001",
@@ -55,7 +52,7 @@ def migrate_forwards(apps, schema_editor):
5552
draft_title="Test Page Deux",
5653
slug="test-page-2",
5754
content_type=content_type,
58-
body=COMPLEX_ORIGINAL_BODY_VALUE,
55+
body=ORIGINAL_BODY_VALUE,
5956
depth=3,
6057
locale_id=1,
6158
path="000100010002",
@@ -66,9 +63,9 @@ def migrate_forwards(apps, schema_editor):
6663
Page.objects.filter(depth=2).update(numchild=2)
6764

6865
# Add two test snippet
69-
TestSnippet.objects.create(title="Test Snippet", body=SIMPLE_ORIGINAL_BODY_VALUE)
66+
TestSnippet.objects.create(title="Test Snippet", body=ORIGINAL_BODY_VALUE)
7067
TestSnippet.objects.create(
71-
title="Test Snippet Deux", body=COMPLEX_ORIGINAL_BODY_VALUE
68+
title="Test Snippet Deux", body=ORIGINAL_BODY_VALUE
7269
)
7370

7471

tests/testapp/migrations/0004_modify_body_values.py

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
from django.db import migrations
44

5-
from tests.testapp.constants import (
6-
COMPLEX_MODIFIED_BODY_VALUE,
7-
COMPLEX_ORIGINAL_BODY_VALUE,
8-
SIMPLE_MODIFIED_BODY_VALUE,
9-
SIMPLE_ORIGINAL_BODY_VALUE,
10-
)
5+
from tests.testapp.constants import MODIFIED_BODY_VALUE, ORIGINAL_BODY_VALUE
116

127

138
"""
@@ -31,35 +26,32 @@
3126
def set_body_values(
3227
apps,
3328
schema_editor,
34-
simple_value: Sequence[tuple[str, Any]],
35-
complex_value: Sequence[dict[str, Any]],
29+
new_value: Sequence[dict[str, Any]],
3630
):
3731
TestPage = apps.get_model("testapp", "TestPage")
3832
TestSnippet = apps.get_model("testapp", "TestSnippet")
3933

4034
# Modify the second TestPage, but leave the first alone
4135
# Uses `simple_value` to show that the new field handles the simple 'list of tuples' format correctly
4236
page = TestPage.objects.last()
43-
page.body = simple_value
37+
page.body = new_value
4438
page.save()
4539

4640
# Modify the second TestSnippet, but leave the first alone
4741
# Uses `complex_value` to show that the new field handles the more complicated 'list of dicts' format correctly
4842
snippet = TestSnippet.objects.last()
49-
snippet.body = complex_value
43+
snippet.body = new_value
5044
snippet.save()
5145

5246

5347
def migrate_forwards(apps, schema_editor):
5448
set_body_values(
55-
apps, schema_editor, SIMPLE_MODIFIED_BODY_VALUE, COMPLEX_MODIFIED_BODY_VALUE
49+
apps, schema_editor, MODIFIED_BODY_VALUE
5650
)
5751

5852

5953
def migrate_backwards(apps, schema_editor):
60-
set_body_values(
61-
apps, schema_editor, SIMPLE_ORIGINAL_BODY_VALUE, COMPLEX_ORIGINAL_BODY_VALUE
62-
)
54+
set_body_values(apps, schema_editor, ORIGINAL_BODY_VALUE)
6355

6456

6557
class Migration(migrations.Migration):

tests/testapp/migrations/0005_create_more_test_objects.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
from django.db import migrations
22

3-
from tests.testapp.constants import (
4-
COMPLEX_ORIGINAL_BODY_VALUE,
5-
SIMPLE_ORIGINAL_BODY_VALUE,
6-
)
3+
from tests.testapp.constants import ORIGINAL_BODY_VALUE
4+
75

86

97
"""
@@ -36,7 +34,7 @@ def migrate_forwards(apps, schema_editor):
3634
draft_title="Test Page Tres",
3735
slug="test-page-3",
3836
content_type=content_type,
39-
body=SIMPLE_ORIGINAL_BODY_VALUE,
37+
body=ORIGINAL_BODY_VALUE,
4038
depth=3,
4139
locale_id=1,
4240
path="000100010003",
@@ -49,7 +47,7 @@ def migrate_forwards(apps, schema_editor):
4947
draft_title="Test Page Cuatro",
5048
slug="test-page-4",
5149
content_type=content_type,
52-
body=COMPLEX_ORIGINAL_BODY_VALUE,
50+
body=ORIGINAL_BODY_VALUE,
5351
depth=3,
5452
locale_id=1,
5553
path="000100010004",
@@ -59,12 +57,12 @@ def migrate_forwards(apps, schema_editor):
5957
)
6058
Page.objects.filter(depth=2).update(numchild=4)
6159

62-
# Add snippetS
60+
# Add snippets
6361
TestSnippet.objects.create(
64-
title="Test Snippet Tres", body=SIMPLE_ORIGINAL_BODY_VALUE
62+
title="Test Snippet Tres", body=ORIGINAL_BODY_VALUE
6563
)
6664
TestSnippet.objects.create(
67-
title="Test Snippet Cuatro", body=COMPLEX_ORIGINAL_BODY_VALUE
65+
title="Test Snippet Cuatro", body=ORIGINAL_BODY_VALUE
6866
)
6967

7068

tests/testapp/migrations/0006_modify_body_values_with_bulk_update.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@
22

33
from django.db import migrations
44

5-
from tests.testapp.constants import (
6-
COMPLEX_MODIFIED_BODY_VALUE,
7-
COMPLEX_ORIGINAL_BODY_VALUE,
8-
SIMPLE_MODIFIED_BODY_VALUE,
9-
SIMPLE_ORIGINAL_BODY_VALUE,
10-
)
5+
from tests.testapp.constants import MODIFIED_BODY_VALUE, ORIGINAL_BODY_VALUE
116

127

138
"""
@@ -26,35 +21,30 @@
2621
def set_body_values(
2722
apps,
2823
schema_editor,
29-
simple_value: Sequence[tuple[str, Any]],
30-
complex_value: Sequence[dict[str, Any]],
24+
new_value: Sequence[dict[str, Any]],
3125
):
3226
TestPage = apps.get_model("testapp", "TestPage")
3327
TestSnippet = apps.get_model("testapp", "TestSnippet")
3428

3529
# Modify "Test Page Cuatro"
3630
# Uses `simple_value` to show that the new field handles the simple 'list of tuples' format correctly
3731
page = TestPage.objects.last()
38-
page.body = simple_value
32+
page.body = new_value
3933
TestPage.objects.bulk_update([page], ["body"])
4034

4135
# Modify "Test Snippet Cuatro"
4236
# Uses `complex_value` to show that the new field handles the more complicated 'list of dicts' format correctly
4337
snippet = TestSnippet.objects.last()
44-
snippet.body = complex_value
38+
snippet.body = new_value
4539
TestSnippet.objects.bulk_update([snippet], ["body"])
4640

4741

4842
def migrate_forwards(apps, schema_editor):
49-
set_body_values(
50-
apps, schema_editor, SIMPLE_MODIFIED_BODY_VALUE, COMPLEX_MODIFIED_BODY_VALUE
51-
)
43+
set_body_values(apps, schema_editor, MODIFIED_BODY_VALUE)
5244

5345

5446
def migrate_backwards(apps, schema_editor):
55-
set_body_values(
56-
apps, schema_editor, SIMPLE_ORIGINAL_BODY_VALUE, COMPLEX_ORIGINAL_BODY_VALUE
57-
)
47+
set_body_values(apps, schema_editor, ORIGINAL_BODY_VALUE)
5848

5949

6050
class Migration(migrations.Migration):

0 commit comments

Comments
 (0)