mirror of
https://github.com/Infinidat/infi.clickhouse_orm.git
synced 2024-11-25 10:13:45 +03:00
Fix datetime tests
This commit is contained in:
parent
b2a5482a65
commit
015a4512e7
|
@ -425,7 +425,6 @@ After cloning the project, run the following commands::
|
||||||
To run the tests, ensure that the ClickHouse server is running on http://localhost:8123/ (this is the default), and run::
|
To run the tests, ensure that the ClickHouse server is running on http://localhost:8123/ (this is the default), and run::
|
||||||
|
|
||||||
bin/nosetests
|
bin/nosetests
|
||||||
=======
|
|
||||||
|
|
||||||
To see test coverage information run::
|
To see test coverage information run::
|
||||||
|
|
||||||
|
|
|
@ -132,6 +132,12 @@ class DateTimeField(Field):
|
||||||
if isinstance(value, string_types):
|
if isinstance(value, string_types):
|
||||||
if value == '0000-00-00 00:00:00':
|
if value == '0000-00-00 00:00:00':
|
||||||
return self.class_default
|
return self.class_default
|
||||||
|
if len(value) == 10:
|
||||||
|
try:
|
||||||
|
value = int(value)
|
||||||
|
return datetime.datetime.utcfromtimestamp(value).replace(tzinfo=pytz.utc)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
dt = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
|
dt = datetime.datetime.strptime(value, '%Y-%m-%d %H:%M:%S')
|
||||||
return timezone_in_use.localize(dt).astimezone(pytz.utc)
|
return timezone_in_use.localize(dt).astimezone(pytz.utc)
|
||||||
raise ValueError('Invalid value for %s - %r' % (self.__class__.__name__, value))
|
raise ValueError('Invalid value for %s - %r' % (self.__class__.__name__, value))
|
||||||
|
|
|
@ -6,32 +6,17 @@ import pytz
|
||||||
|
|
||||||
class SimpleFieldsTest(unittest.TestCase):
|
class SimpleFieldsTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_date_field(self):
|
|
||||||
f = DateField()
|
|
||||||
# Valid values
|
|
||||||
for value in (date(1970, 1, 1), datetime(1970, 1, 1), '1970-01-01', '0000-00-00', 0):
|
|
||||||
self.assertEquals(f.to_python(value, pytz.utc), date(1970, 1, 1))
|
|
||||||
# Invalid values
|
|
||||||
for value in ('nope', '21/7/1999', 0.5):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
f.to_python(value, pytz.utc)
|
|
||||||
# Range check
|
|
||||||
for value in (date(1900, 1, 1), date(2900, 1, 1)):
|
|
||||||
with self.assertRaises(ValueError):
|
|
||||||
f.validate(value)
|
|
||||||
|
|
||||||
def test_datetime_field(self):
|
def test_datetime_field(self):
|
||||||
f = DateTimeField()
|
f = DateTimeField()
|
||||||
epoch = datetime(1970, 1, 1, tzinfo=pytz.utc)
|
epoch = datetime(1970, 1, 1, tzinfo=pytz.utc)
|
||||||
# Valid values
|
# Valid values
|
||||||
for value in (date(1970, 1, 1), datetime(1970, 1, 1), epoch,
|
for value in (date(1970, 1, 1), datetime(1970, 1, 1), epoch,
|
||||||
epoch.astimezone(pytz.timezone('US/Eastern')), epoch.astimezone(pytz.timezone('Asia/Jerusalem')),
|
epoch.astimezone(pytz.timezone('US/Eastern')), epoch.astimezone(pytz.timezone('Asia/Jerusalem')),
|
||||||
'1970-01-01 00:00:00', '0000-00-00 00:00:00', 0):
|
'1970-01-01 00:00:00', '1970-01-17 00:00:17', '0000-00-00 00:00:00', 0):
|
||||||
dt = f.to_python(value, pytz.utc)
|
dt = f.to_python(value, pytz.utc)
|
||||||
self.assertEquals(dt.tzinfo, pytz.utc)
|
self.assertEquals(dt.tzinfo, pytz.utc)
|
||||||
self.assertEquals(dt, epoch)
|
|
||||||
# Verify that conversion to and from db string does not change value
|
# Verify that conversion to and from db string does not change value
|
||||||
dt2 = f.to_python(int(f.to_db_string(dt)), pytz.utc)
|
dt2 = f.to_python(f.to_db_string(dt, quote=False), pytz.utc)
|
||||||
self.assertEquals(dt, dt2)
|
self.assertEquals(dt, dt2)
|
||||||
# Invalid values
|
# Invalid values
|
||||||
for value in ('nope', '21/7/1999', 0.5):
|
for value in ('nope', '21/7/1999', 0.5):
|
||||||
|
@ -52,6 +37,10 @@ class SimpleFieldsTest(unittest.TestCase):
|
||||||
for value in ('nope', '21/7/1999', 0.5):
|
for value in ('nope', '21/7/1999', 0.5):
|
||||||
with self.assertRaises(ValueError):
|
with self.assertRaises(ValueError):
|
||||||
f.to_python(value, pytz.utc)
|
f.to_python(value, pytz.utc)
|
||||||
|
# Range check
|
||||||
|
for value in (date(1900, 1, 1), date(2900, 1, 1)):
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
f.validate(value)
|
||||||
|
|
||||||
def test_date_field_timezone(self):
|
def test_date_field_timezone(self):
|
||||||
# Verify that conversion of timezone-aware datetime is correct
|
# Verify that conversion of timezone-aware datetime is correct
|
||||||
|
|
Loading…
Reference in New Issue
Block a user