summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRussell Keith-Magee <russell@keith-magee.com>2011-01-27 02:29:17 +0000
committerRussell Keith-Magee <russell@keith-magee.com>2011-01-27 02:29:17 +0000
commit4a3ea263ef964a7fb7840544870834889f56f7e4 (patch)
treecb58ab749ff16137ed32bde5b348823267f6c54a
parent456b8a9d0c3c4cbff2e3d88b659c6e4e3e6c7def (diff)
Fixed #14823 -- Corrected bootstrapping problems with register_serializers. Thanks to miker985@uw.edu for the report and draft patch.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15336 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/core/serializers/__init__.py4
-rw-r--r--tests/modeltests/serializers/tests.py53
2 files changed, 56 insertions, 1 deletions
diff --git a/django/core/serializers/__init__.py b/django/core/serializers/__init__.py
index bed1fa20dc..7afc94c1d0 100644
--- a/django/core/serializers/__init__.py
+++ b/django/core/serializers/__init__.py
@@ -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):
diff --git a/tests/modeltests/serializers/tests.py b/tests/modeltests/serializers/tests.py
index 9b648a8d4e..01b4b68ac2 100644
--- a/tests/modeltests/serializers/tests.py
+++ b/tests/modeltests/serializers/tests.py
@@ -3,14 +3,65 @@ from datetime import datetime
from StringIO import StringIO
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
-from django.utils import simplejson
+from django.utils import simplejson, unittest
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.assertIn('json3', public_formats)
+ self.assertIn('json2', public_formats)
+ self.assertIn('xml', 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.assertNotIn('xml', public_formats)
+ self.assertIn('json3', 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.assertIn('xml', all_formats),
+ self.assertIn('xml', public_formats)
+
+ self.assertIn('json2', all_formats)
+ self.assertIn('json2', public_formats)
+
+ self.assertIn('python', all_formats)
+ self.assertNotIn('python', public_formats)
+
class SerializersTestBase(object):
@staticmethod
def _comparison_value(value):