[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.
"""
from io import BytesIO
from django.db import models
from django.utils.encoding import smart_text
from django.utils import six
@ -35,7 +33,7 @@ class Serializer(object):
"""
self.options = options
self.stream = options.pop("stream", BytesIO())
self.stream = options.pop("stream", six.StringIO())
self.selected_fields = options.pop("fields", None)
self.use_natural_keys = options.pop("use_natural_keys", False)
@ -125,7 +123,7 @@ class Deserializer(object):
"""
self.options = options
if isinstance(stream_or_string, six.string_types):
self.stream = BytesIO(stream_or_string)
self.stream = six.StringIO(stream_or_string)
else:
self.stream = stream_or_string
# 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
modifying serialized content.
"""
old_headline = b"Poker has no place on ESPN"
new_headline = b"Poker has no place on television"
old_headline = "Poker has no place on ESPN"
new_headline = "Poker has no place on television"
serial_str = serializers.serialize(self.serializer_name,
Article.objects.all())
serial_str = serial_str.replace(old_headline, new_headline)
@ -285,7 +285,7 @@ class SerializersTransactionTestBase(object):
class XmlSerializerTestCase(SerializersTestBase, TestCase):
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">
<object model="serializers.category">
<field type="CharField" name="name">Reference</field>
@ -331,7 +331,7 @@ class XmlSerializerTestCase(SerializersTestBase, TestCase):
class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
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">
<object pk="1" model="serializers.article">
<field to="serializers.author" name="author" rel="ManyToOneRel">1</field>
@ -351,7 +351,7 @@ class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, Transacti
class JsonSerializerTestCase(SerializersTestBase, TestCase):
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
def _validate_output(serial_str):
@ -381,7 +381,7 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase):
class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "json"
fwd_ref_str = b"""[
fwd_ref_str = """[
{
"pk": 1,
"model": "serializers.article",
@ -414,7 +414,7 @@ except ImportError:
else:
class YamlSerializerTestCase(SerializersTestBase, TestCase):
serializer_name = "yaml"
fwd_ref_str = b"""- fields:
fwd_ref_str = """- fields:
headline: Forward references pose no problem
pub_date: 2006-06-16 15:00:00
categories: [1]
@ -430,7 +430,7 @@ else:
pk: 1
model: serializers.author"""
pkless_str = b"""- fields:
pkless_str = """- fields:
name: Reference
pk: null
model: serializers.category"""
@ -470,7 +470,7 @@ else:
class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "yaml"
fwd_ref_str = b"""- fields:
fwd_ref_str = """- fields:
headline: Forward references pose no problem
pub_date: 2006-06-16 15:00:00
categories: [1]

View File

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