Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render None as empty string #41

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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'