Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #305 from edx/shr/bug/TNL-2686-etree-set-value-wit…
Browse files Browse the repository at this point in the history
…h-empty-string-instead-None

return empty string if value is none
  • Loading branch information
Shrhawk committed Aug 5, 2015
2 parents d1ff8cf + bb8b820 commit 0d3d43b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
10 changes: 9 additions & 1 deletion xblock/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,11 +780,19 @@ class String(JSONField):
MUTABLE = False

def from_json(self, value):
if value is None or isinstance(value, basestring):
if value is None:
return ''
elif isinstance(value, basestring):
return value
else:
raise TypeError('Value stored in a String must be None or a string, found %s' % type(value))

def to_json(self, value):
if value is None:
return ''
self._warn_deprecated_outside_JSONField()
return value

def from_string(self, value):
"""String gets serialized and deserialized without quote marks."""
return self.from_json(value)
Expand Down
9 changes: 8 additions & 1 deletion xblock/test/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,14 @@ def test_json_equals(self):
self.assertJSONOrSetEquals('', '')

def test_none(self):
self.assertJSONOrSetEquals(None, None)
# test for NoneType for to_string and from_string methods
test_field = String(enforce_type=True)

result_to_string = test_field.to_string(None)
self.assertEquals(result_to_string, '')

result_from_string = test_field.from_string(None)
self.assertEquals(result_from_string, '')

def test_error(self):
self.assertJSONOrSetTypeError(['a'])
Expand Down

0 comments on commit 0d3d43b

Please sign in to comment.