2007-03-13 09:03:05 +08:00
|
|
|
"""
|
|
|
|
A test spanning all the capabilities of all the serializers.
|
|
|
|
|
|
|
|
This class defines sample data and a dynamically generated
|
2007-05-21 09:29:58 +08:00
|
|
|
test case that is capable of testing the capabilities of
|
2007-03-13 09:03:05 +08:00
|
|
|
the serializers. This includes all valid data values, plus
|
|
|
|
forward, backwards and self references.
|
|
|
|
"""
|
2024-01-26 19:45:07 +08:00
|
|
|
|
2010-02-24 23:54:03 +08:00
|
|
|
import datetime
|
|
|
|
import decimal
|
2015-02-12 22:54:42 +08:00
|
|
|
import uuid
|
2007-03-13 09:03:05 +08:00
|
|
|
|
2011-07-13 17:35:51 +08:00
|
|
|
from django.core import serializers
|
2011-10-14 05:34:56 +08:00
|
|
|
from django.db import connection, models
|
2015-09-26 18:46:00 +08:00
|
|
|
from django.test import TestCase
|
2007-03-13 09:03:05 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from .models import (
|
2015-09-26 20:14:23 +08:00
|
|
|
Anchor,
|
|
|
|
AutoNowDateTimeData,
|
|
|
|
BigIntegerData,
|
|
|
|
BinaryData,
|
|
|
|
BooleanData,
|
2017-06-06 00:59:22 +08:00
|
|
|
BooleanPKData,
|
|
|
|
CharData,
|
|
|
|
CharPKData,
|
|
|
|
DateData,
|
|
|
|
DatePKData,
|
|
|
|
DateTimeData,
|
|
|
|
DateTimePKData,
|
|
|
|
DecimalData,
|
|
|
|
DecimalPKData,
|
|
|
|
EmailData,
|
|
|
|
EmailPKData,
|
|
|
|
ExplicitInheritBaseModel,
|
|
|
|
FileData,
|
|
|
|
FilePathData,
|
|
|
|
FilePathPKData,
|
|
|
|
FKData,
|
|
|
|
FKDataToField,
|
|
|
|
FKDataToO2O,
|
|
|
|
FKSelfData,
|
|
|
|
FKToUUID,
|
|
|
|
FloatData,
|
|
|
|
FloatPKData,
|
|
|
|
GenericData,
|
|
|
|
GenericIPAddressData,
|
|
|
|
GenericIPAddressPKData,
|
|
|
|
InheritAbstractModel,
|
|
|
|
InheritBaseModel,
|
|
|
|
IntegerData,
|
|
|
|
IntegerPKData,
|
|
|
|
Intermediate,
|
|
|
|
LengthModel,
|
|
|
|
M2MData,
|
|
|
|
M2MIntermediateData,
|
|
|
|
M2MSelfData,
|
2021-01-14 04:28:09 +08:00
|
|
|
ModifyingSaveData,
|
|
|
|
O2OData,
|
|
|
|
PositiveBigIntegerData,
|
|
|
|
PositiveIntegerData,
|
2007-03-13 09:03:05 +08:00
|
|
|
PositiveIntegerPKData,
|
|
|
|
PositiveSmallIntegerData,
|
2021-01-14 04:28:09 +08:00
|
|
|
PositiveSmallIntegerPKData,
|
2017-06-06 00:59:22 +08:00
|
|
|
SlugData,
|
|
|
|
SlugPKData,
|
|
|
|
SmallData,
|
|
|
|
SmallPKData,
|
2019-12-12 11:34:28 +08:00
|
|
|
Tag,
|
|
|
|
TextData,
|
|
|
|
TimeData,
|
|
|
|
UniqueAnchor,
|
|
|
|
UUIDData,
|
|
|
|
UUIDDefaultData,
|
2015-01-28 20:35:27 +08:00
|
|
|
)
|
2015-09-26 20:14:23 +08:00
|
|
|
from .tests import register_tests
|
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
# A set of functions that can be used to recreate
|
2007-07-12 15:45:35 +08:00
|
|
|
# test data objects of various kinds.
|
|
|
|
# The save method is a raw base model save, to make
|
|
|
|
# sure that the data in the database matches the
|
|
|
|
# exact test case.
|
2013-11-03 05:34:05 +08:00
|
|
|
|
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def data_create(pk, klass, data):
|
|
|
|
instance = klass(id=pk)
|
|
|
|
instance.data = data
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
models.Model.save_base(instance, raw=True)
|
2008-06-09 22:03:35 +08:00
|
|
|
return [instance]
|
2007-03-19 19:57:53 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-19 19:57:53 +08:00
|
|
|
def generic_create(pk, klass, data):
|
|
|
|
instance = klass(id=pk)
|
|
|
|
instance.data = data[0]
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
models.Model.save_base(instance, raw=True)
|
2007-03-19 19:57:53 +08:00
|
|
|
for tag in data[1:]:
|
|
|
|
instance.tags.create(data=tag)
|
2008-06-09 22:03:35 +08:00
|
|
|
return [instance]
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def fk_create(pk, klass, data):
|
|
|
|
instance = klass(id=pk)
|
|
|
|
setattr(instance, "data_id", data)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
models.Model.save_base(instance, raw=True)
|
2008-06-09 22:03:35 +08:00
|
|
|
return [instance]
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def m2m_create(pk, klass, data):
|
|
|
|
instance = klass(id=pk)
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
models.Model.save_base(instance, raw=True)
|
2015-10-09 05:17:10 +08:00
|
|
|
instance.data.set(data)
|
2008-06-09 22:03:35 +08:00
|
|
|
return [instance]
|
2007-03-13 09:03:05 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-08-12 20:58:33 +08:00
|
|
|
def im2m_create(pk, klass, data):
|
|
|
|
instance = klass(id=pk)
|
|
|
|
models.Model.save_base(instance, raw=True)
|
|
|
|
return [instance]
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-08-12 20:58:33 +08:00
|
|
|
def im_create(pk, klass, data):
|
|
|
|
instance = klass(id=pk)
|
2010-05-28 17:52:24 +08:00
|
|
|
instance.right_id = data["right"]
|
|
|
|
instance.left_id = data["left"]
|
2008-08-12 20:58:33 +08:00
|
|
|
if "extra" in data:
|
2010-05-28 17:52:24 +08:00
|
|
|
instance.extra = data["extra"]
|
2008-08-12 20:58:33 +08:00
|
|
|
models.Model.save_base(instance, raw=True)
|
|
|
|
return [instance]
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def o2o_create(pk, klass, data):
|
|
|
|
instance = klass()
|
|
|
|
instance.data_id = data
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
models.Model.save_base(instance, raw=True)
|
2008-06-09 22:03:35 +08:00
|
|
|
return [instance]
|
2007-03-13 09:03:05 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def pk_create(pk, klass, data):
|
|
|
|
instance = klass()
|
|
|
|
instance.data = data
|
Merged the queryset-refactor branch into trunk.
This is a big internal change, but mostly backwards compatible with existing
code. Also adds a couple of new features.
Fixed #245, #1050, #1656, #1801, #2076, #2091, #2150, #2253, #2306, #2400, #2430, #2482, #2496, #2676, #2737, #2874, #2902, #2939, #3037, #3141, #3288, #3440, #3592, #3739, #4088, #4260, #4289, #4306, #4358, #4464, #4510, #4858, #5012, #5020, #5261, #5295, #5321, #5324, #5325, #5555, #5707, #5796, #5817, #5987, #6018, #6074, #6088, #6154, #6177, #6180, #6203, #6658
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7477 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2008-04-27 10:50:16 +08:00
|
|
|
models.Model.save_base(instance, raw=True)
|
2008-06-09 22:03:35 +08:00
|
|
|
return [instance]
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
def inherited_create(pk, klass, data):
|
2013-10-27 03:15:03 +08:00
|
|
|
instance = klass(id=pk, **data)
|
2008-06-09 22:03:35 +08:00
|
|
|
# This isn't a raw save because:
|
2011-12-18 01:37:24 +08:00
|
|
|
# 1) we're testing inheritance, not field behavior, so none
|
2008-06-09 22:03:35 +08:00
|
|
|
# of the field values need to be protected.
|
|
|
|
# 2) saving the child class and having the parent created
|
2008-07-27 11:32:44 +08:00
|
|
|
# automatically is easier than manually creating both.
|
2008-06-09 22:03:35 +08:00
|
|
|
models.Model.save(instance)
|
|
|
|
created = [instance]
|
2017-12-07 06:17:59 +08:00
|
|
|
for klass in instance._meta.parents:
|
2008-06-09 22:03:35 +08:00
|
|
|
created.append(klass.objects.get(id=pk))
|
|
|
|
return created
|
2008-07-27 11:32:44 +08:00
|
|
|
|
2022-02-04 03:24:19 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
# A set of functions that can be used to compare
|
|
|
|
# test data objects of various kinds
|
2013-11-03 05:34:05 +08:00
|
|
|
|
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def data_compare(testcase, pk, klass, data):
|
|
|
|
instance = klass.objects.get(id=pk)
|
2012-12-15 07:26:08 +08:00
|
|
|
if klass == BinaryData and data is not None:
|
2016-04-08 10:04:45 +08:00
|
|
|
testcase.assertEqual(
|
|
|
|
bytes(data),
|
|
|
|
bytes(instance.data),
|
|
|
|
"Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)"
|
|
|
|
% (
|
|
|
|
pk,
|
|
|
|
repr(bytes(data)),
|
|
|
|
type(data),
|
|
|
|
repr(bytes(instance.data)),
|
|
|
|
type(instance.data),
|
|
|
|
),
|
2012-12-15 07:26:08 +08:00
|
|
|
)
|
|
|
|
else:
|
2016-04-08 10:04:45 +08:00
|
|
|
testcase.assertEqual(
|
|
|
|
data,
|
|
|
|
instance.data,
|
|
|
|
"Objects with PK=%d not equal; expected '%s' (%s), got '%s' (%s)"
|
|
|
|
% (
|
|
|
|
pk,
|
|
|
|
data,
|
|
|
|
type(data),
|
|
|
|
instance,
|
|
|
|
type(instance.data),
|
|
|
|
),
|
2012-12-15 07:26:08 +08:00
|
|
|
)
|
2007-03-13 09:03:05 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-19 19:57:53 +08:00
|
|
|
def generic_compare(testcase, pk, klass, data):
|
|
|
|
instance = klass.objects.get(id=pk)
|
|
|
|
testcase.assertEqual(data[0], instance.data)
|
2009-06-29 20:29:48 +08:00
|
|
|
testcase.assertEqual(data[1:], [t.data for t in instance.tags.order_by("id")])
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def fk_compare(testcase, pk, klass, data):
|
|
|
|
instance = klass.objects.get(id=pk)
|
|
|
|
testcase.assertEqual(data, instance.data_id)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def m2m_compare(testcase, pk, klass, data):
|
|
|
|
instance = klass.objects.get(id=pk)
|
2009-06-29 20:29:48 +08:00
|
|
|
testcase.assertEqual(data, [obj.id for obj in instance.data.order_by("id")])
|
2007-03-13 09:03:05 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-08-12 20:58:33 +08:00
|
|
|
def im2m_compare(testcase, pk, klass, data):
|
2013-10-19 20:31:38 +08:00
|
|
|
klass.objects.get(id=pk)
|
|
|
|
# actually nothing else to check, the instance just should exist
|
2008-08-12 20:58:33 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-08-12 20:58:33 +08:00
|
|
|
def im_compare(testcase, pk, klass, data):
|
|
|
|
instance = klass.objects.get(id=pk)
|
|
|
|
testcase.assertEqual(data["left"], instance.left_id)
|
|
|
|
testcase.assertEqual(data["right"], instance.right_id)
|
|
|
|
if "extra" in data:
|
|
|
|
testcase.assertEqual(data["extra"], instance.extra)
|
|
|
|
else:
|
|
|
|
testcase.assertEqual("doesn't matter", instance.extra)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def o2o_compare(testcase, pk, klass, data):
|
|
|
|
instance = klass.objects.get(data=data)
|
|
|
|
testcase.assertEqual(data, instance.data_id)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
def pk_compare(testcase, pk, klass, data):
|
|
|
|
instance = klass.objects.get(data=data)
|
|
|
|
testcase.assertEqual(data, instance.data)
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
def inherited_compare(testcase, pk, klass, data):
|
|
|
|
instance = klass.objects.get(id=pk)
|
2013-10-27 03:15:03 +08:00
|
|
|
for key, value in data.items():
|
|
|
|
testcase.assertEqual(value, getattr(instance, key))
|
2008-07-27 11:32:44 +08:00
|
|
|
|
2016-11-13 01:11:23 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
# Define some data types. Each data type is
|
|
|
|
# actually a pair of functions; one to create
|
|
|
|
# and one to compare objects of that type
|
|
|
|
data_obj = (data_create, data_compare)
|
2007-03-19 19:57:53 +08:00
|
|
|
generic_obj = (generic_create, generic_compare)
|
2007-03-13 09:03:05 +08:00
|
|
|
fk_obj = (fk_create, fk_compare)
|
|
|
|
m2m_obj = (m2m_create, m2m_compare)
|
2008-08-12 20:58:33 +08:00
|
|
|
im2m_obj = (im2m_create, im2m_compare)
|
|
|
|
im_obj = (im_create, im_compare)
|
2007-03-13 09:03:05 +08:00
|
|
|
o2o_obj = (o2o_create, o2o_compare)
|
|
|
|
pk_obj = (pk_create, pk_compare)
|
2008-06-09 22:03:35 +08:00
|
|
|
inherited_obj = (inherited_create, inherited_compare)
|
2015-02-13 22:21:33 +08:00
|
|
|
uuid_obj = uuid.uuid4()
|
2007-03-13 09:03:05 +08:00
|
|
|
|
|
|
|
test_data = [
|
2007-05-21 09:29:58 +08:00
|
|
|
# Format: (data type, PK value, Model Class, data)
|
2017-01-07 19:11:46 +08:00
|
|
|
(data_obj, 1, BinaryData, memoryview(b"\x05\xFD\x00")),
|
2012-12-15 07:26:08 +08:00
|
|
|
(data_obj, 2, BinaryData, None),
|
|
|
|
(data_obj, 5, BooleanData, True),
|
|
|
|
(data_obj, 6, BooleanData, False),
|
2017-05-06 22:56:28 +08:00
|
|
|
(data_obj, 7, BooleanData, None),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 10, CharData, "Test Char Data"),
|
|
|
|
(data_obj, 11, CharData, ""),
|
|
|
|
(data_obj, 12, CharData, "None"),
|
|
|
|
(data_obj, 13, CharData, "null"),
|
|
|
|
(data_obj, 14, CharData, "NULL"),
|
|
|
|
(data_obj, 15, CharData, None),
|
Merged Unicode branch into trunk (r4952:5608). This should be fully
backwards compatible for all practical purposes.
Fixed #2391, #2489, #2996, #3322, #3344, #3370, #3406, #3432, #3454, #3492, #3582, #3690, #3878, #3891, #3937, #4039, #4141, #4227, #4286, #4291, #4300, #4452, #4702
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5609 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2007-07-04 20:11:04 +08:00
|
|
|
# (We use something that will fit into a latin1 database encoding here,
|
|
|
|
# because that is still the default used on many system setups.)
|
2012-06-08 00:08:47 +08:00
|
|
|
(data_obj, 16, CharData, "\xa5"),
|
2013-10-27 03:15:03 +08:00
|
|
|
(data_obj, 20, DateData, datetime.date(2006, 6, 16)),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 21, DateData, None),
|
2013-10-27 03:15:03 +08:00
|
|
|
(data_obj, 30, DateTimeData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 31, DateTimeData, None),
|
|
|
|
(data_obj, 40, EmailData, "hovercraft@example.com"),
|
|
|
|
(data_obj, 41, EmailData, None),
|
2007-06-23 22:16:00 +08:00
|
|
|
(data_obj, 42, EmailData, ""),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 50, FileData, "file:///foo/bar/whiz.txt"),
|
2013-10-18 06:27:45 +08:00
|
|
|
# (data_obj, 51, FileData, None),
|
2007-06-23 22:16:00 +08:00
|
|
|
(data_obj, 52, FileData, ""),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 60, FilePathData, "/foo/bar/whiz.txt"),
|
|
|
|
(data_obj, 61, FilePathData, None),
|
2007-06-23 22:16:00 +08:00
|
|
|
(data_obj, 62, FilePathData, ""),
|
2007-05-21 09:29:58 +08:00
|
|
|
(data_obj, 70, DecimalData, decimal.Decimal("12.345")),
|
|
|
|
(data_obj, 71, DecimalData, decimal.Decimal("-12.345")),
|
|
|
|
(data_obj, 72, DecimalData, decimal.Decimal("0.0")),
|
|
|
|
(data_obj, 73, DecimalData, None),
|
|
|
|
(data_obj, 74, FloatData, 12.345),
|
|
|
|
(data_obj, 75, FloatData, -12.345),
|
|
|
|
(data_obj, 76, FloatData, 0.0),
|
|
|
|
(data_obj, 77, FloatData, None),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 80, IntegerData, 123456789),
|
|
|
|
(data_obj, 81, IntegerData, -123456789),
|
|
|
|
(data_obj, 82, IntegerData, 0),
|
|
|
|
(data_obj, 83, IntegerData, None),
|
2015-02-06 02:25:34 +08:00
|
|
|
# (XX, ImageData
|
2011-06-11 21:48:24 +08:00
|
|
|
(data_obj, 95, GenericIPAddressData, "fe80:1424:2223:6cff:fe8a:2e8a:2151:abcd"),
|
|
|
|
(data_obj, 96, GenericIPAddressData, None),
|
2019-10-16 20:32:12 +08:00
|
|
|
(data_obj, 110, PositiveBigIntegerData, 9223372036854775807),
|
|
|
|
(data_obj, 111, PositiveBigIntegerData, None),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 120, PositiveIntegerData, 123456789),
|
|
|
|
(data_obj, 121, PositiveIntegerData, None),
|
|
|
|
(data_obj, 130, PositiveSmallIntegerData, 12),
|
|
|
|
(data_obj, 131, PositiveSmallIntegerData, None),
|
|
|
|
(data_obj, 140, SlugData, "this-is-a-slug"),
|
|
|
|
(data_obj, 141, SlugData, None),
|
2007-06-23 22:16:00 +08:00
|
|
|
(data_obj, 142, SlugData, ""),
|
2007-05-21 09:29:58 +08:00
|
|
|
(data_obj, 150, SmallData, 12),
|
|
|
|
(data_obj, 151, SmallData, -12),
|
|
|
|
(data_obj, 152, SmallData, 0),
|
|
|
|
(data_obj, 153, SmallData, None),
|
2007-03-13 09:03:05 +08:00
|
|
|
(
|
|
|
|
data_obj,
|
|
|
|
160,
|
|
|
|
TextData,
|
|
|
|
"""This is a long piece of text.
|
|
|
|
It contains line breaks.
|
|
|
|
Several of them.
|
|
|
|
The end.""",
|
|
|
|
),
|
|
|
|
(data_obj, 161, TextData, ""),
|
|
|
|
(data_obj, 162, TextData, None),
|
2013-10-27 03:15:03 +08:00
|
|
|
(data_obj, 170, TimeData, datetime.time(10, 42, 37)),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 171, TimeData, None),
|
2007-03-19 19:57:53 +08:00
|
|
|
(generic_obj, 200, GenericData, ["Generic Object 1", "tag1", "tag2"]),
|
|
|
|
(generic_obj, 201, GenericData, ["Generic Object 2", "tag2", "tag3"]),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 300, Anchor, "Anchor 1"),
|
|
|
|
(data_obj, 301, Anchor, "Anchor 2"),
|
2007-05-14 22:21:00 +08:00
|
|
|
(data_obj, 302, UniqueAnchor, "UAnchor 1"),
|
2013-11-03 05:34:05 +08:00
|
|
|
(fk_obj, 400, FKData, 300), # Post reference
|
|
|
|
(fk_obj, 401, FKData, 500), # Pre reference
|
|
|
|
(fk_obj, 402, FKData, None), # Empty reference
|
|
|
|
(m2m_obj, 410, M2MData, []), # Empty set
|
|
|
|
(m2m_obj, 411, M2MData, [300, 301]), # Post reference
|
|
|
|
(m2m_obj, 412, M2MData, [500, 501]), # Pre reference
|
|
|
|
(m2m_obj, 413, M2MData, [300, 301, 500, 501]), # Pre and Post reference
|
|
|
|
(o2o_obj, None, O2OData, 300), # Post reference
|
|
|
|
(o2o_obj, None, O2OData, 500), # Pre reference
|
|
|
|
(fk_obj, 430, FKSelfData, 431), # Pre reference
|
|
|
|
(fk_obj, 431, FKSelfData, 430), # Post reference
|
|
|
|
(fk_obj, 432, FKSelfData, None), # Empty reference
|
2007-03-13 09:03:05 +08:00
|
|
|
(m2m_obj, 440, M2MSelfData, []),
|
|
|
|
(m2m_obj, 441, M2MSelfData, []),
|
|
|
|
(m2m_obj, 442, M2MSelfData, [440, 441]),
|
|
|
|
(m2m_obj, 443, M2MSelfData, [445, 446]),
|
|
|
|
(m2m_obj, 444, M2MSelfData, [440, 441, 445, 446]),
|
|
|
|
(m2m_obj, 445, M2MSelfData, []),
|
|
|
|
(m2m_obj, 446, M2MSelfData, []),
|
2007-05-14 22:21:00 +08:00
|
|
|
(fk_obj, 450, FKDataToField, "UAnchor 1"),
|
|
|
|
(fk_obj, 451, FKDataToField, "UAnchor 2"),
|
|
|
|
(fk_obj, 452, FKDataToField, None),
|
2007-05-28 13:41:32 +08:00
|
|
|
(fk_obj, 460, FKDataToO2O, 300),
|
2008-08-12 20:58:33 +08:00
|
|
|
(im2m_obj, 470, M2MIntermediateData, None),
|
2015-12-03 07:55:50 +08:00
|
|
|
# testing post- and pre-references and extra fields
|
2008-08-12 20:58:33 +08:00
|
|
|
(im_obj, 480, Intermediate, {"right": 300, "left": 470}),
|
2009-12-22 23:18:51 +08:00
|
|
|
(im_obj, 481, Intermediate, {"right": 300, "left": 490}),
|
|
|
|
(im_obj, 482, Intermediate, {"right": 500, "left": 470}),
|
|
|
|
(im_obj, 483, Intermediate, {"right": 500, "left": 490}),
|
|
|
|
(im_obj, 484, Intermediate, {"right": 300, "left": 470, "extra": "extra"}),
|
|
|
|
(im_obj, 485, Intermediate, {"right": 300, "left": 490, "extra": "extra"}),
|
|
|
|
(im_obj, 486, Intermediate, {"right": 500, "left": 470, "extra": "extra"}),
|
|
|
|
(im_obj, 487, Intermediate, {"right": 500, "left": 490, "extra": "extra"}),
|
2008-08-12 20:58:33 +08:00
|
|
|
(im2m_obj, 490, M2MIntermediateData, []),
|
2007-03-13 09:03:05 +08:00
|
|
|
(data_obj, 500, Anchor, "Anchor 3"),
|
|
|
|
(data_obj, 501, Anchor, "Anchor 4"),
|
2007-05-14 22:21:00 +08:00
|
|
|
(data_obj, 502, UniqueAnchor, "UAnchor 2"),
|
2007-03-13 09:03:05 +08:00
|
|
|
(pk_obj, 601, BooleanPKData, True),
|
|
|
|
(pk_obj, 602, BooleanPKData, False),
|
|
|
|
(pk_obj, 610, CharPKData, "Test Char PKData"),
|
2017-06-06 00:59:22 +08:00
|
|
|
(pk_obj, 620, DatePKData, datetime.date(2006, 6, 16)),
|
|
|
|
(pk_obj, 630, DateTimePKData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
|
2007-03-13 09:03:05 +08:00
|
|
|
(pk_obj, 640, EmailPKData, "hovercraft@example.com"),
|
2013-10-18 06:27:45 +08:00
|
|
|
# (pk_obj, 650, FilePKData, 'file:///foo/bar/whiz.txt'),
|
2007-03-13 09:03:05 +08:00
|
|
|
(pk_obj, 660, FilePathPKData, "/foo/bar/whiz.txt"),
|
2007-05-21 09:29:58 +08:00
|
|
|
(pk_obj, 670, DecimalPKData, decimal.Decimal("12.345")),
|
|
|
|
(pk_obj, 671, DecimalPKData, decimal.Decimal("-12.345")),
|
|
|
|
(pk_obj, 672, DecimalPKData, decimal.Decimal("0.0")),
|
|
|
|
(pk_obj, 673, FloatPKData, 12.345),
|
|
|
|
(pk_obj, 674, FloatPKData, -12.345),
|
|
|
|
(pk_obj, 675, FloatPKData, 0.0),
|
2007-03-13 09:03:05 +08:00
|
|
|
(pk_obj, 680, IntegerPKData, 123456789),
|
|
|
|
(pk_obj, 681, IntegerPKData, -123456789),
|
|
|
|
(pk_obj, 682, IntegerPKData, 0),
|
2013-10-18 06:27:45 +08:00
|
|
|
# (XX, ImagePKData
|
2011-06-11 21:48:24 +08:00
|
|
|
(pk_obj, 695, GenericIPAddressPKData, "fe80:1424:2223:6cff:fe8a:2e8a:2151:abcd"),
|
2007-03-13 09:03:05 +08:00
|
|
|
(pk_obj, 720, PositiveIntegerPKData, 123456789),
|
|
|
|
(pk_obj, 730, PositiveSmallIntegerPKData, 12),
|
|
|
|
(pk_obj, 740, SlugPKData, "this-is-a-slug"),
|
2007-05-21 09:29:58 +08:00
|
|
|
(pk_obj, 750, SmallPKData, 12),
|
|
|
|
(pk_obj, 751, SmallPKData, -12),
|
|
|
|
(pk_obj, 752, SmallPKData, 0),
|
2013-10-18 06:27:45 +08:00
|
|
|
# (pk_obj, 760, TextPKData, """This is a long piece of text.
|
|
|
|
# It contains line breaks.
|
|
|
|
# Several of them.
|
|
|
|
# The end."""),
|
2017-09-18 15:34:21 +08:00
|
|
|
# (pk_obj, 770, TimePKData, datetime.time(10, 42, 37)),
|
2013-10-18 06:27:45 +08:00
|
|
|
# (pk_obj, 790, XMLPKData, "<foo></foo>"),
|
2015-02-13 22:21:33 +08:00
|
|
|
(pk_obj, 791, UUIDData, uuid_obj),
|
|
|
|
(fk_obj, 792, FKToUUID, uuid_obj),
|
2019-12-12 11:34:28 +08:00
|
|
|
(pk_obj, 793, UUIDDefaultData, uuid_obj),
|
2013-10-27 03:15:03 +08:00
|
|
|
(data_obj, 800, AutoNowDateTimeData, datetime.datetime(2006, 6, 16, 10, 42, 37)),
|
2007-07-12 15:45:35 +08:00
|
|
|
(data_obj, 810, ModifyingSaveData, 42),
|
2013-10-27 03:15:03 +08:00
|
|
|
(inherited_obj, 900, InheritAbstractModel, {"child_data": 37, "parent_data": 42}),
|
|
|
|
(
|
|
|
|
inherited_obj,
|
|
|
|
910,
|
|
|
|
ExplicitInheritBaseModel,
|
|
|
|
{"child_data": 37, "parent_data": 42},
|
|
|
|
),
|
|
|
|
(inherited_obj, 920, InheritBaseModel, {"child_data": 37, "parent_data": 42}),
|
2009-12-17 23:10:38 +08:00
|
|
|
(data_obj, 1000, BigIntegerData, 9223372036854775807),
|
|
|
|
(data_obj, 1001, BigIntegerData, -9223372036854775808),
|
|
|
|
(data_obj, 1002, BigIntegerData, 0),
|
|
|
|
(data_obj, 1003, BigIntegerData, None),
|
2010-02-24 23:54:03 +08:00
|
|
|
(data_obj, 1004, LengthModel, 0),
|
|
|
|
(data_obj, 1005, LengthModel, 1),
|
2007-03-13 09:03:05 +08:00
|
|
|
]
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2012-03-13 03:41:31 +08:00
|
|
|
|
2007-06-23 22:16:00 +08:00
|
|
|
# Because Oracle treats the empty string as NULL, Oracle is expected to fail
|
|
|
|
# when field.empty_strings_allowed is True and the value is None; skip these
|
|
|
|
# tests.
|
2010-10-11 20:55:17 +08:00
|
|
|
if connection.features.interprets_empty_strings_as_nulls:
|
2007-06-23 22:16:00 +08:00
|
|
|
test_data = [
|
|
|
|
data
|
|
|
|
for data in test_data
|
|
|
|
if not (
|
|
|
|
data[0] == data_obj
|
|
|
|
and data[2]._meta.get_field("data").empty_strings_allowed
|
|
|
|
and data[3] is None
|
2022-02-04 03:24:19 +08:00
|
|
|
)
|
2007-06-23 22:16:00 +08:00
|
|
|
]
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2015-09-26 20:14:23 +08:00
|
|
|
class SerializerDataTests(TestCase):
|
2015-09-26 18:46:00 +08:00
|
|
|
pass
|
2012-02-10 02:56:58 +08:00
|
|
|
|
2015-09-26 20:14:23 +08:00
|
|
|
|
2017-09-07 01:11:18 +08:00
|
|
|
def serializerTest(self, format):
|
2020-08-15 04:07:37 +08:00
|
|
|
# FK to an object with PK of 0. This won't work on MySQL without the
|
|
|
|
# NO_AUTO_VALUE_ON_ZERO SQL mode since it won't let you create an object
|
|
|
|
# with an autoincrement primary key of 0.
|
|
|
|
if connection.features.allows_auto_pk_0:
|
|
|
|
test_data.extend(
|
|
|
|
[
|
|
|
|
(data_obj, 0, Anchor, "Anchor 0"),
|
|
|
|
(fk_obj, 465, FKData, 0),
|
|
|
|
]
|
|
|
|
)
|
2007-03-13 09:03:05 +08:00
|
|
|
|
|
|
|
# Create all the objects defined in the test data
|
|
|
|
objects = []
|
2008-06-09 22:03:35 +08:00
|
|
|
instance_count = {}
|
2010-05-28 17:52:24 +08:00
|
|
|
for func, pk, klass, datum in test_data:
|
2011-08-07 08:43:26 +08:00
|
|
|
with connection.constraint_checks_disabled():
|
|
|
|
objects.extend(func[0](pk, klass, datum))
|
2007-03-13 09:03:05 +08:00
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
# Get a count of the number of objects created for each class
|
|
|
|
for klass in instance_count:
|
|
|
|
instance_count[klass] = klass.objects.count()
|
2008-07-27 11:32:44 +08:00
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
# Add the generic tagged objects to the object list
|
2007-03-19 19:57:53 +08:00
|
|
|
objects.extend(Tag.objects.all())
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2007-03-13 09:03:05 +08:00
|
|
|
# Serialize the test database
|
|
|
|
serialized_data = serializers.serialize(format, objects, indent=2)
|
|
|
|
|
2010-05-28 17:52:24 +08:00
|
|
|
for obj in serializers.deserialize(format, serialized_data):
|
|
|
|
obj.save()
|
2007-03-13 09:03:05 +08:00
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
# Assert that the deserialized data is the same
|
2007-03-13 09:03:05 +08:00
|
|
|
# as the original source
|
|
|
|
for func, pk, klass, datum in test_data:
|
|
|
|
func[1](self, pk, klass, datum)
|
2007-05-21 09:29:58 +08:00
|
|
|
|
2008-06-09 22:03:35 +08:00
|
|
|
# Assert that the number of objects deserialized is the
|
|
|
|
# same as the number that was serialized.
|
|
|
|
for klass, count in instance_count.items():
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(count, klass.objects.count())
|
2008-06-09 22:03:35 +08:00
|
|
|
|
2013-05-08 21:33:02 +08:00
|
|
|
|
2015-09-26 20:14:23 +08:00
|
|
|
register_tests(SerializerDataTests, "test_%s_serializer", serializerTest)
|