Properly skipped yaml tests when not installed

This commit is contained in:
Claude Paroz 2013-08-31 15:23:56 +02:00
parent 3e34005b1b
commit e0643cb676
1 changed files with 47 additions and 42 deletions

View File

@ -5,6 +5,12 @@ import json
from datetime import datetime from datetime import datetime
import unittest import unittest
from xml.dom import minidom from xml.dom import minidom
try:
import yaml
HAS_YAML = True
except ImportError:
HAS_YAML = False
from django.conf import settings from django.conf import settings
from django.core import serializers from django.core import serializers
@ -445,14 +451,11 @@ class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, Transact
} }
}]""" }]"""
try:
import yaml @unittest.skipUnless(HAS_YAML, "No yaml library detected")
except ImportError: class YamlSerializerTestCase(SerializersTestBase, TestCase):
pass serializer_name = "yaml"
else: fwd_ref_str = """- fields:
class YamlSerializerTestCase(SerializersTestBase, TestCase):
serializer_name = "yaml"
fwd_ref_str = """- fields:
headline: Forward references pose no problem headline: Forward references pose no problem
pub_date: 2006-06-16 15:00:00 pub_date: 2006-06-16 15:00:00
categories: [1] categories: [1]
@ -468,7 +471,7 @@ else:
pk: 1 pk: 1
model: serializers.author""" model: serializers.author"""
pkless_str = """- fields: pkless_str = """- fields:
name: Reference name: Reference
pk: null pk: null
model: serializers.category model: serializers.category
@ -476,42 +479,44 @@ else:
name: Non-fiction name: Non-fiction
model: serializers.category""" model: serializers.category"""
@staticmethod @staticmethod
def _validate_output(serial_str): def _validate_output(serial_str):
try: try:
yaml.safe_load(StringIO(serial_str)) yaml.safe_load(StringIO(serial_str))
except Exception: except Exception:
return False return False
else: else:
return True return True
@staticmethod @staticmethod
def _get_pk_values(serial_str): def _get_pk_values(serial_str):
ret_list = [] ret_list = []
stream = StringIO(serial_str) stream = StringIO(serial_str)
for obj_dict in yaml.safe_load(stream): for obj_dict in yaml.safe_load(stream):
ret_list.append(obj_dict["pk"]) ret_list.append(obj_dict["pk"])
return ret_list return ret_list
@staticmethod @staticmethod
def _get_field_values(serial_str, field_name): def _get_field_values(serial_str, field_name):
ret_list = [] ret_list = []
stream = StringIO(serial_str) stream = StringIO(serial_str)
for obj_dict in yaml.safe_load(stream): for obj_dict in yaml.safe_load(stream):
if "fields" in obj_dict and field_name in obj_dict["fields"]: if "fields" in obj_dict and field_name in obj_dict["fields"]:
field_value = obj_dict["fields"][field_name] field_value = obj_dict["fields"][field_name]
# yaml.safe_load will return non-string objects for some # yaml.safe_load will return non-string objects for some
# of the fields we are interested in, this ensures that # of the fields we are interested in, this ensures that
# everything comes back as a string # everything comes back as a string
if isinstance(field_value, six.string_types): if isinstance(field_value, six.string_types):
ret_list.append(field_value) ret_list.append(field_value)
else: else:
ret_list.append(str(field_value)) ret_list.append(str(field_value))
return ret_list return ret_list
class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "yaml" @unittest.skipUnless(HAS_YAML, "No yaml library detected")
fwd_ref_str = """- fields: class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase):
serializer_name = "yaml"
fwd_ref_str = """- fields:
headline: Forward references pose no problem headline: Forward references pose no problem
pub_date: 2006-06-16 15:00:00 pub_date: 2006-06-16 15:00:00
categories: [1] categories: [1]