Skip to content

Commit

Permalink
Add testapp with lots of migrations that play with data
Browse files Browse the repository at this point in the history
  • Loading branch information
ababic committed Dec 20, 2024
1 parent e89477f commit ce49700
Show file tree
Hide file tree
Showing 13 changed files with 495 additions and 0 deletions.
Empty file added tests/__init__.py
Empty file.
Empty file added tests/testapp/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions tests/testapp/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class TestAppConfig(AppConfig):
default_auto_field = "django.db.models.AutoField"
name = "testapp"
79 changes: 79 additions & 0 deletions tests/testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Generated by Django 5.1.4 on 2024-12-20 17:05

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


class Migration(migrations.Migration):

initial = True

dependencies = [
("wagtailcore", "0094_alter_page_locale"),
]

operations = [
migrations.CreateModel(
name="TestPage",
fields=[
(
"page_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to="wagtailcore.page",
),
),
(
"body",
wagtail.fields.StreamField(
[("text", 0), ("integer", 1), ("date", 2)],
blank=True,
block_lookup={
0: ("wagtail.blocks.TextBlock", (), {}),
1: ("wagtail.blocks.IntegerBlock", (), {}),
2: ("wagtail.blocks.DateBlock", (), {}),
},
),
),
],
options={
"abstract": False,
},
bases=("wagtailcore.page", models.Model),
),
migrations.CreateModel(
name="TestSnippet",
fields=[
(
"id",
models.AutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"body",
wagtail.fields.StreamField(
[("text", 0), ("integer", 1), ("date", 2)],
blank=True,
block_lookup={
0: ("wagtail.blocks.TextBlock", (), {}),
1: ("wagtail.blocks.IntegerBlock", (), {}),
2: ("wagtail.blocks.DateBlock", (), {}),
},
),
),
("title", models.CharField(max_length=255)),
],
options={
"abstract": False,
},
),
]
54 changes: 54 additions & 0 deletions tests/testapp/migrations/0002_create_test_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# Generated by Django 5.1.4 on 2024-12-20 16:16

from django.db import migrations


def migrate_forwards(apps, schema_editor):
ContentType = apps.get_model("contenttypes", "ContentType")
Page = apps.get_model("wagtailcore", "Page")
TestPage = apps.get_model("testapp", "TestPage")
TestSnippet = apps.get_model("testapp", "TestSnippet")

body_value = [("text", "Hello World"), ("integer", 123), ("date", "2024-12-25")]
content_type, _ = ContentType.objects.get_or_create(app_label="testapp", model="testpage")

# Add a test page
TestPage.objects.create(
title="Test Page",
draft_title="Test Page",
slug="test-page",
content_type=content_type,
body=body_value,
depth=3,
locale_id=1,
path="000100010001",
numchild=0,
url_path="/home/test-page/",
translation_key="72b25f92-0a15-4bc4-b0cf-9bf1eb976257",
)
Page.objects.filter(depth=2).update(numchild=1)

# Add a test snippet
TestSnippet.objects.create(title="Test Snippet", body=body_value)


def migrate_backwards(apps, schema_editor):
TestPage = apps.get_model("testapp", "TestPage")
TestSnippet = apps.get_model("testapp", "TestSnippet")

for page in TestPage.objects.all():
page.delete()

for snippet in TestSnippet.objects.all():
snippet.delete()


class Migration(migrations.Migration):

dependencies = [
("testapp", "0001_initial"),
]

operations = [
migrations.RunPython(migrate_forwards, migrate_backwards),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.4 on 2024-12-20 16:17

import mlstreamfield.fields
from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("testapp", "0002_create_test_objects"),
]

operations = [
migrations.AlterField(
model_name="testpage",
name="body",
field=mlstreamfield.fields.StreamField(blank=True, block_lookup={}),
),
migrations.AlterField(
model_name="testsnippet",
name="body",
field=mlstreamfield.fields.StreamField(blank=True, block_lookup={}),
),
]
37 changes: 37 additions & 0 deletions tests/testapp/migrations/0004_modify_body_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 5.1.4 on 2024-12-20 16:16

from django.db import migrations


def update_body_values(apps, schema_editor, body_value):
TestPage = apps.get_model("testapp", "TestPage")
TestSnippet = apps.get_model("testapp", "TestSnippet")

# Modify the test page
page = TestPage.objects.get()
page.body = body_value
page.save()

# Modify the test snippet
snippet = TestSnippet.objects.get()
snippet.body = body_value
snippet.save()


def migrate_forwards(apps, schema_editor):
update_body_values(apps, schema_editor, [("text", "Goodbye Galaxy!"), ("integer", 321), ("date", "4024-12-25")])


def migrate_backwards(apps, schema_editor):
update_body_values(apps, schema_editor, [("text", "Hello World"), ("integer", 123), ("date", "2024-12-25")])


class Migration(migrations.Migration):

dependencies = [
("testapp", "0003_swap_native_streamfield_for_mlstreamfield"),
]

operations = [
migrations.RunPython(migrate_forwards, migrate_backwards),
]
53 changes: 53 additions & 0 deletions tests/testapp/migrations/0005_create_more_test_objects.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Generated by Django 5.1.4 on 2024-12-20 16:16

from django.db import migrations


def migrate_forwards(apps, schema_editor):
ContentType = apps.get_model("contenttypes", "ContentType")
Page = apps.get_model("wagtailcore", "Page")
TestPage = apps.get_model("testapp", "TestPage")
TestSnippet = apps.get_model("testapp", "TestSnippet")

body_value = [("text", "Hello World"), ("integer", 123), ("date", "2024-12-25")]

# Add a test page
TestPage.objects.create(
title="Test Page 2",
draft_title="Test Page 2",
slug="test-page-2",
content_type=ContentType.objects.get(app_label="testapp", model="testpage"),
body=body_value,
depth=3,
locale_id=1,
path="000100010002",
numchild=0,
url_path="/home/test-page-2/",
translation_key="9f121f31-bc8b-4591-9955-9fbe7bbff256",
)
Page.objects.filter(depth=2).update(numchild=1)

# Add a test snippet
TestSnippet.objects.create(title="Test Snippet 2", body=body_value)


def migrate_backwards(apps, schema_editor):
TestPage = apps.get_model("testapp", "TestPage")
TestSnippet = apps.get_model("testapp", "TestSnippet")

new_page = TestPage.objects.get(path="000100010002")
new_page.delete()

new_snippet = TestSnippet.objects.get(title="Test Snippet 2")
new_snippet.delete()


class Migration(migrations.Migration):

dependencies = [
("testapp", "0004_modify_body_values"),
]

operations = [
migrations.RunPython(migrate_forwards, migrate_backwards),
]
43 changes: 43 additions & 0 deletions tests/testapp/migrations/0006_modify_non_body_values.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Generated by Django 5.1.4 on 2024-12-20 16:16

from django.db import migrations


def update_title_values(apps, schema_editor, forwards=True):
TestPage = apps.get_model("testapp", "TestPage")
TestSnippet = apps.get_model("testapp", "TestSnippet")

# Modify test pages
for page in TestPage.objects.all():
if forwards:
page.title = f"Super {page.title}"
else:
page.title = page.title.removeprefix("Super ")
page.save()

# Modify test snippets
for snippet in TestSnippet.objects.all():
if forwards:
snippet.title = f"Super {snippet.title}"
else:
snippet.title = snippet.title.removeprefix("Super ")
snippet.save()


def migrate_forwards(apps, schema_editor):
update_title_values(apps, schema_editor, forwards=True)


def migrate_backwards(apps, schema_editor):
update_title_values(apps, schema_editor, forwards=False)


class Migration(migrations.Migration):

dependencies = [
("testapp", "0005_create_more_test_objects"),
]

operations = [
migrations.RunPython(migrate_forwards, migrate_backwards),
]
Empty file.
37 changes: 37 additions & 0 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from django.db import models

from wagtail import blocks
from wagtail.admin.panels import FieldPanel
from wagtail.models import Page
from wagtail.snippets.models import register_snippet

from mlstreamfield.fields import StreamField


class BodyFieldMixin(models.Model):
body = StreamField(
[
("text", blocks.TextBlock()),
("integer", blocks.IntegerBlock()),
("date", blocks.DateBlock()),
],
blank=True,
)

class Meta:
abstract = True


class TestPage(BodyFieldMixin, Page):
content_panels = Page.content_panels + [
FieldPanel("body")
]


@register_snippet
class TestSnippet(BodyFieldMixin, models.Model):
title = models.CharField(max_length=255)
panels = [
FieldPanel("title"),
FieldPanel("body")
]
Loading

0 comments on commit ce49700

Please sign in to comment.