From 3da1aef5a3da3ad92ee187cfc30b027dbef2a9d3 Mon Sep 17 00:00:00 2001 From: Fabian Braun Date: Sun, 27 Oct 2024 01:08:44 +0200 Subject: [PATCH] Fix migration --- djangocms_link/fields.py | 2 +- djangocms_link/migrations/0017_link_link.py | 7 ++- tests/test_migrations.py | 70 +++++++++++++++++++++ 3 files changed, 75 insertions(+), 4 deletions(-) diff --git a/djangocms_link/fields.py b/djangocms_link/fields.py index 1ca48a41..509dcd4c 100644 --- a/djangocms_link/fields.py +++ b/djangocms_link/fields.py @@ -304,7 +304,7 @@ class LinkField(JSONField): """A link is a JSON field with a default LinkFormField""" def __init__(self, *args, **kwargs): - kwargs.setdefault("default", {}) + kwargs.setdefault("default", dict) kwargs.setdefault("blank", True) kwargs.setdefault("help_text", "-") super().__init__(*args, **kwargs) diff --git a/djangocms_link/migrations/0017_link_link.py b/djangocms_link/migrations/0017_link_link.py index bd16ad27..cd8f9505 100644 --- a/djangocms_link/migrations/0017_link_link.py +++ b/djangocms_link/migrations/0017_link_link.py @@ -7,10 +7,11 @@ def forward(apps, schema_editor): Link = apps.get_model("djangocms_link", "Link") - for link in Link.objects.all(): + links = Link.objects.all() + for link in links: if link.external_link: anchor = "#" + link.anchor if link.anchor else "" - link.link = {"external_link": link.url + anchor} + link.link = {"external_link": link.external_link + anchor} elif link.internal_link: opt = link.internal_link._meta link.link = {"internal_link": f"{opt.app_label}.{opt.model_name}:{link.internal_link.pk}"} @@ -24,7 +25,7 @@ def forward(apps, schema_editor): link.link = {"external_link": f"mailto:{link.mailto}"} elif link.anchor: link.link = {"external_link": "#" + link.anchor} - Link.objects.bulk_update(Link.objects.all(), ["link"]) + Link.objects.bulk_update(links, ["link"]) def backward(apps, schema_editor): diff --git a/tests/test_migrations.py b/tests/test_migrations.py index 8fa6214d..92dab9f9 100644 --- a/tests/test_migrations.py +++ b/tests/test_migrations.py @@ -1,10 +1,20 @@ # original from # http://tech.octopus.energy/news/2016/01/21/testing-for-missing-migrations-in-django.html from io import StringIO +from unittest import skipIf from django.core.management import call_command +from django.forms import model_to_dict from django.test import TestCase, override_settings +from cms.api import create_page + +from django_test_migrations.contrib.unittest_case import MigratorTestCase + +from djangocms_link import __version__ +from djangocms_link.helpers import get_link +from tests.helpers import get_filer_file + class MigrationTestCase(TestCase): @override_settings(MIGRATION_MODULES={}) @@ -27,3 +37,63 @@ def test_for_missing_migrations(self): if status_code == '1' and "djangocms_link" in output: self.fail(f'There are missing migrations:\n {output.getvalue()}') + +# @skipIf(__version__ >= '5', "Migration has already been tested before releasing version 5") +class MigrationToVersion5(MigratorTestCase): + migrate_from = ('djangocms_link', '0016_alter_link_cmsplugin_ptr') + migrate_to = ('djangocms_link', '0018_remove_link_anchor_remove_link_external_link_and_more') + + def setUp(self): + self.page = model_to_dict(create_page( + title='test', + template='page.html', + language='en', + )) + if hasattr(self.page, "node"): + self.node = model_to_dict(self.page.node) + self.file = model_to_dict(get_filer_file()) + super().setUp() + + def prepare(self): + Link = self.old_state.apps.get_model('djangocms_link', 'Link') + Page = self.old_state.apps.get_model('cms', 'Page') + File = self.old_state.apps.get_model('filer', 'File') + + self.links = [ + # Link.objects.create( + # template="default", + # name="My Link", + # internal_link=self.page, + # anchor="some_id", + # ), + Link.objects.create( + template="default", + name="My Link", + external_link="http://www.django-cms.com", + ), + # Link.objects.create( + # template="default", + # name="My Link", + # file_link=self.file, + # ), + Link.objects.create( + template="default", + name="My Link", + mailto="test@email.com", + ), + Link.objects.create( + template="default", + name="My Link", + phone="+01 234 567 89", + ), + ] + self.urls = ["http://www.django-cms.com", "mailto:test@email.com", "tel:+0123456789"] + + def test_tags_migrated(self): + Link = self.new_state.apps.get_model('djangocms_link', 'Link') + links = Link.objects.all() + + for link, url in zip(links, self.urls): + with self.subTest(link=link, url=url): + self.assertEqual(get_link(link.link), url) +