Skip to content

Commit 4281dbb

Browse files
author
Michael Barr
committed
Refactored tests to play well with older versions of Django.
1 parent 808a0c9 commit 4281dbb

File tree

16 files changed

+183
-211
lines changed

16 files changed

+183
-211
lines changed

.coveragerc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[run]
2+
omit = timezone_utils/*migrations*

tests/models.py

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# ==============================================================================
2+
# IMPORTS
3+
# ==============================================================================
4+
# ==============================================================================
5+
# IMPORTS
6+
# ==============================================================================
7+
# Python
8+
from __future__ import unicode_literals
9+
from datetime import datetime, time as datetime_time
10+
import pytz
11+
12+
# Django
13+
from django.conf import settings
14+
from django.db import models
15+
from django.utils.translation import ugettext_lazy as _
16+
17+
# App
18+
from timezone_utils.fields import LinkedTZDateTimeField, TimeZoneField
19+
from timezone_utils.choices import PRETTY_ALL_TIMEZONES_CHOICES
20+
21+
22+
# ==============================================================================
23+
# MODELS
24+
# ==============================================================================
25+
class TZWithLowMaxLength(models.Model):
26+
"""Test should check to make sure that the `max_length` is set to the
27+
maximum length of the longest value provided by pytz.all_timezones.
28+
29+
"""
30+
timezone = TimeZoneField(
31+
max_length=15,
32+
null=True,
33+
)
34+
35+
36+
class TZWithBadStringDefault(models.Model):
37+
"""Test should check that a ValidationError is raised when..."""
38+
timezone = TimeZoneField(
39+
default='Bad/Worse',
40+
max_length=64,
41+
null=True,
42+
)
43+
44+
45+
class ModelWithBadTimeOverride(models.Model):
46+
timestamp = LinkedTZDateTimeField(
47+
default=settings.TEST_DATETIME,
48+
time_override='<invalid>',
49+
)
50+
51+
52+
class ModelWithBadPopulateFrom(models.Model):
53+
timezone = TimeZoneField(default='US/Eastern')
54+
timestamp = LinkedTZDateTimeField(
55+
default=settings.TEST_DATETIME,
56+
populate_from='invalid_field_reference',
57+
)
58+
59+
60+
class ModelWithBadTimeZoneCharField(models.Model):
61+
timezone = models.CharField(default='Bad/Worse', max_length=64)
62+
timestamp = LinkedTZDateTimeField(
63+
default=settings.TEST_DATETIME,
64+
populate_from='timezone',
65+
)
66+
67+
68+
class LocationTimeZone(models.Model):
69+
timezone = TimeZoneField(
70+
verbose_name=_('timezone'),
71+
max_length=64,
72+
null=True,
73+
blank=True
74+
)
75+
76+
77+
class LocationTimeZoneChoices(models.Model):
78+
timezone = TimeZoneField(
79+
verbose_name=_('timezone'),
80+
max_length=64,
81+
null=True,
82+
blank=True,
83+
choices=PRETTY_ALL_TIMEZONES_CHOICES,
84+
)
85+
86+
87+
class TZWithGoodStringDefault(models.Model):
88+
"""Test should validate that"""
89+
timezone = TimeZoneField(
90+
default='US/Eastern',
91+
max_length=64,
92+
null=True,
93+
)
94+
95+
96+
class TZWithGoodTZInfoDefault(models.Model):
97+
timezone = TimeZoneField(
98+
default=pytz.timezone('US/Pacific'),
99+
max_length=64,
100+
null=True,
101+
)
102+
103+
104+
class ModelWithDateTimeOnly(models.Model):
105+
timestamp = LinkedTZDateTimeField(
106+
default=settings.TEST_DATETIME,
107+
)
108+
109+
110+
class NullModelWithDateTimeOnly(models.Model):
111+
timestamp = LinkedTZDateTimeField(
112+
null=True,
113+
)
114+
115+
116+
class CallableTimeStampedModel(models.Model):
117+
start = LinkedTZDateTimeField(
118+
default=settings.TEST_DATETIME,
119+
time_override=datetime.min.time
120+
)
121+
end = LinkedTZDateTimeField(
122+
default=settings.TEST_DATETIME,
123+
time_override=datetime.max.time
124+
)
125+
126+
127+
class StaticTimeStampedModel(models.Model):
128+
start = LinkedTZDateTimeField(
129+
default=settings.TEST_DATETIME,
130+
time_override=datetime_time(0, 0)
131+
)
132+
end = LinkedTZDateTimeField(
133+
default=settings.TEST_DATETIME,
134+
time_override=datetime_time(23, 59, 59, 999999)
135+
)
136+
137+
138+
class ModelWithForeignKeyToTimeZone(models.Model):
139+
other_model = models.ForeignKey(
140+
to='tests.TZWithGoodStringDefault',
141+
related_name='fk_to_tz'
142+
)
143+
timestamp = LinkedTZDateTimeField(
144+
default=settings.TEST_DATETIME,
145+
populate_from=lambda instance: instance.other_model.timezone
146+
)
147+
148+
149+
class ModelWithLocalTimeZone(models.Model):
150+
timezone = TimeZoneField(default='US/Eastern')
151+
timestamp = LinkedTZDateTimeField(
152+
default=settings.TEST_DATETIME,
153+
populate_from='timezone'
154+
)
155+
156+
157+
class ModelWithLocalTZCharField(models.Model):
158+
timezone = models.CharField(default='US/Eastern', max_length=64)
159+
timestamp = LinkedTZDateTimeField(
160+
default=settings.TEST_DATETIME,
161+
populate_from='timezone'
162+
)

tests/models/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/models/timezonefield/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/models/timezonefield/invalid.py

Lines changed: 0 additions & 31 deletions
This file was deleted.

tests/models/timezonefield/valid.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

tests/models/tzawaredatetimefield/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

tests/models/tzawaredatetimefield/invalid.py

Lines changed: 0 additions & 38 deletions
This file was deleted.

tests/models/tzawaredatetimefield/valid.py

Lines changed: 0 additions & 76 deletions
This file was deleted.

tests/test_invalid_timezonefield.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from django.test import TestCase
1010

1111
# App
12-
from .models.timezonefield.invalid import (TZWithBadStringDefault,
12+
from tests.models import (TZWithBadStringDefault,
1313
TZWithLowMaxLength)
1414

1515

0 commit comments

Comments
 (0)