Permalink
Browse files

added unit test for djando jalali models

  • Loading branch information...
1 parent eafffef commit 4ef6c91a90d53fe1cb85cc9a274768990258a306 @slashmili committed Apr 2, 2012
@@ -3,7 +3,7 @@
import time
import datetime
import jdatetime
-from django.core import validators
+from django.core import validators, exceptions
from django.utils import datetime_safe, formats
from django.utils.translation import ugettext as _
from widgets import *
@@ -33,7 +33,7 @@ def to_python(self, value):
return jdatetime.date(*time.strptime(value, format)[:3])
except ValueError:
continue
- raise ValidationError(self.error_messages['invalid'])
+ raise exceptions.ValidationError(self.error_messages['invalid'])
class jDateTimeField(forms.Field):
View
No changes.
@@ -0,0 +1,120 @@
+from django.test import TestCase
+from django.core import exceptions
+from django.db.utils import IntegrityError
+import jdatetime
+import datetime
+
+
+from django_jalali.tests.models import jDateModel, jDateTimeModel
+
+from custom_settings import change_settings
+settings_manager = change_settings(append_apps=['django_jalali.tests'])
+
+
+
+
+class CreateModelTestCase(TestCase):
+ def tearDown(self):
+ global settings_manager
+ settings_manager.revert()
+
+ def test_simple_save(self):
+ """test simple function, including saving and retrieving jalali date from database"""
+ jd_model = jDateModel(date=jdatetime.date.today())
+ jd_model.save()
+ d = jDateModel.objects.filter(date=jdatetime.date.today())
+ self.assertEqual(d[0].date.togregorian(), datetime.date.today())
+
+ j_date_time = jdatetime.datetime.today()
+ jdt_model = jDateTimeModel(date_time=j_date_time)
+ jdt_model.save()
+ dt = jDateTimeModel.objects.filter(date_time=j_date_time)
+ self.assertEqual(dt[0].date_time.togregorian(), j_date_time.togregorian())
+ def test_diffrent_saveing(self):
+ jd_model = jDateModel(date='1391-01-01')
+ jd_model.save()
+
+ jd_model = jDateModel(date=datetime.datetime.now())
+ jd_model.save()
+
+ jd_model = jDateModel()
+ jd_model.save()
+
+ jd_model = jDateModel(date=jdatetime.datetime.now())
+ jd_model.save()
+
+ #to_python
+ jdt_model = jDateTimeModel(date_time='1391-01-01')
+ #TODO: Find the correct exceptions
+ #with self.assertRaises(Exception):
+ # jdt_model.date_time = None
+
+ jdt_model.date_time = datetime.datetime(1390,01,02)
+ jdt_model.save()
+ self.assertEqual(jDateTimeModel.objects.filter(date_time=datetime.datetime(1390,01,02))[0].date_time , jdt_model.date_time)
+
+ jdt_model.date_time = jdatetime.date.today()
+ jdt_model.save()
+ self.assertEqual(jDateTimeModel.objects.filter(date_time=jdatetime.date.today())[0].date_time, jdt_model.date_time)
+
+ jdt_model.date_time = datetime.date.today()
+ jdt_model.save()
+ self.assertEqual(jDateTimeModel.objects.filter(date_time=datetime.date.today())[0].date_time, jdt_model.date_time)
+
+ jdt_model.date_time = datetime.date(1390,01,01)
+ jdt_model.save()
+ self.assertEqual(jDateTimeModel.objects.filter(date_time=jdatetime.date(1390,01,01))[0].date_time, jdt_model.date_time)
+
+ jdt_model.date_time = '1391-01-01 12:12'
+ jdt_model.save()
+ self.assertEqual(jDateTimeModel.objects.filter(date_time=jdatetime.datetime(1391,01,01,12,12))[0].date_time, jdt_model.date_time)
+
+
+ jdt_model.date_time = '1391-01-01 12:12:12'
+ jdt_model.save()
+ self.assertEqual(jDateTimeModel.objects.filter(date_time=jdatetime.datetime(1391,01,01,12,12,12))[0].date_time, jdt_model.date_time)
+
+ jdt_model.date_time = '2012-01-01 12:12'
+ jdt_model.save()
+ #TODO: fixus, we'r broken
+ #self.assertEqual(jDateTimeModel.objects.filter(date_time='2012-01-01 12:12')[0].date_time, jdt_model.date_time)
+ #self.assertEqual(jDateTimeModel.objects.filter(date_time=datetime.datetime(2012, 01, 01, 12, 12))[0].date_time, jdt_model.date_time)
+ jdt_model.date_time = '2012-01-01 12:12:12'
+
+ jdt_model.date_time = '1391-01-01 12:12:12.2222'
+ jdt_model.save()
+ self.assertEqual(jDateTimeModel.objects.filter(date_time = '1391-01-01 12:12:12.2222')[0] , jdt_model)
+
+ def test_filtering(self):
+ """test filtering"""
+ today = jdatetime.date.today()
+ jd_model = jDateModel(date=today)
+ jd_model.save()
+ dt = jDateModel.objects.filter(date__year=today.year)
+ print jDateModel.objects.filter(date="%s-%s-%s"%(today.year, today.month, today.day))
+ today = datetime.date.today()
+ jd_model = jDateModel(date=today)
+ jd_model.save()
+ print jDateModel.objects.filter(date=today)
+ print jDateModel.objects.filter(date="%s-%s-%s"%(today.year, today.month, today.day))
+ print str(jDateModel.objects.filter(date__in =["%s-%s-%s"%(today.year, today.month, today.day)]))
+ try :
+ print str(jDateModel.objects.filter(date__how =["%s-%s-%s"%(today.year, today.month, today.day)]))
+ except :
+ pass
+
+
+
+
+ #wrong day value
+ with self.assertRaises(exceptions.ValidationError):
+ jDateModel.objects.filter(date="%s-%s-%s"%(today.year, today.month, 35))
+
+ #invalid format
+ with self.assertRaises(exceptions.ValidationError):
+ jDateModel.objects.filter(date="%s%s/%s"%(today.year, today.month, 35))
+
+ #invalid search
+ with self.assertRaises(ValueError):
+ jDateModel.objects.filter(date__month="12")
+
@@ -0,0 +1,50 @@
+from django.conf import settings
+from django.core.management import call_command
+from django.db.models import loading
+
+NO_SETTING = ('!', None)
+
+class TestSettingsManager(object):
+ """
+ A class which can modify some Django settings temporarily for a
+ test and then revert them to their original values later.
+
+ Automatically handles resyncing the DB if INSTALLED_APPS is
+ modified.
+
+ """
+ def __init__(self):
+ self._original_settings = {}
+
+ def set(self, **kwargs):
+ for k,v in kwargs.iteritems():
+ self._original_settings.setdefault(k, getattr(settings, k,
+ NO_SETTING))
+ setattr(settings, k, v)
+ if 'INSTALLED_APPS' in kwargs:
+ self.syncdb()
+
+ def syncdb(self):
+ loading.cache.loaded = False
+ call_command('syncdb', verbosity=1)
+
+ def revert(self):
+ for k,v in self._original_settings.iteritems():
+ if v == NO_SETTING:
+ delattr(settings, k)
+ else:
+ setattr(settings, k, v)
+ if 'INSTALLED_APPS' in self._original_settings:
+ self.syncdb()
+ self._original_settings = {}
+
+def change_settings(append_apps) :
+ settings_manager = TestSettingsManager()
+ s = []
+ for st in settings.INSTALLED_APPS:
+ s.append(st)
+ for app in append_apps :
+ s.append(app)
+ settings_manager.set(INSTALLED_APPS = s)
+ return settings_manager
+
@@ -0,0 +1,10 @@
+from django.db import models
+from django_jalali.db import models as jmodels
+
+class jDateModel(models.Model):
+ objects = jmodels.jManager()
+ date = jmodels.jDateField(auto_now=True)
+
+class jDateTimeModel(models.Model):
+ objects = jmodels.jManager()
+ date_time = jmodels.jDateTimeField()
View
@@ -1,8 +1,8 @@
from setuptools import setup, find_packages
setup(
- name='django_jalali',
- version='0.9',
+ name='django-jalali',
+ version='1.0',
packages=find_packages(),
description = ("Jalali Date support for Django model and admin interface"),
url = 'http://github.com/slashmili/django-jalali',
@@ -12,6 +12,6 @@
keywords = "django jalali",
license='Python Software Foundation License',
platforms='any',
- requires = ["jdatetime","django"],
+ install_requires = ["jdatetime","django"],
long_description=open('README').read()
)

0 comments on commit 4ef6c91

Please sign in to comment.