Commit ac4e20b4 by Chris Dodge

change serialize_field to check for type. If value is a string, simply return…

change serialize_field to check for type. If value is a string, simply return that, otherwise do a json.dumps
parent 958ecdb0
......@@ -4,7 +4,7 @@
from xmodule.x_module import XModuleFields
from xblock.core import Scope, String, Dict, Boolean, Integer, Float, Any, List
from xmodule.fields import Date, Timedelta
from xmodule.xml_module import XmlDescriptor, serialize_field, deserialize_field, serialize_string_literal
from xmodule.xml_module import XmlDescriptor, serialize_field, deserialize_field
import unittest
from .import get_test_system
from nose.tools import assert_equals
......@@ -137,7 +137,6 @@ class EditableMetadataFieldsTest(unittest.TestCase):
type='Float', options={'min': 0, 'step': .3}
)
# Start of helper methods
def get_xml_editable_fields(self, model_data):
system = get_test_system()
......@@ -179,33 +178,20 @@ class TestSerialize(unittest.TestCase):
def test_serialize(self):
assert_equals('null', serialize_field(None))
assert_equals('-2', serialize_field(-2))
assert_equals('"2"', serialize_field('2'))
assert_equals('2', serialize_field('2'))
assert_equals('-3.41', serialize_field(-3.41))
assert_equals('"2.589"', serialize_field('2.589'))
assert_equals('2.589', serialize_field('2.589'))
assert_equals('false', serialize_field(False))
assert_equals('"false"', serialize_field('false'))
assert_equals('"fAlse"', serialize_field('fAlse'))
assert_equals('"hat box"', serialize_field('hat box'))
assert_equals('false', serialize_field('false'))
assert_equals('fAlse', serialize_field('fAlse'))
assert_equals('hat box', serialize_field('hat box'))
assert_equals('{"bar": "hat", "frog": "green"}', serialize_field({'bar': 'hat', 'frog': 'green'}))
assert_equals('[3.5, 5.6]', serialize_field([3.5, 5.6]))
assert_equals('["foo", "bar"]', serialize_field(['foo', 'bar']))
assert_equals('"2012-12-31T23:59:59Z"', serialize_field("2012-12-31T23:59:59Z"))
assert_equals('"1 day 12 hours 59 minutes 59 seconds"',
serialize_field("1 day 12 hours 59 minutes 59 seconds"))
def test_serialize_string_literal(self):
assert_equals('2', serialize_string_literal('2'))
assert_equals('2.589', serialize_string_literal('2.589'))
assert_equals('false', serialize_string_literal('false'))
assert_equals('fAlse', serialize_string_literal('fAlse'))
assert_equals('hat box', serialize_string_literal('hat box'))
assert_equals('2012-12-31T23:59:59Z', serialize_string_literal("2012-12-31T23:59:59Z"))
assert_equals('2012-12-31T23:59:59Z', serialize_field("2012-12-31T23:59:59Z"))
assert_equals('1 day 12 hours 59 minutes 59 seconds',
serialize_string_literal("1 day 12 hours 59 minutes 59 seconds"))
serialize_field("1 day 12 hours 59 minutes 59 seconds"))
# make sure we can't call serialize_string_literal with a non basestring type
with self.assertRaises(TypeError):
self.assertRaises(serialize_string_literal(2.31))
class TestDeserialize(unittest.TestCase):
def assertDeserializeEqual(self, expected, arg):
......@@ -214,7 +200,6 @@ class TestDeserialize(unittest.TestCase):
"""
assert_equals(expected, deserialize_field(self.test_field(), arg))
def assertDeserializeNonString(self):
"""
Asserts input value is returned for None or something that is not a string.
......
......@@ -83,19 +83,13 @@ def serialize_field(value):
"""
Return a string version of the value (where value is the JSON-formatted, internally stored value).
By default, this is the result of calling json.dumps on the input value.
If the value is a string, then we simply return what was passed in.
Otherwise, we return json.dumps on the input value.
"""
return json.dumps(value, cls=EdxJSONEncoder)
def serialize_string_literal(value):
"""
Assert that the value is a base string and - if it is - simply return it
"""
if not isinstance(value, basestring):
raise TypeError('Value {0} is not of type basestring!'.format(value))
if isinstance(value, basestring):
return value
return value
return json.dumps(value, cls=EdxJSONEncoder)
def deserialize_field(field, value):
......@@ -176,7 +170,7 @@ class XmlDescriptor(XModuleDescriptor):
for field in set(cls.fields + cls.lms.fields):
if field.name == attr:
from_xml = lambda val: deserialize_field(field, val)
to_xml = lambda val: serialize_string_literal(val) if isinstance(val, basestring) else serialize_field(val)
to_xml = lambda val: serialize_field(val)
return AttrMap(from_xml, to_xml)
return AttrMap()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment