[1.2.X] Fixed #14823 -- Corrected bootstrapping problems with register_serializers. Thanks to miker985@uw.edu for the report and draft patch.
Backport of r15336 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15339 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
81f844afb1
commit
407208b0c2
@ -48,6 +48,8 @@ def register_serializer(format, serializer_module, serializers=None):
|
||||
directly into the global register of serializers. Adding serializers
|
||||
directly is not a thread-safe operation.
|
||||
"""
|
||||
if serializers is None and not _serializers:
|
||||
_load_serializers()
|
||||
module = importlib.import_module(serializer_module)
|
||||
if serializers is None:
|
||||
_serializers[format] = module
|
||||
@ -56,6 +58,8 @@ def register_serializer(format, serializer_module, serializers=None):
|
||||
|
||||
def unregister_serializer(format):
|
||||
"Unregister a given serializer. This is not a thread-safe operation."
|
||||
if not _serializers:
|
||||
_load_serializers()
|
||||
del _serializers[format]
|
||||
|
||||
def get_serializer(format):
|
||||
|
@ -1,8 +1,10 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
from StringIO import StringIO
|
||||
import unittest
|
||||
from xml.dom import minidom
|
||||
|
||||
from django.conf import settings
|
||||
from django.core import serializers
|
||||
from django.db import transaction
|
||||
from django.test import TestCase, TransactionTestCase, Approximate
|
||||
@ -11,6 +13,56 @@ from django.utils import simplejson
|
||||
from models import Category, Author, Article, AuthorProfile, Actor, \
|
||||
Movie, Score, Player, Team
|
||||
|
||||
class SerializerRegistrationTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.old_SERIALIZATION_MODULES = getattr(settings, 'SERIALIZATION_MODULES', None)
|
||||
self.old_serializers = serializers._serializers
|
||||
|
||||
serializers._serializers = {}
|
||||
settings.SERIALIZATION_MODULES = {
|
||||
"json2" : "django.core.serializers.json",
|
||||
}
|
||||
|
||||
def tearDown(self):
|
||||
serializers._serializers = self.old_serializers
|
||||
if self.old_SERIALIZATION_MODULES:
|
||||
settings.SERIALIZATION_MODULES = self.old_SERIALIZATION_MODULES
|
||||
else:
|
||||
delattr(settings, 'SERIALIZATION_MODULES')
|
||||
|
||||
def test_register(self):
|
||||
"Registering a new serializer populates the full registry. Refs #14823"
|
||||
serializers.register_serializer('json3', 'django.core.serializers.json')
|
||||
|
||||
public_formats = serializers.get_public_serializer_formats()
|
||||
self.assertTrue('json3' in public_formats)
|
||||
self.assertTrue('json2' in public_formats)
|
||||
self.assertTrue('xml' in public_formats)
|
||||
|
||||
def test_unregister(self):
|
||||
"Unregistering a serializer doesn't cause the registry to be repopulated. Refs #14823"
|
||||
serializers.unregister_serializer('xml')
|
||||
serializers.register_serializer('json3', 'django.core.serializers.json')
|
||||
|
||||
public_formats = serializers.get_public_serializer_formats()
|
||||
|
||||
self.assertFalse('xml' in public_formats)
|
||||
self.assertTrue('json3' in public_formats)
|
||||
|
||||
def test_builtin_serializers(self):
|
||||
"Requesting a list of serializer formats popuates the registry"
|
||||
all_formats = set(serializers.get_serializer_formats())
|
||||
public_formats = set(serializers.get_public_serializer_formats())
|
||||
|
||||
self.assertTrue('xml' in all_formats),
|
||||
self.assertTrue('xml' in public_formats)
|
||||
|
||||
self.assertTrue('json2' in all_formats)
|
||||
self.assertTrue('json2' in public_formats)
|
||||
|
||||
self.assertTrue('python' in all_formats)
|
||||
self.assertFalse('python' in public_formats)
|
||||
|
||||
class SerializersTestBase(object):
|
||||
@staticmethod
|
||||
def _comparison_value(value):
|
||||
|
Loading…
x
Reference in New Issue
Block a user