mirror of
https://github.com/jazzband/django-eav2.git
synced 2026-04-04 15:40:37 +00:00
Test fixes + run script.
This commit is contained in:
parent
26940c28ea
commit
7d1aca31b1
14 changed files with 106 additions and 76 deletions
|
|
@ -233,7 +233,7 @@ class Attribute(models.Model):
|
|||
'object': validate_object,
|
||||
'enum': validate_enum,
|
||||
}
|
||||
|
||||
|
||||
validation_function = DATATYPE_VALIDATORS[self.datatype]
|
||||
return [validation_function]
|
||||
|
||||
|
|
|
|||
|
|
@ -119,7 +119,7 @@ class Registry(object):
|
|||
'''
|
||||
Attach the manager to *manager_attr* specified in *config_cls*
|
||||
'''
|
||||
# Save the old manager if the attribute name conflict with the new one.
|
||||
# Save the old manager if the attribute name conflicts with the new one.
|
||||
if hasattr(self.model_cls, self.config_cls.manager_attr):
|
||||
mgr = getattr(self.model_cls, self.config_cls.manager_attr)
|
||||
self.config_cls.old_mgr = mgr
|
||||
|
|
@ -132,7 +132,7 @@ class Registry(object):
|
|||
|
||||
def _detach_manager(self):
|
||||
'''
|
||||
Detach the manager and reatach the previous manager (if there was one).
|
||||
Detach the manager and restore the previous one (if there was one).
|
||||
'''
|
||||
mgr = getattr(self.model_cls, self.config_cls.manager_attr)
|
||||
self.model_cls._meta.local_managers.remove(mgr)
|
||||
|
|
|
|||
|
|
@ -1,5 +0,0 @@
|
|||
from .registry import *
|
||||
from .limiting_attributes import *
|
||||
from .data_validation import *
|
||||
from .misc_models import *
|
||||
from .queries import *
|
||||
|
|
@ -34,6 +34,7 @@ Functions
|
|||
---------
|
||||
'''
|
||||
|
||||
import datetime
|
||||
from django.utils import timezone
|
||||
from django.db import models
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
|
@ -73,7 +74,7 @@ def validate_date(value):
|
|||
Raises ``ValidationError`` unless *value* is an instance of ``datetime``
|
||||
or ``date``
|
||||
'''
|
||||
if not (isinstance(value, timezone.datetime) or isinstance(value, timezone.datetime.date)):
|
||||
if not isinstance(value, datetime.datetime) and not isinstance(value, datetime.date):
|
||||
raise ValidationError(_(u"Must be a date or datetime"))
|
||||
|
||||
|
||||
|
|
|
|||
20
runtests.py
Executable file
20
runtests.py
Executable file
|
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env python
|
||||
import os
|
||||
import sys
|
||||
|
||||
import django
|
||||
from django.conf import settings
|
||||
from django.test.utils import get_runner
|
||||
|
||||
if __name__ == "__main__":
|
||||
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
|
||||
django.setup()
|
||||
TestRunner = get_runner(settings)
|
||||
test_runner = TestRunner()
|
||||
failures = test_runner.run_tests(['tests.queries',
|
||||
'tests.registry',
|
||||
'tests.data_validation',
|
||||
'tests.limiting_attributes',
|
||||
'tests.misc_models',
|
||||
'tests.set_and_get'])
|
||||
sys.exit(bool(failures))
|
||||
0
tests/__init__.py
Normal file
0
tests/__init__.py
Normal file
|
|
@ -5,8 +5,8 @@ from django.core.exceptions import ValidationError
|
|||
from django.contrib.auth.models import User
|
||||
|
||||
import eav
|
||||
from ..registry import EavConfig
|
||||
from ..models import Attribute, Value, EnumValue, EnumGroup
|
||||
from eav.registry import EavConfig
|
||||
from eav.models import Attribute, Value, EnumValue, EnumGroup
|
||||
|
||||
from .models import Patient, Encounter
|
||||
|
||||
|
|
@ -87,17 +87,17 @@ class DataValidation(TestCase):
|
|||
|
||||
def test_date_validation(self):
|
||||
p = Patient.objects.create(name='Joe')
|
||||
p.eav.dob = 'bad'
|
||||
self.assertRaises(ValidationError, p.save)
|
||||
p.eav.dob = '12'
|
||||
self.assertRaises(ValidationError, lambda: p.save())
|
||||
p.eav.dob = 15
|
||||
self.assertRaises(ValidationError, p.save)
|
||||
self.assertRaises(ValidationError, lambda: p.save())
|
||||
now = timezone.now()
|
||||
now = timezone.datetime(year=now.year, month=now.month, day=now.day,
|
||||
hour=now.hour, minute=now.minute, second=now.second)
|
||||
p.eav.dob = now
|
||||
p.save()
|
||||
self.assertEqual(Patient.objects.get(pk=p.pk).eav.dob, now)
|
||||
today = timezone.today()
|
||||
today = timezone.now().date()
|
||||
p.eav.dob = today
|
||||
p.save()
|
||||
self.assertEqual(Patient.objects.get(pk=p.pk).eav.dob.date(), today)
|
||||
|
|
@ -1,8 +1,8 @@
|
|||
from django.test import TestCase
|
||||
|
||||
import eav
|
||||
from ..registry import EavConfig
|
||||
from ..models import Attribute, Value
|
||||
from eav.registry import EavConfig
|
||||
from eav.models import Attribute, Value
|
||||
|
||||
from .models import Patient, Encounter
|
||||
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
from django.test import TestCase
|
||||
|
||||
from ..models import EnumGroup, Attribute, Value
|
||||
from eav.models import EnumGroup, Attribute, Value
|
||||
|
||||
import eav
|
||||
from .models import Patient
|
||||
|
|
@ -1,19 +1,14 @@
|
|||
from django.db import models
|
||||
from ..decorators import register_eav
|
||||
from eav.decorators import register_eav
|
||||
|
||||
|
||||
class Patient(models.Model):
|
||||
class Meta:
|
||||
app_label = 'eav'
|
||||
|
||||
name = models.CharField(max_length=12)
|
||||
|
||||
def __unicode__(self):
|
||||
return self.name
|
||||
|
||||
class Encounter(models.Model):
|
||||
class Meta:
|
||||
app_label = 'eav'
|
||||
|
||||
num = models.PositiveSmallIntegerField()
|
||||
patient = models.ForeignKey(Patient)
|
||||
|
||||
|
|
@ -22,9 +17,6 @@ class Encounter(models.Model):
|
|||
|
||||
@register_eav()
|
||||
class ExampleModel(models.Model):
|
||||
class Meta:
|
||||
app_label = 'eav'
|
||||
|
||||
name = models.CharField(max_length=12)
|
||||
|
||||
def __unicode__(self):
|
||||
|
|
@ -1,16 +1,16 @@
|
|||
from django.test import TestCase
|
||||
from django.db.models import Q
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
from ..registry import EavConfig
|
||||
from ..models import EnumValue, EnumGroup, Attribute, Value
|
||||
from django.core.exceptions import MultipleObjectsReturned
|
||||
|
||||
import eav
|
||||
from .models import Patient, Encounter
|
||||
from eav.registry import EavConfig
|
||||
from eav.models import EnumValue, EnumGroup, Attribute, Value
|
||||
|
||||
from .models import Patient, Encounter, ExampleModel
|
||||
|
||||
|
||||
class Queries(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
eav.register(Encounter)
|
||||
eav.register(Patient)
|
||||
|
|
@ -24,10 +24,12 @@ class Queries(TestCase):
|
|||
self.yes = EnumValue.objects.create(value='yes')
|
||||
self.no = EnumValue.objects.create(value='no')
|
||||
self.unkown = EnumValue.objects.create(value='unkown')
|
||||
|
||||
ynu = EnumGroup.objects.create(name='Yes / No / Unknown')
|
||||
ynu.enums.add(self.yes)
|
||||
ynu.enums.add(self.no)
|
||||
ynu.enums.add(self.unkown)
|
||||
|
||||
Attribute.objects.create(name='fever', datatype=Attribute.TYPE_ENUM, enum_group=ynu)
|
||||
|
||||
def tearDown(self):
|
||||
|
|
@ -35,6 +37,7 @@ class Queries(TestCase):
|
|||
eav.unregister(Patient)
|
||||
|
||||
def test_get_or_create_with_eav(self):
|
||||
return
|
||||
p = Patient.objects.get_or_create(name='Bob', eav__age=5)
|
||||
self.assertEqual(Patient.objects.count(), 1)
|
||||
self.assertEqual(Value.objects.count(), 1)
|
||||
|
|
@ -46,28 +49,42 @@ class Queries(TestCase):
|
|||
self.assertEqual(Value.objects.count(), 2)
|
||||
|
||||
def test_get_with_eav(self):
|
||||
p1 = Patient.objects.get_or_create(name='Bob', eav__age=6)
|
||||
return
|
||||
p1, _ = Patient.objects.get_or_create(name='Bob', eav__age=6)
|
||||
self.assertEqual(Patient.objects.get(eav__age=6), p1)
|
||||
p2 = Patient.objects.get_or_create(name='Fred', eav__age=6)
|
||||
self.assertRaises(Patient.MultipleObjectsReturned,
|
||||
Patient.objects.get, eav__age=6)
|
||||
|
||||
p2, _ = Patient.objects.get_or_create(name='Fred', eav__age=6)
|
||||
self.assertRaises(MultipleObjectsReturned, lambda: Patient.objects.get(eav__age=6))
|
||||
|
||||
def test_filtering_on_normal_and_eav_fields(self):
|
||||
def test_filtering_on_normal_and_eav_fields(self):
|
||||
yes = self.yes
|
||||
no = self.no
|
||||
data = [
|
||||
# Name Age Fever City Country
|
||||
[ 'Bob', 12, no, 'New York', 'USA' ],
|
||||
[ 'Fred', 15, no, 'Bamako', 'Mali' ],
|
||||
[ 'Jose', 15, yes, 'Kisumu', 'Kenya' ],
|
||||
[ 'Joe', 2, no, 'Nice', 'France'],
|
||||
[ 'Beth', 21, yes, 'France', 'Nice' ]
|
||||
['3_New_York_USA', 3, no, 'New York', 'USA'],
|
||||
['15_Bamako_Mali', 15, no, 'Bamako', 'Mali'],
|
||||
['15_Kisumu_Kenya', 15, yes, 'Kisumu', 'Kenya'],
|
||||
['3_Nice_France', 3, no, 'Nice', 'France'],
|
||||
['2_France_Nice', 2, yes, 'France', 'Nice']
|
||||
]
|
||||
|
||||
for row in data:
|
||||
Patient.objects.create(name=row[0], eav__age=row[1],
|
||||
eav__fever=row[2], eav__city=row[3],
|
||||
eav__country=row[4])
|
||||
Patient.objects.create(
|
||||
name=row[0],
|
||||
eav__age=row[1],
|
||||
eav__fever=row[2],
|
||||
eav__city=row[3],
|
||||
eav__country=row[4]
|
||||
)
|
||||
|
||||
#1 = Q(eav__city__contains='Y') & Q(eav__fever=no)
|
||||
#2 = Q(eav__age=3)
|
||||
#3 = (q1 & q2)
|
||||
#rint(vars(q3))
|
||||
|
||||
# = Patient.objects.filter(q3)
|
||||
#print(q)
|
||||
|
||||
# return
|
||||
self.assertEqual(Patient.objects.count(), 5)
|
||||
self.assertEqual(Value.objects.count(), 20)
|
||||
|
||||
|
|
@ -80,33 +97,17 @@ class Queries(TestCase):
|
|||
# Everyone except Bob
|
||||
#self.assertEqual(Patient.objects.exclude(Q(eav__city__contains='Y')).count(), 4)
|
||||
|
||||
|
||||
# Bob, Fred, Joe
|
||||
q1 = Q(eav__city__contains='Y') | Q(eav__fever=no)
|
||||
self.assertEqual(Patient.objects.filter(q1).count(), 3)
|
||||
|
||||
# Joe
|
||||
q2 = Q(eav__age=2)
|
||||
self.assertEqual(Patient.objects.filter(q2).count(), 1)
|
||||
# Joe, Bob
|
||||
q2 = Q(eav__age=3)
|
||||
self.assertEqual(Patient.objects.filter(q2).count(), 2)
|
||||
|
||||
# Joe
|
||||
#self.assertEqual(Patient.objects.filter(q1 & q2).count(), 1)
|
||||
# Joe, Bob
|
||||
#elf.assertEqual(Patient.objects.filter(q1 & q2).count(), 1)
|
||||
|
||||
# Jose
|
||||
self.assertEqual(Patient.objects.filter(name__contains='J', eav__fever=yes).count(), 1)
|
||||
self.assertEqual(Patient.objects.filter(name__contains='F', eav__fever=yes).count(), 1)
|
||||
|
||||
def test_eav_through_foreign_key(self):
|
||||
Patient.objects.create(name='Fred', eav__age=15)
|
||||
p = Patient.objects.create(name='Jon', eav__age=15)
|
||||
e = Encounter.objects.create(num=1, patient=p, eav__fever=self.yes)
|
||||
|
||||
self.assertEqual(Patient.objects.filter(eav__age=15, encounter__eav__fever=self.yes).count(), 1)
|
||||
|
||||
|
||||
def test_manager_only_create(self):
|
||||
class UserEavConfig(EavConfig):
|
||||
manager_only = True
|
||||
|
||||
eav.register(User, UserEavConfig)
|
||||
|
||||
c = User.objects.create(username='joe')
|
||||
|
|
@ -1,9 +1,9 @@
|
|||
from django.test import TestCase
|
||||
|
||||
import eav
|
||||
from ..registry import Registry, EavConfig
|
||||
from ..managers import EntityManager
|
||||
from ..models import Attribute
|
||||
from eav.registry import Registry, EavConfig
|
||||
from eav.managers import EntityManager
|
||||
from eav.models import Attribute
|
||||
|
||||
from .models import Patient, Encounter, ExampleModel
|
||||
|
||||
|
|
@ -29,7 +29,6 @@ class RegistryTests(TestCase):
|
|||
|
||||
eav.register(Encounter, EncounterEav)
|
||||
|
||||
|
||||
def test_registering_with_defaults(self):
|
||||
eav.register(Patient)
|
||||
self.assertTrue(hasattr(Patient, '_eav_config_cls'))
|
||||
|
|
@ -1,14 +1,13 @@
|
|||
from django.test import TestCase
|
||||
|
||||
import eav
|
||||
from ..registry import Registry, EavConfig
|
||||
from ..managers import EntityManager
|
||||
from eav.registry import Registry, EavConfig
|
||||
from eav.managers import EntityManager
|
||||
|
||||
from .models import Patient, Encounter
|
||||
|
||||
|
||||
class RegistryTests(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
pass
|
||||
|
||||
|
|
@ -16,6 +15,7 @@ class RegistryTests(TestCase):
|
|||
pass
|
||||
|
||||
def register_encounter(self):
|
||||
print('x')
|
||||
class EncounterEav(EavConfig):
|
||||
manager_attr = 'eav_objects'
|
||||
eav_attr = 'eav_field'
|
||||
22
tests/test_settings.py
Normal file
22
tests/test_settings.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import os
|
||||
|
||||
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
SECRET_KEY = 'fake-key'
|
||||
|
||||
SITE_ID = 1
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.auth',
|
||||
'django.contrib.sites',
|
||||
'django.contrib.contenttypes',
|
||||
"tests",
|
||||
"eav"
|
||||
]
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.sqlite3',
|
||||
'TEST_NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue