[py3] Fixed serializers tests

This commit is contained in:
Claude Paroz 2012-08-14 19:48:38 +02:00
parent 7d48e077b5
commit f2fe7a3e36
3 changed files with 15 additions and 17 deletions

View File

@ -2,8 +2,6 @@
Module for abstract serializer/unserializer base classes. Module for abstract serializer/unserializer base classes.
""" """
from io import BytesIO
from django.db import models from django.db import models
from django.utils.encoding import smart_text from django.utils.encoding import smart_text
from django.utils import six from django.utils import six
@ -35,7 +33,7 @@ class Serializer(object):
""" """
self.options = options self.options = options
self.stream = options.pop("stream", BytesIO()) self.stream = options.pop("stream", six.StringIO())
self.selected_fields = options.pop("fields", None) self.selected_fields = options.pop("fields", None)
self.use_natural_keys = options.pop("use_natural_keys", False) self.use_natural_keys = options.pop("use_natural_keys", False)
@ -125,7 +123,7 @@ class Deserializer(object):
""" """
self.options = options self.options = options
if isinstance(stream_or_string, six.string_types): if isinstance(stream_or_string, six.string_types):
self.stream = BytesIO(stream_or_string) self.stream = six.StringIO(stream_or_string)
else: else:
self.stream = stream_or_string self.stream = stream_or_string
# hack to make sure that the models have all been loaded before # hack to make sure that the models have all been loaded before

View File

@ -114,8 +114,8 @@ class SerializersTestBase(object):
Tests the ability to create new objects by Tests the ability to create new objects by
modifying serialized content. modifying serialized content.
""" """
old_headline = b"Poker has no place on ESPN" old_headline = "Poker has no place on ESPN"
new_headline = b"Poker has no place on television" new_headline = "Poker has no place on television"
serial_str = serializers.serialize(self.serializer_name, serial_str = serializers.serialize(self.serializer_name,
Article.objects.all()) Article.objects.all())
serial_str = serial_str.replace(old_headline, new_headline) serial_str = serial_str.replace(old_headline, new_headline)
@ -285,7 +285,7 @@ class SerializersTransactionTestBase(object):
class XmlSerializerTestCase(SerializersTestBase, TestCase): class XmlSerializerTestCase(SerializersTestBase, TestCase):
serializer_name = "xml" serializer_name = "xml"
pkless_str = b"""<?xml version="1.0" encoding="utf-8"?> pkless_str = """<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0"> <django-objects version="1.0">
<object model="serializers.category"> <object model="serializers.category">
<field type="CharField" name="name">Reference</field> <field type="CharField" name="name">Reference</field>
@ -331,7 +331,7 @@ class XmlSerializerTestCase(SerializersTestBase, TestCase):
class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "xml" serializer_name = "xml"
fwd_ref_str = b"""<?xml version="1.0" encoding="utf-8"?> fwd_ref_str = """<?xml version="1.0" encoding="utf-8"?>
<django-objects version="1.0"> <django-objects version="1.0">
<object pk="1" model="serializers.article"> <object pk="1" model="serializers.article">
<field to="serializers.author" name="author" rel="ManyToOneRel">1</field> <field to="serializers.author" name="author" rel="ManyToOneRel">1</field>
@ -351,7 +351,7 @@ class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, Transacti
class JsonSerializerTestCase(SerializersTestBase, TestCase): class JsonSerializerTestCase(SerializersTestBase, TestCase):
serializer_name = "json" serializer_name = "json"
pkless_str = b"""[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]""" pkless_str = """[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]"""
@staticmethod @staticmethod
def _validate_output(serial_str): def _validate_output(serial_str):
@ -381,7 +381,7 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase):
class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "json" serializer_name = "json"
fwd_ref_str = b"""[ fwd_ref_str = """[
{ {
"pk": 1, "pk": 1,
"model": "serializers.article", "model": "serializers.article",
@ -414,7 +414,7 @@ except ImportError:
else: else:
class YamlSerializerTestCase(SerializersTestBase, TestCase): class YamlSerializerTestCase(SerializersTestBase, TestCase):
serializer_name = "yaml" serializer_name = "yaml"
fwd_ref_str = b"""- fields: fwd_ref_str = """- fields:
headline: Forward references pose no problem headline: Forward references pose no problem
pub_date: 2006-06-16 15:00:00 pub_date: 2006-06-16 15:00:00
categories: [1] categories: [1]
@ -430,7 +430,7 @@ else:
pk: 1 pk: 1
model: serializers.author""" model: serializers.author"""
pkless_str = b"""- fields: pkless_str = """- fields:
name: Reference name: Reference
pk: null pk: null
model: serializers.category""" model: serializers.category"""
@ -470,7 +470,7 @@ else:
class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "yaml" serializer_name = "yaml"
fwd_ref_str = b"""- fields: fwd_ref_str = """- fields:
headline: Forward references pose no problem headline: Forward references pose no problem
pub_date: 2006-06-16 15:00:00 pub_date: 2006-06-16 15:00:00
categories: [1] categories: [1]

View File

@ -10,7 +10,6 @@ from __future__ import absolute_import, unicode_literals
import datetime import datetime
import decimal import decimal
from io import BytesIO
try: try:
import yaml import yaml
@ -23,6 +22,7 @@ from django.core.serializers.base import DeserializationError
from django.db import connection, models from django.db import connection, models
from django.http import HttpResponse from django.http import HttpResponse
from django.test import TestCase from django.test import TestCase
from django.utils import six
from django.utils.functional import curry from django.utils.functional import curry
from django.utils.unittest import skipUnless from django.utils.unittest import skipUnless
@ -502,17 +502,17 @@ def streamTest(format, self):
obj.save_base(raw=True) obj.save_base(raw=True)
# Serialize the test database to a stream # Serialize the test database to a stream
for stream in (BytesIO(), HttpResponse()): for stream in (six.StringIO(), HttpResponse()):
serializers.serialize(format, [obj], indent=2, stream=stream) serializers.serialize(format, [obj], indent=2, stream=stream)
# Serialize normally for a comparison # Serialize normally for a comparison
string_data = serializers.serialize(format, [obj], indent=2) string_data = serializers.serialize(format, [obj], indent=2)
# Check that the two are the same # Check that the two are the same
if isinstance(stream, BytesIO): if isinstance(stream, six.StringIO):
self.assertEqual(string_data, stream.getvalue()) self.assertEqual(string_data, stream.getvalue())
else: else:
self.assertEqual(string_data, stream.content) self.assertEqual(string_data, stream.content.decode('utf-8'))
stream.close() stream.close()
for format in serializers.get_serializer_formats(): for format in serializers.get_serializer_formats():