def test_timezones(self): try: os.environ['TZ'] = 'EST+05EDT,M4.1.0,M10.5.0' time.tzset() con = self._connect() cursor = con.cursor() cursor.execute("drop table typetest if exists") cursor.execute( "create table typetest (id integer GENERATED ALWAYS AS IDENTITY, timestamp_col timestamp)" ) vals = (pynuodb.Timestamp(2013, 5, 24, 0, 0, 1), ) cursor.execute("insert into typetest (timestamp_col) values (?)", vals) con.commit() con.close() os.environ['TZ'] = 'PST+08PDT,M4.1.0,M10.5.0' time.tzset() con = self._connect() cursor = con.cursor() cursor.execute("select * from typetest") row = cursor.fetchone() self.assertEqual(vals[0].year, row[1].year) self.assertEqual(vals[0].month, row[1].month) self.assertEqual(vals[0].day, row[1].day + 1) self.assertEqual(vals[0].hour, (row[1].hour + 3) % 24) self.assertEqual(vals[0].minute, row[1].minute) self.assertEqual(vals[0].second, row[1].second) self.assertEqual(vals[0].microsecond, row[1].microsecond) con.close() os.environ['TZ'] = 'CET-01CST,M4.1.0,M10.5.0' time.tzset() con = self._connect() cursor = con.cursor() cursor.execute("select * from typetest") row = cursor.fetchone() self.assertEqual(vals[0].year, row[1].year) self.assertEqual(vals[0].month, row[1].month) self.assertEqual(vals[0].day, row[1].day) self.assertEqual(vals[0].hour, (row[1].hour - 6) % 24) self.assertEqual(vals[0].minute, row[1].minute) self.assertEqual(vals[0].second, row[1].second) self.assertEqual(vals[0].microsecond, row[1].microsecond) cursor.execute("drop table typetest if exists") con.close() finally: try: os.environ.pop('TZ') except: pass
def test_param_date_types(self): con = self._connect() cursor = con.cursor() cursor.execute("drop table typetest if exists") try: cursor.execute( "create table typetest (id integer GENERATED ALWAYS AS IDENTITY, date_col date, " + "time_col time, timestamp_col timestamp)") test_vals = (pynuodb.Date(1970, 1, 1), pynuodb.Time(0, 0, 0), pynuodb.Timestamp(2010, 12, 31, 19, 0, 0)) cursor.execute( "insert into typetest (date_col, time_col, timestamp_col) " + "values (?, ?, ?)", test_vals) con.commit() cursor.execute("select * from typetest order by id desc limit 1") row = cursor.fetchone() self.assertIsInstance(row[1], pynuodb.Date) self.assertIsInstance(row[2], pynuodb.Time) self.assertIsInstance(row[3], pynuodb.Timestamp) self.assertEqual(row[1].year, test_vals[0].year) self.assertEqual(row[1].month, test_vals[0].month) self.assertEqual(row[1].day, test_vals[0].day) self.assertEqual(row[2].hour, test_vals[1].hour) self.assertEqual(row[2].minute, test_vals[1].minute) self.assertEqual(row[2].second, test_vals[1].second) self.assertEqual(row[2].microsecond, test_vals[1].microsecond) self.assertEqual(row[3].year, test_vals[2].year) self.assertEqual(row[3].month, test_vals[2].month) self.assertEqual(row[3].day, test_vals[2].day) self.assertEqual(row[3].hour, test_vals[2].hour) self.assertEqual(row[3].minute, test_vals[2].minute) self.assertEqual(row[3].second, test_vals[2].second) self.assertEqual(row[3].microsecond, test_vals[2].microsecond) finally: try: cursor.execute("drop table typetest if exists") finally: con.close()
def test_all_types(self): con = self._connect() cursor = con.cursor() cursor.execute("drop table typetest if exists") try: cursor.execute( "create table typetest (id integer GENERATED ALWAYS AS IDENTITY, binary_col binary(10), " + "bool_col boolean, timestamp_col timestamp, time_col time, date_col date, string_col string, " + "varchar_col varchar(10), char_col char(10), smallint_col smallint, integer_col integer, bigint_col bigint, " + "numeric_col numeric(10, 2), decimal_col decimal(10, 2), number_col number, double_col double, clob_col clob, blob_col blob)" ) vals = ( pynuodb.Binary("binary"), False, pynuodb.Timestamp(1990, 12, 31, 19, 0, 0), pynuodb.Time(10, 30, 44), pynuodb.Date(1998, 1, 1), "this", "is a", "test", -13546, 156465465, -3135135132132104354, decimal.Decimal('-354564.12'), decimal.Decimal('77788864.6'), decimal.Decimal('-46543213.01324654'), -999.999999, "The test", pynuodb.Binary("test"), ) cursor.execute( "insert into typetest (binary_col, bool_col, timestamp_col, time_col, date_col, string_col, " + "varchar_col, char_col, smallint_col, integer_col, bigint_col, numeric_col, decimal_col, " + "number_col, double_col, clob_col, blob_col) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", vals) con.commit() cursor.execute("select * from typetest order by id desc limit 1") row = cursor.fetchone() for i in range(1, 3): self.assertEqual(row[i], vals[i - 1]) self.assertIsInstance(row[3], pynuodb.Timestamp) self.assertIsInstance(row[4], pynuodb.Time) self.assertIsInstance(row[5], pynuodb.Date) self.assertEqual(row[3].year, vals[2].year) self.assertEqual(row[3].month, vals[2].month) self.assertEqual(row[3].day, vals[2].day) self.assertEqual(row[3].hour, vals[2].hour) self.assertEqual(row[3].minute, vals[2].minute) self.assertEqual(row[3].second, vals[2].second) self.assertEqual(row[3].microsecond, vals[2].microsecond) self.assertEqual(row[4].hour, vals[3].hour) self.assertEqual(row[4].minute, vals[3].minute) self.assertEqual(row[4].second, vals[3].second) self.assertEqual(row[4].microsecond, vals[3].microsecond) self.assertEqual(row[5].year, vals[4].year) self.assertEqual(row[5].month, vals[4].month) self.assertEqual(row[5].day, vals[4].day) for i in range(6, len(row)): self.assertEqual(row[i], vals[i - 1]) finally: try: cursor.execute("drop table typetest if exists") finally: con.close()
def test_date_types(self): con = self._connect() cursor = con.cursor() cursor.execute("drop table typetest if exists") try: cursor.execute( "create table typetest (id integer GENERATED ALWAYS AS IDENTITY, date_col date, " + "time_col time, timestamp_col_EDT timestamp, timestamp_col_EST timestamp)" ) test_vals = ( pynuodb.Date(2008, 1, 1), pynuodb.Time(8, 13, 34), pynuodb.Timestamp(2014, 12, 19, 14, 8, 30, 99, Local), pynuodb.Timestamp(2014, 7, 23, 6, 22, 19, 88, Local), ) exc_str = ("insert into typetest (" "date_col, " "time_col, " "timestamp_col_EDT, " "timestamp_col_EST) " "values (?, ?, ?, ?)") cursor.execute(exc_str, test_vals) cursor.execute("select * from typetest order by id desc limit 1") row = cursor.fetchone() self.assertIsInstance(row[1], pynuodb.Date) self.assertIsInstance(row[2], pynuodb.Time) self.assertIsInstance(row[3], pynuodb.Timestamp) self.assertIsInstance(row[4], pynuodb.Timestamp) self.assertEqual(test_vals[2] - test_vals[3], row[3] - row[4]) self.assertEqual(row[1].year, test_vals[0].year) self.assertEqual(row[1].month, test_vals[0].month) self.assertEqual(row[1].day, test_vals[0].day) self.assertEqual(row[2].hour, test_vals[1].hour) self.assertEqual(row[2].minute, test_vals[1].minute) self.assertEqual(row[2].second, test_vals[1].second) self.assertEqual(row[2].microsecond, test_vals[1].microsecond) self.assertEqual(row[3].year, test_vals[2].year) self.assertEqual(row[3].month, test_vals[2].month) self.assertEqual(row[3].day, test_vals[2].day) self.assertEqual(row[3].hour, test_vals[2].hour) self.assertEqual(row[3].minute, test_vals[2].minute) self.assertEqual(row[3].second, test_vals[2].second) self.assertEqual(row[3].microsecond, test_vals[2].microsecond) self.assertEqual(row[4].year, test_vals[3].year) self.assertEqual(row[4].month, test_vals[3].month) self.assertEqual(row[4].day, test_vals[3].day) self.assertEqual(row[4].hour, test_vals[3].hour) self.assertEqual(row[4].minute, test_vals[3].minute) self.assertEqual(row[4].second, test_vals[3].second) self.assertEqual(row[4].microsecond, test_vals[3].microsecond) finally: try: cursor.execute("drop table typetest if exists") finally: con.close()