diff options
| author | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2017-02-24 03:48:41 +0100 |
|---|---|---|
| committer | Daniele Varrazzo <daniele.varrazzo@gmail.com> | 2017-02-24 03:48:41 +0100 |
| commit | 834e9996dac55b230d26bc5b3a3d7c56e9bf92e2 (patch) | |
| tree | 764742ebb4eba01d4967dfea51f121841ca24c1b /tests/test_dates.py | |
| parent | e351606b692439ad99a57d4c21c16f29b14f11c3 (diff) | |
| download | psycopg2-834e9996dac55b230d26bc5b3a3d7c56e9bf92e2.tar.gz | |
Parse interval only using integers
(almost... except for micros rounding)
While this is probably an improvement on the previous implementation,
I am largely waving a dead chicken at windows, which keeps failing to
pass the seconds overflow test. If it doesn't pass now either I'll start
blaming Python's timedelta.
Diffstat (limited to 'tests/test_dates.py')
| -rwxr-xr-x | tests/test_dates.py | 25 |
1 files changed, 21 insertions, 4 deletions
diff --git a/tests/test_dates.py b/tests/test_dates.py index 1000997..bffd05d 100755 --- a/tests/test_dates.py +++ b/tests/test_dates.py @@ -28,6 +28,11 @@ from psycopg2.tz import FixedOffsetTimezone, ZERO from testutils import unittest, ConnectingTestCase, skip_before_postgres +def total_seconds(d): + """Return total number of seconds of a timedelta as a float.""" + return d.days * 24 * 60 * 60 + d.seconds + d.microseconds / 1000000.0 + + class CommonDatetimeTestsMixin: def execute(self, *args): @@ -334,10 +339,6 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin): self.assertEqual(t, time(0, 0, tzinfo=FixedOffsetTimezone(330))) def test_large_interval(self): - def total_seconds(d): - """Return total number of seconds of a timedelta as a float.""" - return d.days * 24 * 60 * 60 + d.seconds + d.microseconds / 1000000.0 - t = self.execute("select '999999:00:00'::interval") self.assertEqual(total_seconds(t), 999999 * 60 * 60) @@ -356,6 +357,22 @@ class DatetimeTests(ConnectingTestCase, CommonDatetimeTestsMixin): t = self.execute("select '-999999:00:00.9'::interval") self.assertEqual(total_seconds(t), -999999 * 60 * 60 - 0.9) + def test_micros_rounding(self): + t = self.execute("select '0.1'::interval") + self.assertEqual(total_seconds(t), 0.1) + + t = self.execute("select '0.01'::interval") + self.assertEqual(total_seconds(t), 0.01) + + 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) + # Only run the datetime tests if psycopg was compiled with support. if not hasattr(psycopg2.extensions, 'PYDATETIME'): |
