Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

added unit test for djando jalali models

  • Loading branch information...
commit 4ef6c91a90d53fe1cb85cc9a274768990258a306 1 parent eafffef
Milad authored
4 django_jalali/forms/__init__.py
@@ -3,7 +3,7 @@
3 3 import time
4 4 import datetime
5 5 import jdatetime
6   -from django.core import validators
  6 +from django.core import validators, exceptions
7 7 from django.utils import datetime_safe, formats
8 8 from django.utils.translation import ugettext as _
9 9 from widgets import *
@@ -33,7 +33,7 @@ def to_python(self, value):
33 33 return jdatetime.date(*time.strptime(value, format)[:3])
34 34 except ValueError:
35 35 continue
36   - raise ValidationError(self.error_messages['invalid'])
  36 + raise exceptions.ValidationError(self.error_messages['invalid'])
37 37
38 38
39 39 class jDateTimeField(forms.Field):
0  django_jalali/models.py
No changes.
120 django_jalali/tests/__init__.py
... ... @@ -0,0 +1,120 @@
  1 +from django.test import TestCase
  2 +from django.core import exceptions
  3 +from django.db.utils import IntegrityError
  4 +import jdatetime
  5 +import datetime
  6 +
  7 +
  8 +from django_jalali.tests.models import jDateModel, jDateTimeModel
  9 +
  10 +from custom_settings import change_settings
  11 +settings_manager = change_settings(append_apps=['django_jalali.tests'])
  12 +
  13 +
  14 +
  15 +
  16 +class CreateModelTestCase(TestCase):
  17 + def tearDown(self):
  18 + global settings_manager
  19 + settings_manager.revert()
  20 +
  21 + def test_simple_save(self):
  22 + """test simple function, including saving and retrieving jalali date from database"""
  23 + jd_model = jDateModel(date=jdatetime.date.today())
  24 + jd_model.save()
  25 + d = jDateModel.objects.filter(date=jdatetime.date.today())
  26 + self.assertEqual(d[0].date.togregorian(), datetime.date.today())
  27 +
  28 + j_date_time = jdatetime.datetime.today()
  29 + jdt_model = jDateTimeModel(date_time=j_date_time)
  30 + jdt_model.save()
  31 + dt = jDateTimeModel.objects.filter(date_time=j_date_time)
  32 + self.assertEqual(dt[0].date_time.togregorian(), j_date_time.togregorian())
  33 + def test_diffrent_saveing(self):
  34 + jd_model = jDateModel(date='1391-01-01')
  35 + jd_model.save()
  36 +
  37 + jd_model = jDateModel(date=datetime.datetime.now())
  38 + jd_model.save()
  39 +
  40 + jd_model = jDateModel()
  41 + jd_model.save()
  42 +
  43 + jd_model = jDateModel(date=jdatetime.datetime.now())
  44 + jd_model.save()
  45 +
  46 + #to_python
  47 + jdt_model = jDateTimeModel(date_time='1391-01-01')
  48 + #TODO: Find the correct exceptions
  49 + #with self.assertRaises(Exception):
  50 + # jdt_model.date_time = None
  51 +
  52 + jdt_model.date_time = datetime.datetime(1390,01,02)
  53 + jdt_model.save()
  54 + self.assertEqual(jDateTimeModel.objects.filter(date_time=datetime.datetime(1390,01,02))[0].date_time , jdt_model.date_time)
  55 +
  56 + jdt_model.date_time = jdatetime.date.today()
  57 + jdt_model.save()
  58 + self.assertEqual(jDateTimeModel.objects.filter(date_time=jdatetime.date.today())[0].date_time, jdt_model.date_time)
  59 +
  60 + jdt_model.date_time = datetime.date.today()
  61 + jdt_model.save()
  62 + self.assertEqual(jDateTimeModel.objects.filter(date_time=datetime.date.today())[0].date_time, jdt_model.date_time)
  63 +
  64 + jdt_model.date_time = datetime.date(1390,01,01)
  65 + jdt_model.save()
  66 + self.assertEqual(jDateTimeModel.objects.filter(date_time=jdatetime.date(1390,01,01))[0].date_time, jdt_model.date_time)
  67 +
  68 + jdt_model.date_time = '1391-01-01 12:12'
  69 + jdt_model.save()
  70 + self.assertEqual(jDateTimeModel.objects.filter(date_time=jdatetime.datetime(1391,01,01,12,12))[0].date_time, jdt_model.date_time)
  71 +
  72 +
  73 + jdt_model.date_time = '1391-01-01 12:12:12'
  74 + jdt_model.save()
  75 + self.assertEqual(jDateTimeModel.objects.filter(date_time=jdatetime.datetime(1391,01,01,12,12,12))[0].date_time, jdt_model.date_time)
  76 +
  77 + jdt_model.date_time = '2012-01-01 12:12'
  78 + jdt_model.save()
  79 + #TODO: fixus, we'r broken
  80 + #self.assertEqual(jDateTimeModel.objects.filter(date_time='2012-01-01 12:12')[0].date_time, jdt_model.date_time)
  81 + #self.assertEqual(jDateTimeModel.objects.filter(date_time=datetime.datetime(2012, 01, 01, 12, 12))[0].date_time, jdt_model.date_time)
  82 + jdt_model.date_time = '2012-01-01 12:12:12'
  83 +
  84 + jdt_model.date_time = '1391-01-01 12:12:12.2222'
  85 + jdt_model.save()
  86 + self.assertEqual(jDateTimeModel.objects.filter(date_time = '1391-01-01 12:12:12.2222')[0] , jdt_model)
  87 +
  88 + def test_filtering(self):
  89 + """test filtering"""
  90 + today = jdatetime.date.today()
  91 + jd_model = jDateModel(date=today)
  92 + jd_model.save()
  93 + dt = jDateModel.objects.filter(date__year=today.year)
  94 + print jDateModel.objects.filter(date="%s-%s-%s"%(today.year, today.month, today.day))
  95 + today = datetime.date.today()
  96 + jd_model = jDateModel(date=today)
  97 + jd_model.save()
  98 + print jDateModel.objects.filter(date=today)
  99 + print jDateModel.objects.filter(date="%s-%s-%s"%(today.year, today.month, today.day))
  100 + print str(jDateModel.objects.filter(date__in =["%s-%s-%s"%(today.year, today.month, today.day)]))
  101 + try :
  102 + print str(jDateModel.objects.filter(date__how =["%s-%s-%s"%(today.year, today.month, today.day)]))
  103 + except :
  104 + pass
  105 +
  106 +
  107 +
  108 +
  109 + #wrong day value
  110 + with self.assertRaises(exceptions.ValidationError):
  111 + jDateModel.objects.filter(date="%s-%s-%s"%(today.year, today.month, 35))
  112 +
  113 + #invalid format
  114 + with self.assertRaises(exceptions.ValidationError):
  115 + jDateModel.objects.filter(date="%s%s/%s"%(today.year, today.month, 35))
  116 +
  117 + #invalid search
  118 + with self.assertRaises(ValueError):
  119 + jDateModel.objects.filter(date__month="12")
  120 +
50 django_jalali/tests/custom_settings.py
... ... @@ -0,0 +1,50 @@
  1 +from django.conf import settings
  2 +from django.core.management import call_command
  3 +from django.db.models import loading
  4 +
  5 +NO_SETTING = ('!', None)
  6 +
  7 +class TestSettingsManager(object):
  8 + """
  9 + A class which can modify some Django settings temporarily for a
  10 + test and then revert them to their original values later.
  11 +
  12 + Automatically handles resyncing the DB if INSTALLED_APPS is
  13 + modified.
  14 +
  15 + """
  16 + def __init__(self):
  17 + self._original_settings = {}
  18 +
  19 + def set(self, **kwargs):
  20 + for k,v in kwargs.iteritems():
  21 + self._original_settings.setdefault(k, getattr(settings, k,
  22 + NO_SETTING))
  23 + setattr(settings, k, v)
  24 + if 'INSTALLED_APPS' in kwargs:
  25 + self.syncdb()
  26 +
  27 + def syncdb(self):
  28 + loading.cache.loaded = False
  29 + call_command('syncdb', verbosity=1)
  30 +
  31 + def revert(self):
  32 + for k,v in self._original_settings.iteritems():
  33 + if v == NO_SETTING:
  34 + delattr(settings, k)
  35 + else:
  36 + setattr(settings, k, v)
  37 + if 'INSTALLED_APPS' in self._original_settings:
  38 + self.syncdb()
  39 + self._original_settings = {}
  40 +
  41 +def change_settings(append_apps) :
  42 + settings_manager = TestSettingsManager()
  43 + s = []
  44 + for st in settings.INSTALLED_APPS:
  45 + s.append(st)
  46 + for app in append_apps :
  47 + s.append(app)
  48 + settings_manager.set(INSTALLED_APPS = s)
  49 + return settings_manager
  50 +
10 django_jalali/tests/models.py
... ... @@ -0,0 +1,10 @@
  1 +from django.db import models
  2 +from django_jalali.db import models as jmodels
  3 +
  4 +class jDateModel(models.Model):
  5 + objects = jmodels.jManager()
  6 + date = jmodels.jDateField(auto_now=True)
  7 +
  8 +class jDateTimeModel(models.Model):
  9 + objects = jmodels.jManager()
  10 + date_time = jmodels.jDateTimeField()
6 setup.py
... ... @@ -1,8 +1,8 @@
1 1 from setuptools import setup, find_packages
2 2
3 3 setup(
4   - name='django_jalali',
5   - version='0.9',
  4 + name='django-jalali',
  5 + version='1.0',
6 6 packages=find_packages(),
7 7 description = ("Jalali Date support for Django model and admin interface"),
8 8 url = 'http://github.com/slashmili/django-jalali',
@@ -12,6 +12,6 @@
12 12 keywords = "django jalali",
13 13 license='Python Software Foundation License',
14 14 platforms='any',
15   - requires = ["jdatetime","django"],
  15 + install_requires = ["jdatetime","django"],
16 16 long_description=open('README').read()
17 17 )

0 comments on commit 4ef6c91

Please sign in to comment.
Something went wrong with that request. Please try again.