Skip to content

Commit

Permalink
Render None as empty string (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverroick authored Jun 4, 2018
1 parent da56d16 commit 39698bf
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
__pycache__/
*.py[cod]
*$py.class
.pytest_cache/

# C extensions
*.so
Expand Down
2 changes: 2 additions & 0 deletions jsonattrs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ def choice_dict(self):
return {c: l for c, l in zip(self.choices, self.choice_labels)}

def render(self, val):
if val is None:
return ''
if self.choice_dict is None:
return val
else:
Expand Down
44 changes: 44 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,47 @@ def test_str(self):
attr = Attribute(name='testattr', id=123)
assert str(attr) == '<Attribute #123: name=testattr>'
assert repr(attr) == '<Attribute #123: name=testattr>'


class AttributeTest(TestCase):
def setUp(self):
self.fixtures = create_fixtures(do_schemas=False, load_attr_types=True)
self.schema = Schema.objects.create(
content_type=self.fixtures['party_t'], selectors=()
)

def test_render_integer(self):
attr_type = AttributeType.objects.get(name='integer')
attr = Attribute.objects.create(
schema_id=self.schema.id,
name='integer',
long_name='Test attribute integer',
index=0,
attr_type_id=attr_type.id,
)
assert attr.render(None) == ''
assert attr.render(12) == 12

def test_render_decimal(self):
attr_type = AttributeType.objects.get(name='decimal')
attr = Attribute.objects.create(
schema_id=self.schema.id,
name='decimal',
long_name='Test attribute decimal',
index=0,
attr_type_id=attr_type.id,
)
assert attr.render(None) == ''
assert attr.render(12.5) == 12.5

def test_render_date(self):
attr_type = AttributeType.objects.get(name='date')
attr = Attribute.objects.create(
schema_id=self.schema.id,
name='date',
long_name='Test attribute date',
index=0,
attr_type_id=attr_type.id,
)
assert attr.render(None) == ''
assert attr.render('2018-05-31') == '2018-05-31'

0 comments on commit 39698bf

Please sign in to comment.