Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated NoYamlSerializerTestCase to run with yaml.

In order to verify the behavior of using the yaml serializer when yaml
is on the system, fake the ImportError when the serializer is being
registered.
  • Loading branch information...
commit ca3ac4a3e37cc844bef7f7553c6b1a6cc642e6e6 1 parent 076cf13
@rca rca authored
Showing with 45 additions and 2 deletions.
  1. +45 −2 tests/serializers/tests.py
View
47 tests/serializers/tests.py
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
+import importlib
import json
from datetime import datetime
import re
@@ -440,12 +441,54 @@ class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, Transact
}]"""
-@unittest.skipIf(HAS_YAML, "Yaml is installed")
+YAML_IMPORT_ERROR_MESSAGE = r'No module named yaml'
+class YamlImportModuleMock(object):
+ """Provides a wrapped import_module function to simulate yaml ImportError
+
+ In order to run tests that verify the behavior of the YAML serializer
+ when run on a system that has yaml installed (like the django CI server),
+ mock import_module, so that it raises an ImportError when the yaml
+ serializer is being imported. The importlib.import_module() call is
+ being made in the serializers.register_serializer().
+
+ #12756
+ """
+ def __init__(self):
+ self._import_module = importlib.import_module
+
+ def import_module(self, module_path):
+ if module_path == serializers.BUILTIN_SERIALIZERS['yaml']:
+ raise ImportError(YAML_IMPORT_ERROR_MESSAGE)
+
+ return self._import_module(module_path)
+
+
class NoYamlSerializerTestCase(TestCase):
"""Not having pyyaml installed provides a misleading error
#12756
"""
+ @classmethod
+ def setUpClass(cls):
+ """Removes imported yaml and stubs importlib.import_module"""
+ super(NoYamlSerializerTestCase, cls).setUpClass()
+
+ cls._import_module_mock = YamlImportModuleMock()
+ importlib.import_module = cls._import_module_mock.import_module
+
+ # clear out cached serializers to emulate yaml missing
+ serializers._serializers = {}
+
+ @classmethod
+ def tearDownClass(cls):
+ """Puts yaml back if necessary"""
+ super(NoYamlSerializerTestCase, cls).tearDownClass()
+
+ importlib.import_module = cls._import_module_mock._import_module
+
+ # clear out cached serializers to clean out BadSerializer instances
+ serializers._serializers = {}
+
def test_missing_pyyaml_error_message(self):
"""Using yaml serializer without pyyaml raises ImportError"""
jane = Author(name="Jane")
@@ -456,7 +499,7 @@ def test_deserializer_pyyaml_error_message(self):
self.assertRaises(ImportError, serializers.deserialize, "yaml", "")
def test_missing_pyyaml_error_message(self):
- self.assertRaisesRegexp(management.CommandError, r'No module named yaml',
+ self.assertRaisesRegexp(management.CommandError, YAML_IMPORT_ERROR_MESSAGE,
management.call_command, 'dumpdata', format='yaml')
Please sign in to comment.
Something went wrong with that request. Please try again.