summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorRoberto Aguilar <roberto@baremetal.io>2013-09-06 21:35:21 +0000
committerRoberto Aguilar <roberto@baremetal.io>2013-09-06 22:26:13 +0000
commitca3ac4a3e37cc844bef7f7553c6b1a6cc642e6e6 (patch)
tree7b623ca1b422569479179ee9f4fbd1e47dc58a6b /tests
parent076cf131ec8084ce6d9cc3a05b0f4bcc167d6cbc (diff)
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.
Diffstat (limited to 'tests')
-rw-r--r--tests/serializers/tests.py47
1 files changed, 45 insertions, 2 deletions
diff --git a/tests/serializers/tests.py b/tests/serializers/tests.py
index b32aa22657..5ed6e7f9cb 100644
--- a/tests/serializers/tests.py
+++ b/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 @@ class NoYamlSerializerTestCase(TestCase):
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')