From 3783fb83d16154292e4b1d8a7690fcd3a0d296dc Mon Sep 17 00:00:00 2001 From: Eric Sherman Date: Tue, 3 Jan 2023 19:02:31 -0600 Subject: [PATCH] initially playing around with tests --- tests/settings.py | 3 +- tests/tests/test_import.py | 100 +++++++++++++++++++++++++++++++++++++ tests/tests/test_views.py | 24 ++++++--- 3 files changed, 119 insertions(+), 8 deletions(-) diff --git a/tests/settings.py b/tests/settings.py index 23bf1cd..7bee120 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -147,7 +147,8 @@ WAGTAILTRANSFER_UPDATE_RELATED_MODELS = ['wagtailimages.Image', 'tests.advert'] WAGTAILTRANSFER_LOOKUP_FIELDS = { - 'tests.category': ['name'] + 'tests.category': ['name'], + 'wagtailcore.page': ['slug', 'locale_id'], } # The default name for the Page -> Comment relation from Wagtail 2.15 onward. Setting this ensures that diff --git a/tests/tests/test_import.py b/tests/tests/test_import.py index 84595b4..9988096 100644 --- a/tests/tests/test_import.py +++ b/tests/tests/test_import.py @@ -67,6 +67,38 @@ def test_import_model(self): cats = Category.objects.all() self.assertEquals(cats.count(), 2) + def test_import_page_via_lookup(self): + + data = """{ + "ids_for_import": [ + ["wagtailcore.page", 12] + ], + "mappings": [ + ["wagtailcore.page", 12, "/home/"] + ], + "objects": [ + { + "model": "tests.simplepage", + "pk": 12, + "parent_id": 1, + "fields": { + "title": "New home", + "show_in_menus": false, + "live": true, + "slug": "home", + "intro": "This is the updated homepage", + "wagtail_admin_comments": [] + } + } + ] + }""" + + importer = ImportPlanner(model="tests.category") + importer.add_json(data) + importer.run() + # add assertions + + def test_import_pages(self): # make a draft edit to the homepage @@ -134,6 +166,73 @@ def test_import_pages(self): created_page_revision = created_page.get_latest_revision_as_page() self.assertEqual(created_page_revision.intro, "This page is imported from the source site") + def test_import_pages_via_lookup(self): + # make a draft edit to the homepage + home = SimplePage.objects.get(slug='home') + home.title = "Draft home" + home.save_revision() + + data = """{ + "ids_for_import": [ + ["wagtailcore.page", 12], + ["wagtailcore.page", 15] + ], + "mappings": [ + ["wagtailcore.page", 12, "/home/"], + ["wagtailcore.page", 15, "/home/imported-child-page"] + ], + "objects": [ + { + "model": "tests.simplepage", + "pk": 15, + "parent_id": 12, + "fields": { + "title": "Imported child page", + "show_in_menus": false, + "live": true, + "slug": "imported-child-page", + "intro": "This page is imported from the source site", + "wagtail_admin_comments": [] + } + }, + { + "model": "tests.simplepage", + "pk": 12, + "parent_id": 1, + "fields": { + "title": "New home", + "show_in_menus": false, + "live": true, + "slug": "home", + "intro": "This is the updated homepage", + "wagtail_admin_comments": [] + } + } + ] + }""" + + importer = ImportPlanner(root_page_source_pk=12, destination_parent_id=None) + importer.add_json(data) + importer.run() + + updated_page = SimplePage.objects.get(url_path='/home/') + self.assertEqual(updated_page.intro, "This is the updated homepage") + self.assertEqual(updated_page.title, "New home") + self.assertEqual(updated_page.draft_title, "New home") + + # get_latest_revision (as used in the edit-page view) should also reflect the imported content + updated_page_revision = updated_page.get_latest_revision_as_page() + self.assertEqual(updated_page_revision.intro, "This is the updated homepage") + self.assertEqual(updated_page_revision.title, "New home") + + created_page = SimplePage.objects.get(url_path='/home/imported-child-page/') + self.assertEqual(created_page.intro, "This page is imported from the source site") + # An initial page revision should also be created + self.assertTrue(created_page.get_latest_revision()) + created_page_revision = created_page.get_latest_revision_as_page() + self.assertEqual(created_page_revision.intro, "This page is imported from the source site") + + def test_import_pages_with_fk(self): data = """{ "ids_for_import": [ @@ -423,6 +522,7 @@ def test_import_page_with_child_models(self): importer = ImportPlanner(root_page_source_pk=100, destination_parent_id=2) importer.add_json(data) + # importer.run() new_page = SectionedPage.objects.get(id=page_id) diff --git a/tests/tests/test_views.py b/tests/tests/test_views.py index 668f493..0a7762d 100644 --- a/tests/tests/test_views.py +++ b/tests/tests/test_views.py @@ -2,10 +2,11 @@ from datetime import date, datetime, timezone from unittest import mock +from django.conf import settings from django.contrib.auth.models import AnonymousUser, Group, Permission, User from django.contrib.contenttypes.models import ContentType from django.shortcuts import redirect -from django.test import TestCase +from django.test import TestCase, override_settings, modify_settings from django.urls import reverse from tests.models import SponsoredPage @@ -24,6 +25,17 @@ def test_get(self): self.assertEqual(response.status_code, 200) self.assertContains(response, 'data-wagtail-component="content-import-form"') +class TestCheckPageExistence(TestCase): + fixtures = ['test.json'] + + def setUp(self): + self.client.login(username='admin', password='password') + + def test_get_page_existence(self): + # in the case of `'wagtailcore.page': ['slug', 'locale_id']` TODO: override this value instead + response = self.client.get('/admin/wagtail-transfer/api/check_uid/?uid=home,1') + # 200 means existing page was found + self.assertEqual(response.status_code, 200) @mock.patch('requests.post') @mock.patch('requests.get') @@ -35,16 +47,14 @@ def setUp(self): def test_run(self, get, post): get.return_value.status_code = 200 + # TODO these values should be adjusted only in cases where we are not using UIDs for pages get.return_value.content = b"""{ "ids_for_import": [ - ["wagtailcore.page", 12], - ["wagtailcore.page", 15], - ["wagtailcore.page", 16] ], "mappings": [ - ["wagtailcore.page", 12, "22222222-2222-2222-2222-222222222222"], - ["wagtailcore.page", 15, "00017017-5555-5555-5555-555555555555"], - ["wagtailcore.page", 16, "00e99e99-6666-6666-6666-666666666666"], + ["wagtailcore.page", 12, "/home/"], + ["wagtailcore.page", 15, "/home/oil-is-great/"], + ["wagtailcore.page", 16, "/home/eggs-are-great-too/"], ["tests.advert", 11, "adadadad-1111-1111-1111-111111111111"], ["tests.advert", 8, "adadadad-8888-8888-8888-888888888888"] ],