fix: correct str() and repr() for EnumGroup and EnumValue (#92)

* test: add tests for Enum represtations

* chore: correct str() and repr() for EnumGroup and EnumValue
This commit is contained in:
Mike 2021-10-26 14:04:44 +00:00 committed by GitHub
parent 9b5df1c99e
commit bb68d7cec7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 7 deletions

View file

@ -70,10 +70,20 @@ class EnumValue(models.Model):
the same *Yes* and *No* *EnumValues* for both *EnumGroups*.
"""
value = models.CharField(_('Value'), db_index=True, unique=True, max_length=50)
value = models.CharField(
_('Value'),
db_index=True,
unique=True,
max_length=50,
)
def __str__(self):
return '<EnumValue {}>'.format(self.value)
"""String representation of `EnumValue` instance."""
return str(self.value)
def __repr__(self):
"""String representation of `EnumValue` object."""
return '<EnumValue {0}>'.format(self.value)
class EnumGroup(models.Model):
@ -89,7 +99,12 @@ class EnumGroup(models.Model):
values = models.ManyToManyField(EnumValue, verbose_name=_('Enum group'))
def __str__(self):
return '<EnumGroup {}>'.format(self.name)
"""String representation of `EnumGroup` instance."""
return str(self.name)
def __repr__(self):
"""String representation of `EnumGroup` object."""
return '<EnumGroup {0}>'.format(self.name)
class Attribute(models.Model):

View file

@ -1,3 +1,4 @@
import pytest
from django.test import TestCase
import eav
@ -5,11 +6,32 @@ from eav.models import Attribute, EnumGroup, EnumValue, Value
from test_project.models import Patient
@pytest.fixture()
def enumgroup(db):
"""Sample `EnumGroup` object for testing."""
test_group = EnumGroup.objects.create(name='Yes / No')
value_yes = EnumValue.objects.create(value='Yes')
value_no = EnumValue.objects.create(value='No')
test_group.values.add(value_yes)
test_group.values.add(value_no)
return test_group
def test_enumgroup_display(enumgroup):
"""Test repr() and str() of EnumGroup."""
assert '<EnumGroup {0}>'.format(enumgroup.name) == repr(enumgroup)
assert str(enumgroup) == str(enumgroup.name)
def test_enumvalue_display(enumgroup):
"""Test repr() and str() of EnumValue."""
test_value = enumgroup.values.first()
assert '<EnumValue {0}>'.format(test_value.value) == repr(test_value)
assert str(test_value) == test_value.value
class MiscModels(TestCase):
def test_enumgroup_str(self):
name = 'Yes / No'
e = EnumGroup.objects.create(name=name)
self.assertEqual('<EnumGroup Yes / No>', str(e))
"""Miscellaneous tests on models."""
def test_attribute_help_text(self):
desc = 'Patient Age'