Skip to content

Commit

Permalink
Always use lists of dicts for body values
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Babic committed Dec 23, 2024
1 parent 9a13386 commit 3099a0e
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 86 deletions.
25 changes: 16 additions & 9 deletions tests/test_migration_outcomes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.test import TestCase
from testapp.constants import COMPLEX_MODIFIED_BODY_VALUE, COMPLEX_ORIGINAL_BODY_VALUE
from testapp.constants import ORIGINAL_BODY_VALUE, MODIFIED_BODY_VALUE
from testapp.models import TestPage, TestSnippet


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

self.assertEqual(self.snippet_1.title, "Modified Test Snippet")
self.assertEqual(len(self.snippet_1.body.raw_data), 3)
for i, block in enumerate(self.snippet_1.body.raw_data):
compare_to = COMPLEX_ORIGINAL_BODY_VALUE[i]
compare_to = ORIGINAL_BODY_VALUE[i]
self.assertEqual(block["type"], compare_to["type"])
self.assertEqual(block["value"], compare_to["value"])

Expand All @@ -45,14 +47,16 @@ def test_modified_original_pages_and_snippets_have_retained_their_modified_body_
test_snippet = self.snippet_2

self.assertEqual(test_page.title, "Modified Test Page Deux")
self.assertEqual(len(test_page.body.raw_data), 3)
for i, block in enumerate(test_page.body.raw_data):
compare_to = COMPLEX_MODIFIED_BODY_VALUE[i]
compare_to = MODIFIED_BODY_VALUE[i]
self.assertEqual(block["type"], compare_to["type"])
self.assertEqual(block["value"], compare_to["value"])

self.assertEqual(test_snippet.title, "Modified Test Snippet Deux")
self.assertEqual(len(test_snippet.body.raw_data), 3)
for i, block in enumerate(test_snippet.body.raw_data):
compare_to = COMPLEX_MODIFIED_BODY_VALUE[i]
compare_to = MODIFIED_BODY_VALUE[i]
self.assertEqual(block["type"], compare_to["type"])
self.assertEqual(block["value"], compare_to["value"])

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

self.assertEqual(test_page.title, "Bulk Modified Test Page Tres")
self.assertEqual(len(test_page.body.raw_data), 3)
for i, block in enumerate(test_page.body.raw_data):
compare_to = COMPLEX_ORIGINAL_BODY_VALUE[i]
compare_to = ORIGINAL_BODY_VALUE[i]
self.assertEqual(block["type"], compare_to["type"])
self.assertEqual(block["value"], compare_to["value"])

self.assertEqual(test_snippet.title, "Bulk Modified Test Snippet Tres")
for i, block in enumerate(test_snippet.body.raw_data):
compare_to = COMPLEX_ORIGINAL_BODY_VALUE[i]
compare_to = ORIGINAL_BODY_VALUE[i]
self.assertEqual(block["type"], compare_to["type"])
self.assertEqual(block["value"], compare_to["value"])

Expand All @@ -94,13 +99,15 @@ def test_modified_newer_pages_and_snippets_have_retained_their_modified_body_val
test_snippet = self.snippet_4

self.assertEqual(test_page.title, "Bulk Modified Test Page Cuatro")
self.assertEqual(len(test_page.body.raw_data), 3)
for i, block in enumerate(test_page.body.raw_data):
compare_to = COMPLEX_MODIFIED_BODY_VALUE[i]
compare_to = MODIFIED_BODY_VALUE[i]
self.assertEqual(block["type"], compare_to["type"])
self.assertEqual(block["value"], compare_to["value"])

self.assertEqual(test_snippet.title, "Bulk Modified Test Snippet Cuatro")
self.assertEqual(len(test_snippet.body.raw_data), 3)
for i, block in enumerate(test_snippet.body.raw_data):
compare_to = COMPLEX_MODIFIED_BODY_VALUE[i]
compare_to = MODIFIED_BODY_VALUE[i]
self.assertEqual(block["type"], compare_to["type"])
self.assertEqual(block["value"], compare_to["value"])
32 changes: 12 additions & 20 deletions tests/testapp/constants.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
from .utils import convert_simple_streamfield_value_to_dicts


SIMPLE_ORIGINAL_BODY_VALUE = [
("text", "Hello World"),
("integer", "123"),
("date", "2024-12-25"),
TEXT_BLOCK_ID = "813c03ab-e647-4bb0-9c7a-b3c2cb8ea10c"
INTEGER_BLOCK_ID = "e1e72d7d-ce60-45dd-bec7-5a229c2bfb67"
DATE_BLOCK_ID = "550719d5-8fb8-4233-a5ea-51a757708c92"

ORIGINAL_BODY_VALUE = [
{"type": "text", "value": "Hello World!", "id": TEXT_BLOCK_ID},
{"type": "integer", "value": "123", "id": INTEGER_BLOCK_ID},
{"type": "date", "value": "2024-12-25", "id": DATE_BLOCK_ID},
]

COMPLEX_ORIGINAL_BODY_VALUE = convert_simple_streamfield_value_to_dicts(
SIMPLE_ORIGINAL_BODY_VALUE, add_ids=True
)


SIMPLE_MODIFIED_BODY_VALUE = [
("text", "Goodbye Galaxy!"),
("integer", "321"),
("date", "3024-12-25"),
MODIFIED_BODY_VALUE = [
{"type": "text", "value": "Goodbye Galaxy!", "id": TEXT_BLOCK_ID},
{"type": "integer", "value": "321", "id": INTEGER_BLOCK_ID},
{"type": "date", "value": "3024-12-25", "id": DATE_BLOCK_ID},
]

COMPLEX_MODIFIED_BODY_VALUE = convert_simple_streamfield_value_to_dicts(
SIMPLE_MODIFIED_BODY_VALUE
)
4 changes: 2 additions & 2 deletions tests/testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Generated by Django 4.2.17 on 2024-12-22 08:16

from django.db import migrations, models
import django.db.models.deletion
import wagtail.blocks
import wagtail.fields

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
Expand Down
15 changes: 5 additions & 10 deletions tests/testapp/migrations/0002_create_test_objects.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from django.db import migrations

from tests.testapp.constants import (
COMPLEX_ORIGINAL_BODY_VALUE,
SIMPLE_ORIGINAL_BODY_VALUE,
)
from tests.testapp.constants import ORIGINAL_BODY_VALUE


"""
Expand Down Expand Up @@ -42,7 +39,7 @@ def migrate_forwards(apps, schema_editor):
draft_title="Test Page",
slug="test-page",
content_type=content_type,
body=SIMPLE_ORIGINAL_BODY_VALUE,
body=ORIGINAL_BODY_VALUE,
depth=3,
locale_id=1,
path="000100010001",
Expand All @@ -55,7 +52,7 @@ def migrate_forwards(apps, schema_editor):
draft_title="Test Page Deux",
slug="test-page-2",
content_type=content_type,
body=COMPLEX_ORIGINAL_BODY_VALUE,
body=ORIGINAL_BODY_VALUE,
depth=3,
locale_id=1,
path="000100010002",
Expand All @@ -66,10 +63,8 @@ def migrate_forwards(apps, schema_editor):
Page.objects.filter(depth=2).update(numchild=2)

# Add two test snippet
TestSnippet.objects.create(title="Test Snippet", body=SIMPLE_ORIGINAL_BODY_VALUE)
TestSnippet.objects.create(
title="Test Snippet Deux", body=COMPLEX_ORIGINAL_BODY_VALUE
)
TestSnippet.objects.create(title="Test Snippet", body=ORIGINAL_BODY_VALUE)
TestSnippet.objects.create(title="Test Snippet Deux", body=ORIGINAL_BODY_VALUE)


def migrate_backwards(apps, schema_editor):
Expand Down
22 changes: 6 additions & 16 deletions tests/testapp/migrations/0004_modify_body_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

from django.db import migrations

from tests.testapp.constants import (
COMPLEX_MODIFIED_BODY_VALUE,
COMPLEX_ORIGINAL_BODY_VALUE,
SIMPLE_MODIFIED_BODY_VALUE,
SIMPLE_ORIGINAL_BODY_VALUE,
)
from tests.testapp.constants import MODIFIED_BODY_VALUE, ORIGINAL_BODY_VALUE


"""
Expand All @@ -31,35 +26,30 @@
def set_body_values(
apps,
schema_editor,
simple_value: Sequence[tuple[str, Any]],
complex_value: Sequence[dict[str, Any]],
new_value: Sequence[dict[str, Any]],
):
TestPage = apps.get_model("testapp", "TestPage")
TestSnippet = apps.get_model("testapp", "TestSnippet")

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

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


def migrate_forwards(apps, schema_editor):
set_body_values(
apps, schema_editor, SIMPLE_MODIFIED_BODY_VALUE, COMPLEX_MODIFIED_BODY_VALUE
)
set_body_values(apps, schema_editor, MODIFIED_BODY_VALUE)


def migrate_backwards(apps, schema_editor):
set_body_values(
apps, schema_editor, SIMPLE_ORIGINAL_BODY_VALUE, COMPLEX_ORIGINAL_BODY_VALUE
)
set_body_values(apps, schema_editor, ORIGINAL_BODY_VALUE)


class Migration(migrations.Migration):
Expand Down
19 changes: 6 additions & 13 deletions tests/testapp/migrations/0005_create_more_test_objects.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from django.db import migrations

from tests.testapp.constants import (
COMPLEX_ORIGINAL_BODY_VALUE,
SIMPLE_ORIGINAL_BODY_VALUE,
)
from tests.testapp.constants import ORIGINAL_BODY_VALUE


"""
Expand Down Expand Up @@ -36,7 +33,7 @@ def migrate_forwards(apps, schema_editor):
draft_title="Test Page Tres",
slug="test-page-3",
content_type=content_type,
body=SIMPLE_ORIGINAL_BODY_VALUE,
body=ORIGINAL_BODY_VALUE,
depth=3,
locale_id=1,
path="000100010003",
Expand All @@ -49,7 +46,7 @@ def migrate_forwards(apps, schema_editor):
draft_title="Test Page Cuatro",
slug="test-page-4",
content_type=content_type,
body=COMPLEX_ORIGINAL_BODY_VALUE,
body=ORIGINAL_BODY_VALUE,
depth=3,
locale_id=1,
path="000100010004",
Expand All @@ -59,13 +56,9 @@ def migrate_forwards(apps, schema_editor):
)
Page.objects.filter(depth=2).update(numchild=4)

# Add snippetS
TestSnippet.objects.create(
title="Test Snippet Tres", body=SIMPLE_ORIGINAL_BODY_VALUE
)
TestSnippet.objects.create(
title="Test Snippet Cuatro", body=COMPLEX_ORIGINAL_BODY_VALUE
)
# Add snippets
TestSnippet.objects.create(title="Test Snippet Tres", body=ORIGINAL_BODY_VALUE)
TestSnippet.objects.create(title="Test Snippet Cuatro", body=ORIGINAL_BODY_VALUE)


def migrate_backwards(apps, schema_editor):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@

from django.db import migrations

from tests.testapp.constants import (
COMPLEX_MODIFIED_BODY_VALUE,
COMPLEX_ORIGINAL_BODY_VALUE,
SIMPLE_MODIFIED_BODY_VALUE,
SIMPLE_ORIGINAL_BODY_VALUE,
)
from tests.testapp.constants import MODIFIED_BODY_VALUE, ORIGINAL_BODY_VALUE


"""
Expand All @@ -26,35 +21,30 @@
def set_body_values(
apps,
schema_editor,
simple_value: Sequence[tuple[str, Any]],
complex_value: Sequence[dict[str, Any]],
new_value: Sequence[dict[str, Any]],
):
TestPage = apps.get_model("testapp", "TestPage")
TestSnippet = apps.get_model("testapp", "TestSnippet")

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

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


def migrate_forwards(apps, schema_editor):
set_body_values(
apps, schema_editor, SIMPLE_MODIFIED_BODY_VALUE, COMPLEX_MODIFIED_BODY_VALUE
)
set_body_values(apps, schema_editor, MODIFIED_BODY_VALUE)


def migrate_backwards(apps, schema_editor):
set_body_values(
apps, schema_editor, SIMPLE_ORIGINAL_BODY_VALUE, COMPLEX_ORIGINAL_BODY_VALUE
)
set_body_values(apps, schema_editor, ORIGINAL_BODY_VALUE)


class Migration(migrations.Migration):
Expand Down

0 comments on commit 3099a0e

Please sign in to comment.