mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-04-08 09:20:59 +00:00
Merge branch 'master' of github.com:mvpdev/eav
This commit is contained in:
commit
14173b822f
4 changed files with 252 additions and 1 deletions
|
|
@ -70,7 +70,8 @@ class EavAttribute(models.Model):
|
|||
super(EavAttribute, self).save(*args, **kwargs)
|
||||
|
||||
def add_label(self, label):
|
||||
self.labels.get_or_create(name=label)
|
||||
label, created = EavAttributeLabel.objects.get_or_create(name=label)
|
||||
label.eavattribute_set.add(self)
|
||||
|
||||
def remove_label(self, label):
|
||||
try:
|
||||
|
|
|
|||
|
|
@ -1 +1,2 @@
|
|||
from basics import *
|
||||
from set_and_get import *
|
||||
|
|
|
|||
77
tests/set_and_get.py
Normal file
77
tests/set_and_get.py
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
from datetime import datetime
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from ..models import *
|
||||
from ..utils import EavRegistry, EavConfig
|
||||
from .models import Patient
|
||||
|
||||
|
||||
class EavSetterAndGetterTests(TestCase):
|
||||
|
||||
"""
|
||||
Testing setters and getters
|
||||
"""
|
||||
|
||||
|
||||
def setUp(self):
|
||||
|
||||
EavRegistry.unregister(Patient)
|
||||
EavRegistry.register(Patient)
|
||||
|
||||
self.attribute = EavAttribute.objects\
|
||||
.create(datatype=EavAttribute.TYPE_TEXT,
|
||||
name='City', slug='city')
|
||||
|
||||
self.patient = Patient.objects.create(name="Doe")
|
||||
|
||||
self.value = EavValue.objects.create(object=self.patient,
|
||||
attribute=self.attribute,
|
||||
value_text='Denver')
|
||||
|
||||
def tearDown(self):
|
||||
EavRegistry.unregister(Patient)
|
||||
|
||||
|
||||
def test_you_can_assign_a_value_to_an_unsaved_object(self):
|
||||
|
||||
patient = Patient()
|
||||
patient.eav.city = 'Paris'
|
||||
patient.save()
|
||||
|
||||
self.assertEqual(patient.eav.city, 'Paris')
|
||||
self.assertEqual(EavValue.objects.filter(value_text='Paris').count(), 1)
|
||||
|
||||
|
||||
def test_you_can_assign_a_value_to_a_saved_object(self):
|
||||
|
||||
patient = Patient.objects.create(name='new_patient')
|
||||
patient.eav.city = 'Paris'
|
||||
patient.save()
|
||||
|
||||
self.assertEqual(patient.eav.city, 'Paris')
|
||||
self.assertEqual(EavValue.objects.filter(value_text='Paris').count(), 1)
|
||||
|
||||
|
||||
def test_assign_a_value_that_is_not_an_eav_attribute_does_nothing(self):
|
||||
|
||||
self.patient.eav.no_an_attribute = 'Woot'
|
||||
self.patient.save()
|
||||
self.assertFalse(EavValue.objects.filter(value_text='Paris').count())
|
||||
|
||||
|
||||
def test_attributes_can_be_labelled(self):
|
||||
|
||||
attribute = EavAttribute.objects\
|
||||
.create(datatype=EavAttribute.TYPE_TEXT,
|
||||
name='Country', slug='country')
|
||||
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')
|
||||
self.assertEqual(EavAttribute.objects.filter(labels__name='c').count(),
|
||||
2)
|
||||
172
tests/tests.py
Normal file
172
tests/tests.py
Normal file
|
|
@ -0,0 +1,172 @@
|
|||
from datetime import datetime
|
||||
|
||||
from django.test import TestCase
|
||||
|
||||
from ..models import *
|
||||
from ..utils import EavRegistry, EavConfig
|
||||
from .models import Patient
|
||||
|
||||
|
||||
class EavBasicTests(TestCase):
|
||||
|
||||
"""
|
||||
Testing basics such as registration, printing and object creation
|
||||
"""
|
||||
|
||||
|
||||
def setUp(self):
|
||||
|
||||
EavRegistry.unregister(Patient)
|
||||
EavRegistry.register(Patient)
|
||||
|
||||
self.attribute = EavAttribute.objects\
|
||||
.create(datatype=EavAttribute.TYPE_TEXT,
|
||||
name='City', slug='city')
|
||||
self.entity = Patient.objects.create(name="Doe")
|
||||
|
||||
self.value = EavValue.objects.create(object=self.entity,
|
||||
attribute=self.attribute,
|
||||
value_text='Denver')
|
||||
|
||||
|
||||
def tearDown(self):
|
||||
EavRegistry.unregister(Patient)
|
||||
|
||||
|
||||
def test_can_create_attribute(self):
|
||||
EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
|
||||
name='My text test',
|
||||
help_text='My help text')
|
||||
|
||||
def test_attribute_unicode(self):
|
||||
self.assertEqual(unicode(self.attribute), "City (Text)")
|
||||
|
||||
|
||||
def test_can_eaventity_children_give_you_all_attributes_by_default(self):
|
||||
qs = Patient.eav.get_eav_attributes()
|
||||
self.assertEqual(list(qs), list(EavAttribute.objects.all()))
|
||||
|
||||
|
||||
def test_value_creation(self):
|
||||
EavValue.objects.create(object=self.entity,
|
||||
attribute=self.attribute,
|
||||
value_float=1.2)
|
||||
|
||||
def test_value_unicode(self):
|
||||
self.assertEqual(unicode(self.value), "Doe - City: \"Denver\"")
|
||||
|
||||
|
||||
def test_value_unicode(self):
|
||||
self.assertEqual(unicode(self.value), "Doe - City: \"Denver\"")
|
||||
|
||||
|
||||
def test_value_types(self):
|
||||
_text = EavAttribute.objects.create(datatype=EavAttribute.TYPE_TEXT,
|
||||
name='Text',
|
||||
help_text='The text')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
attribute = _text)
|
||||
value = "Test text"
|
||||
val.value = value
|
||||
val.save()
|
||||
self.assertEqual(val.value, value)
|
||||
|
||||
_float = EavAttribute.objects.create(datatype=EavAttribute.TYPE_FLOAT,
|
||||
name='Float',
|
||||
help_text='The float')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
attribute = _float)
|
||||
value = 1.22
|
||||
val.value = value
|
||||
val.save()
|
||||
self.assertEqual(val.value, value)
|
||||
|
||||
|
||||
_int = EavAttribute.objects.create(datatype=EavAttribute.TYPE_INT,
|
||||
name='Int',
|
||||
help_text='The int')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
attribute = _int)
|
||||
value = 7
|
||||
val.value = value
|
||||
val.save()
|
||||
self.assertEqual(val.value, value)
|
||||
|
||||
_date = EavAttribute.objects.create(datatype=EavAttribute.TYPE_DATE,
|
||||
name='Date',
|
||||
help_text='The date')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
attribute = _date)
|
||||
value = datetime.now()
|
||||
val.value = value
|
||||
val.save()
|
||||
self.assertEqual(val.value, value)
|
||||
|
||||
_bool = EavAttribute.objects.create(datatype=EavAttribute.TYPE_BOOLEAN,
|
||||
name='Bool',
|
||||
help_text='The bool')
|
||||
val = EavValue.objects.create(object=self.entity,
|
||||
attribute = _bool)
|
||||
value = False
|
||||
val.value = value
|
||||
val.save()
|
||||
self.assertEqual(val.value, value)
|
||||
|
||||
|
||||
def test_eavregistry_ataches_and_detaches_eav_attribute(self):
|
||||
EavRegistry.unregister(Patient)
|
||||
p = Patient()
|
||||
self.assertFalse(hasattr(p, 'eav'))
|
||||
|
||||
EavRegistry.register(Patient)
|
||||
p2 = Patient()
|
||||
self.assertTrue(p2.eav)
|
||||
|
||||
|
||||
def test_eavregistry_ataches_and_detaches_eav_attribute(self):
|
||||
EavRegistry.unregister(Patient)
|
||||
p = Patient()
|
||||
self.assertFalse(hasattr(p, 'eav'))
|
||||
|
||||
EavRegistry.register(Patient)
|
||||
p2 = Patient()
|
||||
self.assertTrue(p2.eav)
|
||||
|
||||
|
||||
def test_eavregistry_accept_a_settings_class_with_get_queryset(self):
|
||||
EavRegistry.unregister(Patient)
|
||||
|
||||
class PatientEav(EavConfig):
|
||||
|
||||
def get_eav_attributes(self):
|
||||
return EavAttribute.objects.all()
|
||||
|
||||
EavRegistry.register(Patient, PatientEav)
|
||||
|
||||
p = Patient()
|
||||
|
||||
EavRegistry.unregister(Patient)
|
||||
|
||||
|
||||
def test_eavregistry_accept_a_settings_class_with_field_names(self):
|
||||
|
||||
p = Patient()
|
||||
registered_manager = Patient.objects
|
||||
EavRegistry.unregister(Patient)
|
||||
|
||||
class PatientEav(EavConfig):
|
||||
|
||||
proxy_field_name = 'my_eav'
|
||||
manager_field_name ='my_objects'
|
||||
|
||||
EavRegistry.register(Patient, PatientEav)
|
||||
|
||||
p2 = Patient()
|
||||
self.assertEqual(type(p.eav), type(p2.my_eav))
|
||||
self.assertEqual(type(registered_manager), type(Patient.my_objects))
|
||||
|
||||
bak_registered_manager = Patient.objects
|
||||
|
||||
EavRegistry.unregister(Patient)
|
||||
|
||||
self.assertEqual(type(Patient.objects), type(bak_registered_manager))
|
||||
Loading…
Reference in a new issue