|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +#Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved. |
| 3 | +#------------------------------------------------------------------------------ |
| 4 | + |
| 5 | +""" |
| 6 | +3800 - Module for testing the input and output type handlers. |
| 7 | +""" |
| 8 | + |
| 9 | +import test_env |
| 10 | + |
| 11 | +import cx_Oracle as oracledb |
| 12 | +import json |
| 13 | + |
| 14 | +class Building(object): |
| 15 | + |
| 16 | + def __init__(self, building_id, description, num_floors): |
| 17 | + self.building_id = building_id |
| 18 | + self.description = description |
| 19 | + self.num_floors = num_floors |
| 20 | + |
| 21 | + def __repr__(self): |
| 22 | + return "<Building %s: %s>" % (self.building_id, self.description) |
| 23 | + |
| 24 | + def __eq__(self, other): |
| 25 | + if isinstance(other, Building): |
| 26 | + return other.building_id == self.building_id \ |
| 27 | + and other.description == self.description \ |
| 28 | + and other.num_floors == self.num_floors |
| 29 | + return NotImplemented |
| 30 | + |
| 31 | + def to_json(self): |
| 32 | + return json.dumps(self.__dict__) |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def from_json(cls, value): |
| 36 | + result = json.loads(value) |
| 37 | + return cls(**result) |
| 38 | + |
| 39 | + |
| 40 | +class TestCase(test_env.BaseTestCase): |
| 41 | + |
| 42 | + def building_in_converter(self, value): |
| 43 | + return value.to_json() |
| 44 | + |
| 45 | + def input_type_handler(self, cursor, value, num_elements): |
| 46 | + if isinstance(value, Building): |
| 47 | + return cursor.var(oracledb.STRING, arraysize=num_elements, |
| 48 | + inconverter=self.building_in_converter) |
| 49 | + |
| 50 | + def output_type_handler(self, cursor, name, default_type, size, precision, |
| 51 | + scale): |
| 52 | + if default_type == oracledb.STRING: |
| 53 | + return cursor.var(default_type, arraysize=cursor.arraysize, |
| 54 | + outconverter=Building.from_json) |
| 55 | + |
| 56 | + def test_3800(self): |
| 57 | + "3800 - binding unsupported python object without input type handler" |
| 58 | + self.cursor.execute("truncate table TestTempTable") |
| 59 | + sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)" |
| 60 | + building = Building(1, "The First Building", 5) |
| 61 | + self.assertRaises(oracledb.NotSupportedError, self.cursor.execute, sql, |
| 62 | + (building.building_id, building)) |
| 63 | + |
| 64 | + def test_3801(self): |
| 65 | + "3801 - not callable input type handler" |
| 66 | + self.cursor.execute("truncate table TestTempTable") |
| 67 | + building = Building(1, "The First Building", 5) |
| 68 | + sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)" |
| 69 | + self.cursor.inputtypehandler = 5 |
| 70 | + self.assertRaises(TypeError, self.cursor.execute, sql, |
| 71 | + (building.building_id, building)) |
| 72 | + |
| 73 | + def test_3802(self): |
| 74 | + "3802 - binding unsupported python object with input type handler" |
| 75 | + self.cursor.execute("truncate table TestTempTable") |
| 76 | + building = Building(1, "The First Building", 5) |
| 77 | + sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)" |
| 78 | + self.cursor.inputtypehandler = self.input_type_handler |
| 79 | + self.cursor.execute(sql, (building.building_id, building)) |
| 80 | + self.connection.commit() |
| 81 | + self.cursor.execute("select IntCol, StringCol from TestTempTable") |
| 82 | + self.assertEqual(self.cursor.fetchall(), |
| 83 | + [(building.building_id, building.to_json())]) |
| 84 | + |
| 85 | + def test_3803(self): |
| 86 | + "3803 - input type handler and output type handler on cursor level" |
| 87 | + self.cursor.execute("truncate table TestTempTable") |
| 88 | + building_one = Building(1, "The First Building", 5) |
| 89 | + building_two = Building(2, "The Second Building", 87) |
| 90 | + sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)" |
| 91 | + cursor_one = self.connection.cursor() |
| 92 | + cursor_two = self.connection.cursor() |
| 93 | + cursor_one.inputtypehandler = self.input_type_handler |
| 94 | + cursor_one.execute(sql, (building_one.building_id, building_one)) |
| 95 | + self.connection.commit() |
| 96 | + cursor_one.execute("select IntCol, StringCol from TestTempTable") |
| 97 | + self.assertEqual(cursor_one.fetchall(), |
| 98 | + [(building_one.building_id, building_one.to_json())]) |
| 99 | + self.assertRaises(oracledb.NotSupportedError, cursor_two.execute, |
| 100 | + sql, (building_two.building_id, building_two)) |
| 101 | + cursor_two.outputtypehandler = self.output_type_handler |
| 102 | + cursor_two.execute("select IntCol, StringCol from TestTempTable") |
| 103 | + self.assertEqual(cursor_two.fetchall(), |
| 104 | + [(building_one.building_id, building_one)]) |
| 105 | + |
| 106 | + def test_3804(self): |
| 107 | + "3804 - input type handler and output type handler on connection level" |
| 108 | + self.cursor.execute("truncate table TestTempTable") |
| 109 | + building_one = Building(1, "The First Building", 5) |
| 110 | + building_two = Building(2, "The Second Building", 87) |
| 111 | + sql = "insert into TestTempTable (IntCol, StringCol) values (:1, :2)" |
| 112 | + connection = test_env.get_connection() |
| 113 | + connection.inputtypehandler = self.input_type_handler |
| 114 | + cursor_one = connection.cursor() |
| 115 | + cursor_two = connection.cursor() |
| 116 | + cursor_one.execute(sql, (building_one.building_id, building_one)) |
| 117 | + cursor_two.execute(sql, (building_two.building_id, building_two)) |
| 118 | + connection.commit() |
| 119 | + expected_data = [ |
| 120 | + (building_one.building_id, building_one), |
| 121 | + (building_two.building_id, building_two) |
| 122 | + ] |
| 123 | + connection.outputtypehandler = self.output_type_handler |
| 124 | + cursor_one.execute("select IntCol, StringCol from TestTempTable") |
| 125 | + self.assertEqual(cursor_one.fetchall(), expected_data) |
| 126 | + cursor_two.execute("select IntCol, StringCol from TestTempTable") |
| 127 | + self.assertEqual(cursor_two.fetchall(), expected_data) |
| 128 | + other_cursor = self.connection.cursor() |
| 129 | + self.assertRaises(oracledb.NotSupportedError, other_cursor.execute, |
| 130 | + sql, (building_one.building_id, building_one)) |
| 131 | + |
| 132 | +if __name__ == "__main__": |
| 133 | + test_env.run_test_cases() |
0 commit comments