mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-04-10 02:11:00 +00:00
Added test for _save_single
This commit is contained in:
parent
4d3127223c
commit
9ce78e1418
2 changed files with 35 additions and 1 deletions
|
|
@ -105,6 +105,14 @@ class EavAttribute(models.Model):
|
|||
self._save_single_value(entity, value)
|
||||
|
||||
def _save_single_value(self, entity, value=None, attribute=None):
|
||||
"""
|
||||
Save a a value of type that doesn't need special joining like
|
||||
int, float, text, date, etc.
|
||||
|
||||
Value should not be an EavValue object but a normal value.
|
||||
Use attribute if you want to use something else than the current
|
||||
one
|
||||
"""
|
||||
ct = ContentType.objects.get_for_model(entity)
|
||||
attribute = attribute or self
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -36,6 +36,24 @@ class EavSetterAndGetterTests(TestCase):
|
|||
def test_get_value_to_entity(self):
|
||||
self.assertEqual(self.attribute.get_value_for_entity(self.patient),
|
||||
self.value)
|
||||
|
||||
|
||||
def test_save_single_value(self):
|
||||
patient = Patient.objects.create(name="x")
|
||||
attr = EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
|
||||
name='a', slug='a')
|
||||
# does nothing
|
||||
attr._save_single_value(patient)
|
||||
|
||||
# save value
|
||||
attr._save_single_value(patient, 'b')
|
||||
patient = Patient.objects.get(name="x")
|
||||
self.assertEqual(patient.eav.a, 'b')
|
||||
|
||||
# save value on another attribute
|
||||
attr._save_single_value(patient, 'Paris', self.attribute)
|
||||
patient = Patient.objects.get(name="x")
|
||||
self.assertEqual(patient.eav.city, 'Paris')
|
||||
|
||||
|
||||
def test_you_can_assign_a_value_to_an_unsaved_object(self):
|
||||
|
|
@ -99,17 +117,25 @@ class EavSetterAndGetterTests(TestCase):
|
|||
attribute = EavAttribute.objects\
|
||||
.create(datatype=EavAttribute.TYPE_TEXT,
|
||||
name='Country', slug='country')
|
||||
|
||||
# add labels
|
||||
self.attribute.add_label('a')
|
||||
self.attribute.add_label('c')
|
||||
attribute.add_label('b')
|
||||
attribute.add_label('c')
|
||||
|
||||
self.assertEqual(EavAttribute.objects.get(labels__name='a').name,
|
||||
'City')
|
||||
self.assertEqual(EavAttribute.objects.get(labels__name='b').name,
|
||||
'Country')
|
||||
|
||||
# cross labels
|
||||
self.assertEqual(EavAttribute.objects.filter(labels__name='c').count(),
|
||||
2)
|
||||
|
||||
|
||||
# remove labels
|
||||
self.attribute.remove_label('a')
|
||||
self.assertFalse(EavAttribute.objects.filter(labels__name='a').count())
|
||||
# remove a label that is not attach does nothing
|
||||
self.attribute.remove_label('a')
|
||||
self.attribute.remove_label('x')
|
||||
|
|
|
|||
Loading…
Reference in a new issue