mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-03-16 22:40:26 +00:00
This merge does two things:
1. Removes completely dependency on the Sites framework (which was implemented erroneously).
Specifically, site field on Attribute and related constraints. With those changes, attributes are now global. That is, all attributes are visible to all entities. As a result, slug in now unique.
2. Content-type field is used to limit entities that can be assigned an attribute (see get_all_attributes method on Entity). This way attributes with content_type assigned are, for all intended purposes, private to Entity of that content type. The problem is that those attributes are still visible to other entities. Moreover, originally, attributes had non-unique slugs that are unique together with content_type and site. With removal of site (and because content_type is nullable) a situation might exist in which two attributes exist with the same slug/name but one with content_type and one with NULL. Simple solution to all this hassle is to drop private attributes altogether, which is what this pull-request does.
Resolves: #9
Commits:
* Remove sites dependency; fix Attribute uniqueness (#9)
* Add uniqueness test to attributes
* Remove content_type from Attribute class
* Update runtests
* Remove redudant call
* Fix wrong assertion in attribute test
* Fix string formatting
65 lines
2.2 KiB
Python
65 lines
2.2 KiB
Python
from django.core.exceptions import ValidationError
|
|
from django.test import TestCase
|
|
|
|
import eav
|
|
from eav.models import Attribute, Value
|
|
from eav.registry import EavConfig
|
|
|
|
from .models import Encounter, Patient
|
|
|
|
|
|
class Attributes(TestCase):
|
|
'''
|
|
TODO: Explain this test.
|
|
'''
|
|
|
|
def setUp(self):
|
|
class EncounterEavConfig(EavConfig):
|
|
manager_attr = 'eav_objects'
|
|
eav_attr = 'eav_field'
|
|
generic_relation_attr = 'encounter_eav_values'
|
|
generic_relation_related_name = 'encounters'
|
|
|
|
@classmethod
|
|
def get_attributes(cls):
|
|
return Attribute.objects.filter(slug__contains='a')
|
|
|
|
eav.register(Encounter, EncounterEavConfig)
|
|
eav.register(Patient)
|
|
|
|
Attribute.objects.create(name='age', datatype=Attribute.TYPE_INT)
|
|
Attribute.objects.create(name='height', datatype=Attribute.TYPE_FLOAT)
|
|
Attribute.objects.create(name='weight', datatype=Attribute.TYPE_FLOAT)
|
|
Attribute.objects.create(name='color', datatype=Attribute.TYPE_TEXT)
|
|
|
|
def tearDown(self):
|
|
eav.unregister(Encounter)
|
|
eav.unregister(Patient)
|
|
|
|
def test_get_attribute_querysets(self):
|
|
self.assertEqual(Patient._eav_config_cls.get_attributes().count(), 4)
|
|
self.assertEqual(Encounter._eav_config_cls.get_attributes().count(), 1)
|
|
|
|
def test_duplicate_attributs(self):
|
|
'''
|
|
Ensure that no two Attributes with the same slug can exist.
|
|
'''
|
|
with self.assertRaises(ValidationError):
|
|
Attribute.objects.create(name='height', datatype=Attribute.TYPE_FLOAT)
|
|
|
|
def test_setting_attributes(self):
|
|
p = Patient.objects.create(name='Jon')
|
|
e = Encounter.objects.create(patient=p, num=1)
|
|
p.eav.age = 3
|
|
p.eav.height = 2.3
|
|
p.save()
|
|
e.eav_field.age = 4
|
|
e.eav_field.height = 4.5
|
|
e.save()
|
|
self.assertEqual(Value.objects.count(), 3)
|
|
p = Patient.objects.get(name='Jon')
|
|
self.assertEqual(p.eav.age, 3)
|
|
self.assertEqual(p.eav.height, 2.3)
|
|
e = Encounter.objects.get(num=1)
|
|
self.assertEqual(e.eav_field.age, 4)
|
|
self.assertFalse(hasattr(e.eav_field, 'height'))
|