mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-22 00:46:33 +03:00
Interval tests refactored to reduce verbosity
This commit is contained in:
parent
2b5e131831
commit
45f7ec73be
|
@ -205,19 +205,23 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
self.DATETIME("2007-01-01 13:30:29.123456", self.curs).tzinfo, None)
|
self.DATETIME("2007-01-01 13:30:29.123456", self.curs).tzinfo, None)
|
||||||
|
|
||||||
|
def _check_interval(self, s, want_secs=None):
|
||||||
|
self.curs.execute(
|
||||||
|
"select %s::interval, extract('epoch' from %s::interval)",
|
||||||
|
(s, s))
|
||||||
|
td, secs = self.curs.fetchone()
|
||||||
|
if want_secs is not None:
|
||||||
|
secs = want_secs
|
||||||
|
|
||||||
|
self.assertEqual(total_seconds(td), secs,
|
||||||
|
"'%s'::interval -> '%s' = %s sec instead of %s"
|
||||||
|
% (s, td, total_seconds(td), secs))
|
||||||
|
|
||||||
def test_parse_interval(self):
|
def test_parse_interval(self):
|
||||||
value = self.INTERVAL('42 days 12:34:56.123456', self.curs)
|
self._check_interval('42 days 12:34:56.123456')
|
||||||
self.assertNotEqual(value, None)
|
|
||||||
self.assertEqual(value.days, 42)
|
|
||||||
self.assertEqual(value.seconds, 45296)
|
|
||||||
self.assertEqual(value.microseconds, 123456)
|
|
||||||
|
|
||||||
def test_parse_negative_interval(self):
|
def test_parse_negative_interval(self):
|
||||||
value = self.INTERVAL('-42 days -12:34:56.123456', self.curs)
|
self._check_interval('-42 days -12:34:56.123456')
|
||||||
self.assertNotEqual(value, None)
|
|
||||||
self.assertEqual(value.days, -43)
|
|
||||||
self.assertEqual(value.seconds, 41103)
|
|
||||||
self.assertEqual(value.microseconds, 876544)
|
|
||||||
|
|
||||||
def test_parse_infinity(self):
|
def test_parse_infinity(self):
|
||||||
value = self.DATETIME('-infinity', self.curs)
|
value = self.DATETIME('-infinity', self.curs)
|
||||||
|
@ -340,39 +344,19 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin):
|
||||||
|
|
||||||
@skip_before_postgres(8, 1)
|
@skip_before_postgres(8, 1)
|
||||||
def test_large_interval(self):
|
def test_large_interval(self):
|
||||||
t = self.execute("select '999999:00:00'::interval")
|
self._check_interval('999999:00:00')
|
||||||
self.assertEqual(total_seconds(t), 999999 * 60 * 60)
|
self._check_interval('-999999:00:00')
|
||||||
|
self._check_interval('999999:00:00.1')
|
||||||
t = self.execute("select '-999999:00:00'::interval")
|
self._check_interval('999999:00:00.9')
|
||||||
self.assertEqual(total_seconds(t), -999999 * 60 * 60)
|
self._check_interval('-999999:00:00.1')
|
||||||
|
self._check_interval('-999999:00:00.9')
|
||||||
t = self.execute("select '999999:00:00.1'::interval")
|
|
||||||
self.assertEqual(total_seconds(t), 999999 * 60 * 60 + 0.1)
|
|
||||||
|
|
||||||
t = self.execute("select '999999:00:00.9'::interval")
|
|
||||||
self.assertEqual(total_seconds(t), 999999 * 60 * 60 + 0.9)
|
|
||||||
|
|
||||||
t = self.execute("select '-999999:00:00.1'::interval")
|
|
||||||
self.assertEqual(total_seconds(t), -999999 * 60 * 60 - 0.1)
|
|
||||||
|
|
||||||
t = self.execute("select '-999999:00:00.9'::interval")
|
|
||||||
self.assertEqual(total_seconds(t), -999999 * 60 * 60 - 0.9)
|
|
||||||
|
|
||||||
def test_micros_rounding(self):
|
def test_micros_rounding(self):
|
||||||
t = self.execute("select '0.1'::interval")
|
self._check_interval('0.1')
|
||||||
self.assertEqual(total_seconds(t), 0.1)
|
self._check_interval('0.01')
|
||||||
|
self._check_interval('0.000001')
|
||||||
t = self.execute("select '0.01'::interval")
|
self._check_interval('0.0000004', 0)
|
||||||
self.assertEqual(total_seconds(t), 0.01)
|
self._check_interval('0.0000006', 1e-6)
|
||||||
|
|
||||||
t = self.execute("select '0.000001'::interval")
|
|
||||||
self.assertEqual(total_seconds(t), 1e-6)
|
|
||||||
|
|
||||||
t = self.execute("select '0.0000004'::interval")
|
|
||||||
self.assertEqual(total_seconds(t), 0)
|
|
||||||
|
|
||||||
t = self.execute("select '0.0000006'::interval")
|
|
||||||
self.assertEqual(total_seconds(t), 1e-6)
|
|
||||||
|
|
||||||
def test_interval_overflow(self):
|
def test_interval_overflow(self):
|
||||||
cur = self.conn.cursor()
|
cur = self.conn.cursor()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user