diff --git a/.gitignore b/.gitignore index d443378a..eaedbd1a 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ __pycache__/ *.py[cod] *$py.class +.pytest_cache/ # C extensions *.so diff --git a/jsonattrs/models.py b/jsonattrs/models.py index b55e6cba..7e757019 100644 --- a/jsonattrs/models.py +++ b/jsonattrs/models.py @@ -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: diff --git a/tests/test_models.py b/tests/test_models.py index 2774da03..e61b34b9 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -99,3 +99,47 @@ def test_str(self): attr = Attribute(name='testattr', id=123) assert str(attr) == '' assert repr(attr) == '' + + +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'