1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
|
import os
import pytest
from django import forms
from django.db import models
from timezone_field import TimeZoneField, backends
USA_TZS = [
"US/Alaska",
"US/Arizona",
"US/Central",
"US/Eastern",
"US/Hawaii",
"US/Mountain",
"US/Pacific",
]
# we have to define these Models at import time, so django will create the DB table
# we then redefine-the model at runtime to customize the fields further
class _Model(models.Model):
tz = TimeZoneField()
tz_opt = TimeZoneField()
tz_opt_default = TimeZoneField()
class _ModelChoice(models.Model):
tz_superset = TimeZoneField()
tz_subset = TimeZoneField()
class _ModelOldChoiceFormat(models.Model):
tz_superset = TimeZoneField()
tz_subset = TimeZoneField()
@pytest.fixture
def Model(use_pytz):
class _Model(models.Model):
tz = TimeZoneField(use_pytz=use_pytz)
tz_opt = TimeZoneField(blank=True, use_pytz=use_pytz)
tz_opt_default = TimeZoneField(blank=True, default="America/Los_Angeles", use_pytz=use_pytz)
yield _Model
@pytest.fixture
def ModelChoice(use_pytz, all_tzstrs):
class _ModelChoice(models.Model):
tz_superset = TimeZoneField(
choices=[(tz, tz) for tz in all_tzstrs],
blank=True,
use_pytz=use_pytz,
)
tz_subset = TimeZoneField(
choices=[(tz, tz) for tz in USA_TZS],
blank=True,
use_pytz=use_pytz,
)
yield _ModelChoice
@pytest.fixture
def ModelOldChoiceFormat(use_pytz, all_tzstrs, to_tzobj):
class _ModelOldChoiceFormat(models.Model):
tz_superset = TimeZoneField(
choices=[(to_tzobj(tz), tz) for tz in all_tzstrs],
blank=True,
use_pytz=use_pytz,
)
tz_subset = TimeZoneField(
choices=[(to_tzobj(tz), tz) for tz in USA_TZS],
blank=True,
use_pytz=use_pytz,
)
yield _ModelOldChoiceFormat
@pytest.fixture
def ModelForm(Model):
class _ModelForm(forms.ModelForm):
class Meta:
model = Model
fields = "__all__"
yield _ModelForm
@pytest.fixture(params=[(os.environ["TZ_ENGINE"])] if "TZ_ENGINE" in os.environ else ["pytz", "zoneinfo"])
def use_pytz(request):
yield request.param == "pytz"
@pytest.fixture
def to_tzobj(use_pytz):
tz_backend = backends.get_tz_backend(use_pytz)
yield tz_backend.to_tzobj
@pytest.fixture
def utc_tzobj(use_pytz):
tz_backend = backends.get_tz_backend(use_pytz)
yield tz_backend.utc_tzobj
@pytest.fixture
def all_tzstrs(use_pytz):
tz_backend = backends.get_tz_backend(use_pytz)
yield tz_backend.all_tzstrs
@pytest.fixture
def base_tzstrs(use_pytz):
tz_backend = backends.get_tz_backend(use_pytz)
yield tz_backend.base_tzstrs
@pytest.fixture
def pst():
yield "America/Los_Angeles"
@pytest.fixture
def pst_tz(to_tzobj, pst):
yield to_tzobj(pst) # for pytz: pytz.tzinfo.DstTzInfo
@pytest.fixture
def gmt():
yield "GMT"
@pytest.fixture
def gmt_tz(to_tzobj, gmt):
yield to_tzobj(gmt) # for pytz: pytz.tzinfo.StaticTzInfo
@pytest.fixture
def utc():
yield "UTC"
@pytest.fixture
def utc_tz(utc_tzobj):
yield utc_tzobj # for pytz: pytz.utc singleton
@pytest.fixture
def uncommon_tz(use_pytz):
# there are no Zoneinfo "uncommon" tzs
yield "Singapore" if use_pytz else "foobar"
@pytest.fixture
def invalid_tz():
yield "foobar"
|