|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +# Copyright (c) 2022, Oracle and/or its affiliates. |
| 3 | +# |
| 4 | +# This software is dual-licensed to you under the Universal Permissive License |
| 5 | +# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License |
| 6 | +# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose |
| 7 | +# either license. |
| 8 | +# |
| 9 | +# If you elect to accept the software under the Apache License, Version 2.0, |
| 10 | +# the following applies: |
| 11 | +# |
| 12 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 13 | +# you may not use this file except in compliance with the License. |
| 14 | +# You may obtain a copy of the License at |
| 15 | +# |
| 16 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +# |
| 18 | +# Unless required by applicable law or agreed to in writing, software |
| 19 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +# See the License for the specific language governing permissions and |
| 22 | +# limitations under the License. |
| 23 | +#------------------------------------------------------------------------------ |
| 24 | + |
| 25 | +""" |
| 26 | +4800 - Module for testing timestamp with local time zone variables |
| 27 | +""" |
| 28 | + |
| 29 | +import datetime |
| 30 | + |
| 31 | +import oracledb |
| 32 | +import test_env |
| 33 | + |
| 34 | +class TestCase(test_env.BaseTestCase): |
| 35 | + |
| 36 | + def setUp(self): |
| 37 | + super().setUp() |
| 38 | + self.raw_data = [] |
| 39 | + self.data_by_key = {} |
| 40 | + base_date = datetime.datetime(2022, 6, 2) |
| 41 | + self.cursor.execute("alter session set time_zone = 'UTC'") |
| 42 | + for i in range(1, 11): |
| 43 | + if i % 4 == 0: |
| 44 | + tz_hours = i |
| 45 | + elif i % 2 == 0: |
| 46 | + tz_hours = i + 0.5 |
| 47 | + else: |
| 48 | + tz_hours = -(i + 0.5) |
| 49 | + tz_offset = datetime.timedelta(hours=tz_hours) |
| 50 | + microseconds = int(str(i * 50).ljust(6, "0")) |
| 51 | + offset = datetime.timedelta(days=i, seconds=i * 2, |
| 52 | + microseconds=microseconds) |
| 53 | + col = base_date + tz_offset + offset |
| 54 | + if i % 2: |
| 55 | + tz_offset = datetime.timedelta(hours=6) |
| 56 | + microseconds = int(str(i * 125).ljust(6, "0")) |
| 57 | + offset = datetime.timedelta(days=i + 1, |
| 58 | + seconds=i * 3, |
| 59 | + microseconds=microseconds) |
| 60 | + nullable_col = base_date + offset |
| 61 | + else: |
| 62 | + nullable_col = None |
| 63 | + data_tuple = (i, col, nullable_col) |
| 64 | + self.raw_data.append(data_tuple) |
| 65 | + self.data_by_key[i] = data_tuple |
| 66 | + |
| 67 | + def test_4800_bind_timestamp(self): |
| 68 | + "4800 - test binding in a timestamp" |
| 69 | + self.cursor.setinputsizes(value=oracledb.DB_TYPE_TIMESTAMP_LTZ) |
| 70 | + self.cursor.execute(""" |
| 71 | + select * from TestTimestampLTZs |
| 72 | + where TimestampLTZCol = :value""", |
| 73 | + value=datetime.datetime(2022, 6, 6, 18, 30, 10, 250000)) |
| 74 | + self.assertEqual(self.cursor.fetchall(), [self.data_by_key[5]]) |
| 75 | + |
| 76 | + def test_4801_bind_null(self): |
| 77 | + "4801 - test binding in a null" |
| 78 | + self.cursor.setinputsizes(value=oracledb.DB_TYPE_TIMESTAMP_LTZ) |
| 79 | + self.cursor.execute(""" |
| 80 | + select * from TestTimestampLTZs |
| 81 | + where TimestampLTZCol = :value""", |
| 82 | + value=None) |
| 83 | + self.assertEqual(self.cursor.fetchall(), []) |
| 84 | + |
| 85 | + def test_4802_bind_out_set_input_sizes(self): |
| 86 | + "4802 - test binding out with set input sizes defined" |
| 87 | + bv = self.cursor.setinputsizes(value=oracledb.DB_TYPE_TIMESTAMP_LTZ) |
| 88 | + self.cursor.execute(""" |
| 89 | + begin |
| 90 | + :value := to_timestamp('20220603', 'YYYYMMDD'); |
| 91 | + end;""") |
| 92 | + self.assertEqual(bv["value"].getvalue(), datetime.datetime(2022, 6, 3)) |
| 93 | + |
| 94 | + def test_4803_bind_in_out_set_input_sizes(self): |
| 95 | + "4803 - test binding in/out with set input sizes defined" |
| 96 | + bv = self.cursor.setinputsizes(value=oracledb.DB_TYPE_TIMESTAMP_LTZ) |
| 97 | + self.cursor.execute(""" |
| 98 | + begin |
| 99 | + :value := :value + 5.25; |
| 100 | + end;""", |
| 101 | + value=datetime.datetime(2022, 5, 10, 12, 0, 0)) |
| 102 | + self.assertEqual(bv["value"].getvalue(), |
| 103 | + datetime.datetime(2022, 5, 15, 18, 0, 0)) |
| 104 | + |
| 105 | + def test_4804_bind_out_var(self): |
| 106 | + "4804 - test binding out with cursor.var() method" |
| 107 | + var = self.cursor.var(oracledb.DB_TYPE_TIMESTAMP_LTZ) |
| 108 | + self.cursor.execute(""" |
| 109 | + begin |
| 110 | + :value := to_date('20220601 15:38:12', |
| 111 | + 'YYYYMMDD HH24:MI:SS'); |
| 112 | + end;""", |
| 113 | + value=var) |
| 114 | + self.assertEqual(var.getvalue(), |
| 115 | + datetime.datetime(2022, 6, 1, 15, 38, 12)) |
| 116 | + |
| 117 | + def test_4805_bind_in_out_var_direct_set(self): |
| 118 | + "4805 - test binding in/out with cursor.var() method" |
| 119 | + var = self.cursor.var(oracledb.DB_TYPE_TIMESTAMP_LTZ) |
| 120 | + var.setvalue(0, datetime.datetime(2022, 5, 30, 6, 0, 0)) |
| 121 | + self.cursor.execute(""" |
| 122 | + begin |
| 123 | + :value := :value + 5.25; |
| 124 | + end;""", |
| 125 | + value = var) |
| 126 | + self.assertEqual(var.getvalue(), |
| 127 | + datetime.datetime(2022, 6, 4, 12, 0, 0)) |
| 128 | + |
| 129 | + def test_4806_cursor_description(self): |
| 130 | + "4806 - test cursor description is accurate" |
| 131 | + self.cursor.execute("select * from TestTimestampLTZs") |
| 132 | + expected_value = [ |
| 133 | + ('INTCOL', oracledb.DB_TYPE_NUMBER, 10, None, 9, 0, False), |
| 134 | + ('TIMESTAMPLTZCOL', oracledb.DB_TYPE_TIMESTAMP_LTZ, 23, None, 0, 6, |
| 135 | + False), |
| 136 | + ('NULLABLECOL', oracledb.DB_TYPE_TIMESTAMP_LTZ, 23, None, 0, 6, |
| 137 | + True) |
| 138 | + ] |
| 139 | + self.assertEqual(self.cursor.description, expected_value) |
| 140 | + |
| 141 | + def test_4807_fetchall(self): |
| 142 | + "4807 - test that fetching all of the data returns the correct results" |
| 143 | + self.cursor.execute("select * From TestTimestampLTZs order by IntCol") |
| 144 | + self.assertEqual(self.cursor.fetchall(), self.raw_data) |
| 145 | + self.assertEqual(self.cursor.fetchall(), []) |
| 146 | + |
| 147 | + def test_4808_fetchmany(self): |
| 148 | + "4808 - test that fetching data in chunks returns the correct results" |
| 149 | + self.cursor.execute("select * From TestTimestampLTZs order by IntCol") |
| 150 | + self.assertEqual(self.cursor.fetchmany(3), self.raw_data[0:3]) |
| 151 | + self.assertEqual(self.cursor.fetchmany(2), self.raw_data[3:5]) |
| 152 | + self.assertEqual(self.cursor.fetchmany(4), self.raw_data[5:9]) |
| 153 | + self.assertEqual(self.cursor.fetchmany(3), self.raw_data[9:]) |
| 154 | + self.assertEqual(self.cursor.fetchmany(3), []) |
| 155 | + |
| 156 | + def test_4809_fetchone(self): |
| 157 | + "4809 - test that fetching a single row returns the correct results" |
| 158 | + self.cursor.execute(""" |
| 159 | + select * |
| 160 | + from TestTimestampLTZs |
| 161 | + where IntCol in (3, 4) |
| 162 | + order by IntCol""") |
| 163 | + self.assertEqual(self.cursor.fetchone(), self.data_by_key[3]) |
| 164 | + self.assertEqual(self.cursor.fetchone(), self.data_by_key[4]) |
| 165 | + self.assertEqual(self.cursor.fetchone(), None) |
| 166 | + |
| 167 | + def test_4810_bind_timestamp_with_zero_fseconds(self): |
| 168 | + "4810 - test binding a timestamp with zero fractional seconds" |
| 169 | + self.cursor.setinputsizes(value=oracledb.DB_TYPE_TIMESTAMP_LTZ) |
| 170 | + self.cursor.execute(""" |
| 171 | + select * |
| 172 | + from TestTimestampLTZs |
| 173 | + where trunc(TimestampLTZCol) = :value""", |
| 174 | + value=datetime.datetime(2022, 6, 12)) |
| 175 | + self.assertEqual(self.cursor.fetchall(), [self.data_by_key[10]]) |
| 176 | + |
| 177 | +if __name__ == "__main__": |
| 178 | + test_env.run_test_cases() |
0 commit comments