|
| 1 | +# ----------------------------------------------------------------------------- |
| 2 | +# Copyright (c) 2024, 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 | +7000 - Module for testing async connections shortcut methods |
| 27 | +""" |
| 28 | + |
| 29 | +import unittest |
| 30 | + |
| 31 | +import test_env |
| 32 | + |
| 33 | + |
| 34 | +@unittest.skipUnless( |
| 35 | + test_env.get_is_thin(), "asyncio not supported in thick mode" |
| 36 | +) |
| 37 | +class TestCase(test_env.BaseAsyncTestCase): |
| 38 | + |
| 39 | + async def test_7700(self): |
| 40 | + "7700 - test execute()" |
| 41 | + await self.conn.execute("truncate table TestTempTable") |
| 42 | + await self.conn.execute( |
| 43 | + "insert into TestTempTable (IntCol) values (:1)", [77] |
| 44 | + ) |
| 45 | + await self.conn.execute( |
| 46 | + "insert into TestTempTable (IntCol) values (:val)", dict(val=15) |
| 47 | + ) |
| 48 | + await self.conn.commit() |
| 49 | + |
| 50 | + res = await self.conn.fetchall( |
| 51 | + "select IntCol from TestTempTable order by IntCol" |
| 52 | + ) |
| 53 | + self.assertEqual(res, [(15,), (77,)]) |
| 54 | + |
| 55 | + async def test_7701(self): |
| 56 | + "7701 - test fetchall()" |
| 57 | + await self.conn.execute("truncate table TestTempTable") |
| 58 | + data = [(2,), (3,)] |
| 59 | + await self.conn.executemany( |
| 60 | + "insert into TestTempTable (IntCol) values (:1)", data |
| 61 | + ) |
| 62 | + res = await self.conn.fetchall( |
| 63 | + "select IntCol from TestTempTable order by IntCol" |
| 64 | + ) |
| 65 | + self.assertEqual(res, data) |
| 66 | + |
| 67 | + async def test_7702(self): |
| 68 | + "7702 - test fetchall() with arraysize" |
| 69 | + await self.conn.execute("truncate table TestTempTable") |
| 70 | + await self.conn.executemany( |
| 71 | + "insert into TestTempTable (IntCol) values (:value)", |
| 72 | + [{"value": i} for i in range(3)], |
| 73 | + ) |
| 74 | + await self.conn.commit() |
| 75 | + res = await self.conn.fetchall( |
| 76 | + "select IntCol from TestTempTable order by IntCol", arraysize=1 |
| 77 | + ) |
| 78 | + self.assertEqual(res, [(0,), (1,), (2,)]) |
| 79 | + |
| 80 | + async def test_7703(self): |
| 81 | + "7703 - test fetchall() with rowfactory" |
| 82 | + await self.conn.execute("truncate table TestTempTable") |
| 83 | + await self.conn.execute( |
| 84 | + """ |
| 85 | + insert into TestTempTable (IntCol, StringCol1) |
| 86 | + values (1, 'test_7703') |
| 87 | + """ |
| 88 | + ) |
| 89 | + await self.conn.commit() |
| 90 | + |
| 91 | + column_names = ["INTCOL", "STRINGCOL1"] |
| 92 | + |
| 93 | + def rowfactory(*row): |
| 94 | + return dict(zip(column_names, row)) |
| 95 | + |
| 96 | + res = await self.conn.fetchall( |
| 97 | + "select IntCol, StringCol1 from TestTempTable", |
| 98 | + rowfactory=rowfactory, |
| 99 | + ) |
| 100 | + expected_value = [{"INTCOL": 1, "STRINGCOL1": "test_7703"}] |
| 101 | + self.assertEqual(res, expected_value) |
| 102 | + |
| 103 | + async def test_7704(self): |
| 104 | + "7704 - test executemany()" |
| 105 | + await self.conn.execute("truncate table TestTempTable") |
| 106 | + await self.conn.executemany( |
| 107 | + "insert into TestTempTable (IntCol) values (:1)", [(1,), (2,)] |
| 108 | + ) |
| 109 | + await self.conn.executemany( |
| 110 | + "insert into TestTempTable (IntCol) values (:value)", |
| 111 | + [{"value": 3}, {"value": 4}], |
| 112 | + ) |
| 113 | + await self.conn.commit() |
| 114 | + res = await self.conn.fetchall( |
| 115 | + "select IntCol from TestTempTable order by IntCol" |
| 116 | + ) |
| 117 | + self.assertEqual(res, [(1,), (2,), (3,), (4,)]) |
| 118 | + |
| 119 | + async def test_7705(self): |
| 120 | + "7705 - test fetchone()" |
| 121 | + await self.conn.execute("truncate table TestTempTable") |
| 122 | + await self.conn.executemany( |
| 123 | + "insert into TestTempTable (IntCol) values (:1)", [(9,), (10,)] |
| 124 | + ) |
| 125 | + await self.conn.commit() |
| 126 | + |
| 127 | + res = await self.conn.fetchone( |
| 128 | + "select IntCol from TestTempTable order by IntCol" |
| 129 | + ) |
| 130 | + self.assertEqual(res, (9,)) |
| 131 | + |
| 132 | + res = await self.conn.fetchone("select :1 from dual", [23]) |
| 133 | + self.assertEqual(res, (23,)) |
| 134 | + |
| 135 | + res = await self.conn.fetchone("select :val from dual", {"val": 5}) |
| 136 | + self.assertEqual(res, (5,)) |
| 137 | + |
| 138 | + async def test_7706(self): |
| 139 | + "7706 - test fetchmany()" |
| 140 | + data = [(i,) for i in range(10)] |
| 141 | + await self.conn.execute("truncate table TestTempTable") |
| 142 | + await self.conn.executemany( |
| 143 | + "insert into TestTempTable (IntCol) values (:1)", data |
| 144 | + ) |
| 145 | + await self.conn.commit() |
| 146 | + res = await self.conn.fetchmany( |
| 147 | + "select IntCol from TestTempTable order by IntCol", |
| 148 | + num_rows=3, |
| 149 | + ) |
| 150 | + self.assertEqual(res, data[:3]) |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + test_env.run_test_cases() |
0 commit comments