Added test for set and get

This commit is contained in:
ksamuel 2010-09-07 12:45:57 +00:00
parent 5444c15c7e
commit d54b73a4cd
2 changed files with 42 additions and 1 deletions

View file

@ -90,8 +90,11 @@ class EavAttribute(models.Model):
'''
ct = ContentType.objects.get_for_model(entity)
qs = self.eavvalue_set.filter(content_type=ct, object_id=entity.pk)
if qs.count():
if qs.count() == 1:
return qs[0]
raise AttributeError(u"You should have one and only one value"\
u"for the attribute %s and the entity %s. Found "\
u"%s" % (self, entity, qs.count()))
def save_value(self, entity, value):
self._save_single_value(entity, value)

View file

@ -32,6 +32,11 @@ class EavSetterAndGetterTests(TestCase):
def tearDown(self):
EavRegistry.unregister(Patient)
def test_get_value_to_entity(self):
self.assertEqual(self.attribute.get_value_for_entity(self.patient),
self.value)
def test_you_can_assign_a_value_to_an_unsaved_object(self):
@ -53,6 +58,35 @@ class EavSetterAndGetterTests(TestCase):
self.assertEqual(EavValue.objects.filter(value_text='Paris').count(), 1)
def test_you_can_create_several_type_of_attributes(self):
EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
name='text', slug='text')
EavAttribute.objects.create(datatype=EavAttribute.TYPE_FLOAT,
name='float', slug='float')
EavAttribute.objects.create(datatype=EavAttribute.TYPE_INT,
name='int', slug='int')
EavAttribute.objects.create(datatype=EavAttribute.TYPE_DATE,
name='date', slug='date')
EavAttribute.objects.create(datatype=EavAttribute.TYPE_BOOLEAN,
name='bool', slug='bool')
now = datetime.today()
self.patient.eav.text = 'a'
self.patient.eav.float = 1.0
self.patient.eav.int = 1
self.patient.eav.date = now
self.patient.eav.bool = True
self.patient.save()
patient = Patient.objects.get(pk=self.patient.pk)
self.assertEqual(self.patient.eav.float, 1.0)
self.assertEqual(self.patient.eav.int, 1)
self.assertEqual(self.patient.eav.date, now)
self.assertEqual(self.patient.eav.bool, True)
def test_assign_a_value_that_is_not_an_eav_attribute_does_nothing(self):
self.patient.eav.no_an_attribute = 'Woot'
@ -75,3 +109,7 @@ class EavSetterAndGetterTests(TestCase):
'Country')
self.assertEqual(EavAttribute.objects.filter(labels__name='c').count(),
2)
self.attribute.remove_label('a')
self.assertFalse(EavAttribute.objects.filter(labels__name='a').count())
self.attribute.remove_label('a')