Skip to content

Commit

Permalink
Add tests for tag deletion tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
emilytoppm committed Oct 19, 2020
1 parent 727f831 commit f438cad
Showing 1 changed file with 124 additions and 1 deletion.
125 changes: 124 additions & 1 deletion tests/tests/test_import.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import importlib
import os.path
import shutil
from unittest import mock

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.files.images import ImageFile
from django.test import TestCase
from django.test import TestCase, override_settings
from wagtail.core.models import Collection, Page
from wagtail.images.models import Image

Expand Down Expand Up @@ -1449,3 +1450,125 @@ def test_import_model_with_generic_foreign_key(self):
imported_ad = Advert.objects.filter(id=4).first()
self.assertIsNotNone(imported_ad)
self.assertEqual(imported_ad.tags.first().name, "test_tag")

def test_import_model_with_deleted_reverse_related_models(self):
# test re-importing a model where WAGTAILTRANSFER_FOLLOWED_REVERSE_RELATIONS is used to track tag deletions
# will delete tags correctly
data = """{
"ids_for_import": [["tests.advert", 4]],
"mappings": [
["taggit.tag", 152, "ac92b2ba-0fa6-11eb-800b-287fcf66f689"],
["tests.advert", 4, "ac931726-0fa6-11eb-800c-287fcf66f689"],
["taggit.taggeditem", 150, "ac938e5a-0fa6-11eb-800d-287fcf66f689"]
],
"objects": [
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[<Tag: test_tag>]", "tagged_items": [150]}
},
{
"model": "taggit.taggeditem",
"pk": 150,
"fields": {"content_object": ["tests.advert", 4], "tag": 152}
},
{
"model": "taggit.tag",
"pk": 152,
"fields": {"name": "test_tag", "slug": "testtag"}
}
]
}"""

importer = ImportPlanner(root_page_source_pk=1, destination_parent_id=None)
importer.add_json(data)
importer.run()

imported_ad = Advert.objects.filter(id=4).first()
self.assertIsNotNone(imported_ad)
self.assertEqual(imported_ad.tags.first().name, "test_tag")

data = """{
"ids_for_import": [["tests.advert", 4]],
"mappings": [
["tests.advert", 4, "ac931726-0fa6-11eb-800c-287fcf66f689"]
],
"objects": [
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[]", "tagged_items": []}
}
]
}"""

importer = ImportPlanner(root_page_source_pk=1, destination_parent_id=None)
importer.add_json(data)
importer.run()

imported_ad = Advert.objects.filter(id=4).first()
self.assertIsNotNone(imported_ad)
self.assertIsNone(imported_ad.tags.first())

@override_settings(WAGTAILTRANSFER_FOLLOWED_REVERSE_RELATIONS=[('tests.advert', 'tagged_items', False)])
def test_import_model_with_untracked_deleted_reverse_related_models(self):
# test re-importing a model where WAGTAILTRANFER_FOLLOWED_REVERSE_RELATIONS is not used to track tag deletions
# will not delete tags
from wagtail_transfer import field_adapters
importlib.reload(field_adapters)
# force reload field adapters as followed/deleted variables are set on module load, so will not get new setting
data = """{
"ids_for_import": [["tests.advert", 4]],
"mappings": [
["taggit.tag", 152, "ac92b2ba-0fa6-11eb-800b-287fcf66f689"],
["tests.advert", 4, "ac931726-0fa6-11eb-800c-287fcf66f689"],
["taggit.taggeditem", 150, "ac938e5a-0fa6-11eb-800d-287fcf66f689"]
],
"objects": [
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[<Tag: test_tag>]", "tagged_items": [150]}
},
{
"model": "taggit.taggeditem",
"pk": 150,
"fields": {"content_object": ["tests.advert", 4], "tag": 152}
},
{
"model": "taggit.tag",
"pk": 152,
"fields": {"name": "test_tag", "slug": "testtag"}
}
]
}"""

importer = ImportPlanner(root_page_source_pk=1, destination_parent_id=None)
importer.add_json(data)
importer.run()

imported_ad = Advert.objects.filter(id=4).first()
self.assertIsNotNone(imported_ad)
self.assertEqual(imported_ad.tags.first().name, "test_tag")

data = """{
"ids_for_import": [["tests.advert", 4]],
"mappings": [
["tests.advert", 4, "ac931726-0fa6-11eb-800c-287fcf66f689"]
],
"objects": [
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[]", "tagged_items": []}
}
]
}"""

importer = ImportPlanner(root_page_source_pk=1, destination_parent_id=None)
importer.add_json(data)
importer.run()

imported_ad = Advert.objects.filter(id=4).first()
self.assertIsNotNone(imported_ad)
self.assertIsNotNone(imported_ad.tags.first())

0 comments on commit f438cad

Please sign in to comment.