Skip to content

Commit

Permalink
Merge pull request #67 from dkirkham/correct-model-types
Browse files Browse the repository at this point in the history
Correct model types
  • Loading branch information
emilytoppm authored Jan 20, 2021
2 parents 994d846 + 06b341a commit cd9fd8e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 22 deletions.
9 changes: 6 additions & 3 deletions tests/fixtures/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@
"pk": 1,
"model": "tests.advert",
"fields": {
"slogan": "put a tiger in your tank"
"slogan": "put a tiger in your tank",
"run_until": "2020-12-23T21:00:00Z"
}
},
{
Expand Down Expand Up @@ -207,14 +208,16 @@
"pk": 2,
"model": "tests.advert",
"fields": {
"slogan": "Buy a thing you definitely need!"
"slogan": "Buy a thing you definitely need!",
"run_until": "2021-04-01T12:00:00Z"
}
},
{
"pk": 3,
"model": "tests.advert",
"fields": {
"slogan": "Buy a full-scale authentically hydrogen-filled replica of the Hindenburg!"
"slogan": "Buy a full-scale authentically hydrogen-filled replica of the Hindenburg!",
"run_until": "1937-05-06T23:25:00Z"
}
},
{
Expand Down
25 changes: 25 additions & 0 deletions tests/migrations/0017_correctmodeltypes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.0.11 on 2020-12-22 22:53

from django.db import migrations, models
import django.utils.timezone


class Migration(migrations.Migration):

dependencies = [
('tests', '0016_advert_tags'),
]

operations = [
migrations.AddField(
model_name='advert',
name='run_until',
field=models.DateTimeField(default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='advert',
name='run_from',
field=models.DateField(blank=True, null=True),
),
]
2 changes: 2 additions & 0 deletions tests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class SimplePage(Page):
class Advert(models.Model):
slogan = models.CharField(max_length=255)
tags = TaggableManager()
run_from = models.DateField(blank=True, null=True)
run_until = models.DateTimeField()


class LongAdvert(Advert):
Expand Down
7 changes: 5 additions & 2 deletions tests/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import uuid
from unittest import mock
from datetime import datetime, timezone

from django.conf import settings
from django.core.files import File
Expand Down Expand Up @@ -448,6 +449,8 @@ def test_objects_api(self):
self.assertEqual(data['ids_for_import'], [])
self.assertEqual(data['objects'][0]['model'], 'tests.advert')
self.assertEqual(data['objects'][0]['fields']['slogan'], "put a tiger in your tank")
self.assertEqual(data['objects'][0]['fields']['run_until'], "2020-12-23T21:00:00Z")
self.assertEqual(data['objects'][0]['fields']['run_from'], None)

self.assertEqual(data['mappings'], [['tests.advert', 1, 'adadadad-1111-1111-1111-111111111111']])

Expand Down Expand Up @@ -517,7 +520,7 @@ def test_model_with_field_lookup(self):
def test_model_with_multi_table_inheritance(self):
# LongAdvert inherits from Advert. Fetching the base instance over the objects api should
# return a LongAdvert model
long_ad = LongAdvert.objects.create(slogan='test', description='longertest')
long_ad = LongAdvert.objects.create(slogan='test', run_until=datetime.now(timezone.utc), description='longertest')

response = self.get({
'tests.advert': [long_ad.pk]
Expand All @@ -534,7 +537,7 @@ def test_model_with_multi_table_inheritance(self):
def test_model_with_tags(self):
# test that a reverse relation such as tagged_items is followed to obtain references to the
# tagged_items, if the model and relationship are specified in WAGTAILTRANSFER_FOLLOWED_REVERSE_RELATIONS
ad = Advert.objects.create(slogan='test')
ad = Advert.objects.create(slogan='test', run_until=datetime.now(timezone.utc))
ad.tags.add('test_tag')

response = self.get({
Expand Down
51 changes: 38 additions & 13 deletions tests/tests/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os.path
import shutil
from unittest import mock
from datetime import datetime, timezone

from django.conf import settings
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -172,7 +173,9 @@ def test_import_pages_with_fk(self):
"model": "tests.advert",
"pk": 11,
"fields": {
"slogan": "put a leopard in your tank"
"slogan": "put a leopard in your tank",
"run_until": "2020-12-23T21:05:43Z",
"run_from": null
}
},
{
Expand All @@ -193,7 +196,9 @@ def test_import_pages_with_fk(self):
"model": "tests.advert",
"pk": 8,
"fields": {
"slogan": "go to work on an egg"
"slogan": "go to work on an egg",
"run_until": "2020-12-23T01:23:45Z",
"run_from": null
}
},
{
Expand All @@ -215,12 +220,16 @@ def test_import_pages_with_fk(self):
self.assertEqual(updated_page.intro, "yay fossil fuels and climate change")
# advert is listed in WAGTAILTRANSFER_UPDATE_RELATED_MODELS, so changes to the advert should have been pulled in too
self.assertEqual(updated_page.advert.slogan, "put a leopard in your tank")
self.assertEqual(updated_page.advert.run_until, datetime(2020, 12, 23, 21, 5, 43, tzinfo=timezone.utc))
self.assertEqual(updated_page.advert.run_from, None)
# author is not listed in WAGTAILTRANSFER_UPDATE_RELATED_MODELS, so should be left unchanged
self.assertEqual(updated_page.author.bio, "Jack Kerouac's car has broken down.")

created_page = SponsoredPage.objects.get(url_path='/home/eggs-are-great-too/')
self.assertEqual(created_page.intro, "you can make cakes with them")
self.assertEqual(created_page.advert.slogan, "go to work on an egg")
self.assertEqual(created_page.advert.run_until, datetime(2020, 12, 23, 1, 23, 45, tzinfo=timezone.utc))
self.assertEqual(created_page.advert.run_from, None)

def test_import_pages_with_orphaned_uid(self):
# the author UID listed here exists in the destination's IDMapping table, but
Expand Down Expand Up @@ -255,7 +264,9 @@ def test_import_pages_with_orphaned_uid(self):
"model": "tests.advert",
"pk": 11,
"fields": {
"slogan": "put a leopard in your tank"
"slogan": "put a leopard in your tank",
"run_until": "2020-12-23T21:05:43Z",
"run_from": null
}
},
{
Expand Down Expand Up @@ -1006,12 +1017,12 @@ def test_import_page_with_parental_many_to_many(self):
{
"model": "tests.advert",
"pk": 200,
"fields": {"slogan": "Buy a thing you definitely need!"}
"fields": {"slogan": "Buy a thing you definitely need!", "run_until": "2021-04-01T12:00:00Z", "run_from": null}
},
{
"model": "tests.advert",
"pk": 300,
"fields": {"slogan": "Buy a half-scale authentically hydrogen-filled replica of the Hindenburg!"}
"fields": {"slogan": "Buy a half-scale authentically hydrogen-filled replica of the Hindenburg!", "run_until": "1937-05-06T23:25:12Z", "run_from": null}
}
]}
"""
Expand All @@ -1029,6 +1040,8 @@ def test_import_page_with_parental_many_to_many(self):

# advert is listed in WAGTAILTRANSFER_UPDATE_RELATED_MODELS, so changes to the advert should have been pulled in too
self.assertEqual(advert_3.slogan, "Buy a half-scale authentically hydrogen-filled replica of the Hindenburg!")
self.assertEqual(advert_3.run_until, datetime(1937, 5, 6, 23, 25, 12, tzinfo=timezone.utc))
self.assertEqual(advert_3.run_from, None)

def test_import_object_with_many_to_many(self):
# Test that an imported object with a ManyToManyField has its ids converted to the destination site's
Expand All @@ -1044,12 +1057,12 @@ def test_import_object_with_many_to_many(self):
{
"model": "tests.advert",
"pk": 200,
"fields": {"slogan": "Buy a thing you definitely need!"}
"fields": {"slogan": "Buy a thing you definitely need!", "run_until": "2021-04-01T12:00:00Z", "run_from": null}
},
{
"model": "tests.advert",
"pk": 300,
"fields": {"slogan": "Buy a half-scale authentically hydrogen-filled replica of the Hindenburg!"}
"fields": {"slogan": "Buy a half-scale authentically hydrogen-filled replica of the Hindenburg!", "run_until": "1937-05-06T23:25:12Z", "run_from": null}
}
]}"""

Expand All @@ -1064,6 +1077,8 @@ def test_import_object_with_many_to_many(self):

# advert is listed in WAGTAILTRANSFER_UPDATE_RELATED_MODELS, so changes to the advert should have been pulled in too
self.assertEqual(advert_3.slogan, "Buy a half-scale authentically hydrogen-filled replica of the Hindenburg!")
self.assertEqual(advert_3.run_until, datetime(1937, 5, 6, 23, 25, 12, tzinfo=timezone.utc))
self.assertEqual(advert_3.run_from, None)

def test_import_with_field_based_lookup(self):
data = """{
Expand Down Expand Up @@ -1097,7 +1112,9 @@ def test_import_with_field_based_lookup(self):
"model": "tests.advert",
"pk": 11,
"fields": {
"slogan": "put a leopard in your tank"
"slogan": "put a leopard in your tank",
"run_until": "2020-12-23T21:05:43Z",
"run_from": null
}
},
{
Expand Down Expand Up @@ -1446,6 +1463,8 @@ def test_import_multi_table_model(self):
"pk": 4,
"fields": {
"slogan": "test",
"run_until": "2020-12-23T12:34:56Z",
"run_from": null,
"description": "longertest"
}
}
Expand All @@ -1459,6 +1478,7 @@ def test_import_multi_table_model(self):
imported_ad = LongAdvert.objects.filter(id=4).first()
self.assertIsNotNone(imported_ad)
self.assertEqual(imported_ad.slogan, "test")
self.assertEqual(imported_ad.run_until, datetime(2020, 12, 23, 12, 34, 56, tzinfo=timezone.utc))
self.assertEqual(imported_ad.description, "longertest")

def test_import_model_with_generic_foreign_key(self):
Expand All @@ -1474,7 +1494,8 @@ def test_import_model_with_generic_foreign_key(self):
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[<Tag: test_tag>]", "tagged_items": null}
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test",
"run_until": "2021-12-23T12:34:56Z", "run_from": null, "tags": "[<Tag: test_tag>]", "tagged_items": null}
},
{
"model": "taggit.taggeditem",
Expand Down Expand Up @@ -1511,7 +1532,8 @@ def test_import_model_with_deleted_reverse_related_models(self):
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[<Tag: test_tag>]", "tagged_items": [150]}
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test",
"run_until": "2021-12-23T12:00:00Z", "run_from": null, "tags": "[<Tag: test_tag>]", "tagged_items": [150]}
},
{
"model": "taggit.taggeditem",
Expand Down Expand Up @@ -1543,7 +1565,8 @@ def test_import_model_with_deleted_reverse_related_models(self):
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[]", "tagged_items": []}
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test",
"run_until": "2021-12-23T12:00:00Z", "run_from": null, "tags": "[]", "tagged_items": []}
}
]
}"""
Expand Down Expand Up @@ -1574,7 +1597,8 @@ def test_import_model_with_untracked_deleted_reverse_related_models(self):
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[<Tag: test_tag>]", "tagged_items": [150]}
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test",
"run_until": "2021-12-23T12:00:00Z", "run_from": null, "tags": "[<Tag: test_tag>]", "tagged_items": [150]}
},
{
"model": "taggit.taggeditem",
Expand Down Expand Up @@ -1606,7 +1630,8 @@ def test_import_model_with_untracked_deleted_reverse_related_models(self):
{
"model": "tests.advert",
"pk": 4,
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test", "tags": "[]", "tagged_items": []}
"fields": {"longadvert": null, "sponsoredpage": null, "slogan": "test",
"run_until": "2021-12-23T12:00:00Z", "run_from": null, "tags": "[]", "tagged_items": []}
}
]
}"""
Expand Down
19 changes: 16 additions & 3 deletions tests/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
from unittest import mock
from datetime import datetime, date, timezone

from django.test import TestCase

Expand Down Expand Up @@ -99,14 +100,18 @@ def test_run(self, get, post):
"model": "tests.advert",
"pk": 11,
"fields": {
"slogan": "put a leopard in your tank"
"slogan": "put a leopard in your tank",
"run_until": "2020-12-23T01:23:45Z",
"run_from": "2020-01-21"
}
},
{
"model": "tests.advert",
"pk": 8,
"fields": {
"slogan": "go to work on an egg"
"slogan": "go to work on an egg",
"run_until": "2020-01-23T01:23:45Z",
"run_from": null
}
}
]
Expand Down Expand Up @@ -137,10 +142,14 @@ def test_run(self, get, post):
updated_page = SponsoredPage.objects.get(url_path='/home/oil-is-still-great/')
self.assertEqual(updated_page.intro, "yay fossil fuels and climate change")
self.assertEqual(updated_page.advert.slogan, "put a leopard in your tank")
self.assertEqual(updated_page.advert.run_until, datetime(2020, 12, 23, 1, 23, 45, tzinfo=timezone.utc))
self.assertEqual(updated_page.advert.run_from, date(2020, 1, 21))

created_page = SponsoredPage.objects.get(url_path='/home/eggs-are-great-too/')
self.assertEqual(created_page.intro, "you can make cakes with them")
self.assertEqual(created_page.advert.slogan, "go to work on an egg")
self.assertEqual(created_page.advert.run_until, datetime(2020, 1, 23, 1, 23, 45, tzinfo=timezone.utc))
self.assertEqual(created_page.advert.run_from, None)

def test_missing_related_object(self, get, post):
# If an imported object contains references to an object which does not exist at the source
Expand Down Expand Up @@ -218,7 +227,9 @@ def test_missing_related_object(self, get, post):
"model": "tests.advert",
"pk": 11,
"fields": {
"slogan": "put a leopard in your tank"
"slogan": "put a leopard in your tank",
"run_until": "2020-12-23T01:23:45Z",
"run_from": null
}
}
]
Expand Down Expand Up @@ -249,6 +260,8 @@ def test_missing_related_object(self, get, post):
updated_page = SponsoredPage.objects.get(url_path='/home/oil-is-still-great/')
self.assertEqual(updated_page.intro, "yay fossil fuels and climate change")
self.assertEqual(updated_page.advert.slogan, "put a leopard in your tank")
self.assertEqual(updated_page.advert.run_until, datetime(2020, 12, 23, 1, 23, 45, tzinfo=timezone.utc))
self.assertEqual(updated_page.advert.run_from, None)

# The egg advert was missing in the object-api response, and the FK on SponsoredPage is
# nullable, so it should create the egg page without the advert
Expand Down
2 changes: 1 addition & 1 deletion wagtail_transfer/field_adapters.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def populate_field(self, instance, value, context):
as returned by `serialize`
"""
value = self.update_object_references(value, context.destination_ids_by_source)
setattr(instance, self.field.get_attname(), value)
setattr(instance, self.field.get_attname(), self.field.to_python(value))

def get_managed_fields(self):
"""
Expand Down

0 comments on commit cd9fd8e

Please sign in to comment.