|
| 1 | +import decimal |
| 2 | +import datetime |
| 3 | +from collections import namedtuple |
| 4 | + |
| 5 | +TypeFailure = namedtuple( |
| 6 | + "TypeFailure", "query,columnType,resultType,resultValue," |
| 7 | + "actualValue,actualType,description,conf") |
| 8 | +ResultFailure = namedtuple( |
| 9 | + "ResultFailure", "query,columnType,resultType,resultValue," |
| 10 | + "actualValue,actualType,description,conf") |
| 11 | +ExecFailure = namedtuple( |
| 12 | + "ExecFailure", "query,columnType,resultType,resultValue," |
| 13 | + "actualValue,actualType,description,conf,error") |
| 14 | + |
| 15 | + |
| 16 | +class SmokeTestMixin: |
| 17 | + def test_smoke_test(self): |
| 18 | + with self.cursor() as cursor: |
| 19 | + cursor.execute("select 0") |
| 20 | + rows = cursor.fetchall() |
| 21 | + self.assertEqual(len(rows), 1) |
| 22 | + self.assertEqual(rows[0][0], 0) |
| 23 | + |
| 24 | + |
| 25 | +class CoreTestMixin: |
| 26 | + """ |
| 27 | + This mixin expects to be mixed with a CursorTest-like class with the following extra attributes: |
| 28 | + validate_row_value_type: bool |
| 29 | + validate_result: bool |
| 30 | + """ |
| 31 | + |
| 32 | + # A list of (subquery, column_type, python_type, expected_result) |
| 33 | + # To be executed as "SELECT {} FROM RANGE(...)" and "SELECT {}" |
| 34 | + range_queries = [ |
| 35 | + ("TRUE", 'boolean', bool, True), |
| 36 | + ("cast(1 AS TINYINT)", 'byte', int, 1), |
| 37 | + ("cast(1000 AS SMALLINT)", 'short', int, 1000), |
| 38 | + ("cast(100000 AS INTEGER)", 'integer', int, 100000), |
| 39 | + ("cast(10000000000000 AS BIGINT)", 'long', int, 10000000000000), |
| 40 | + ("cast(100.001 AS DECIMAL(6, 3))", 'decimal', decimal.Decimal, 100.001), |
| 41 | + ("date '2020-02-20'", 'date', datetime.date, datetime.date(2020, 2, 20)), |
| 42 | + ("unhex('f000')", 'binary', bytes, b'\xf0\x00'), # pyodbc internal mismatch |
| 43 | + ("'foo'", 'string', str, 'foo'), |
| 44 | + # SPARK-32130: 6.x: "4 weeks 2 days" vs 7.x: "30 days" |
| 45 | + # ("interval 30 days", str, str, "interval 4 weeks 2 days"), |
| 46 | + # ("interval 3 days", str, str, "interval 3 days"), |
| 47 | + ("CAST(NULL AS DOUBLE)", 'double', type(None), None), |
| 48 | + ] |
| 49 | + |
| 50 | + # Full queries, only the first column of the first row is checked |
| 51 | + queries = [("NULL UNION (SELECT 1) order by 1", 'integer', type(None), None)] |
| 52 | + |
| 53 | + def run_tests_on_queries(self, default_conf): |
| 54 | + failures = [] |
| 55 | + for (query, columnType, rowValueType, answer) in self.range_queries: |
| 56 | + with self.cursor(default_conf) as cursor: |
| 57 | + failures.extend( |
| 58 | + self.run_query(cursor, query, columnType, rowValueType, answer, default_conf)) |
| 59 | + failures.extend( |
| 60 | + self.run_range_query(cursor, query, columnType, rowValueType, answer, |
| 61 | + default_conf)) |
| 62 | + |
| 63 | + for (query, columnType, rowValueType, answer) in self.queries: |
| 64 | + with self.cursor(default_conf) as cursor: |
| 65 | + failures.extend( |
| 66 | + self.run_query(cursor, query, columnType, rowValueType, answer, default_conf)) |
| 67 | + |
| 68 | + if failures: |
| 69 | + self.fail("Failed testing result set with Arrow. " |
| 70 | + "Failed queries: {}".format("\n\n".join([str(f) for f in failures]))) |
| 71 | + |
| 72 | + def run_query(self, cursor, query, columnType, rowValueType, answer, conf): |
| 73 | + full_query = "SELECT {}".format(query) |
| 74 | + expected_column_types = self.expected_column_types(columnType) |
| 75 | + try: |
| 76 | + cursor.execute(full_query) |
| 77 | + (result, ) = cursor.fetchone() |
| 78 | + if not all(cursor.description[0][1] == type for type in expected_column_types): |
| 79 | + return [ |
| 80 | + TypeFailure(full_query, expected_column_types, rowValueType, answer, result, |
| 81 | + type(result), cursor.description, conf) |
| 82 | + ] |
| 83 | + if self.validate_row_value_type and type(result) is not rowValueType: |
| 84 | + return [ |
| 85 | + TypeFailure(full_query, expected_column_types, rowValueType, answer, result, |
| 86 | + type(result), cursor.description, conf) |
| 87 | + ] |
| 88 | + if self.validate_result and str(answer) != str(result): |
| 89 | + return [ |
| 90 | + ResultFailure(full_query, query, expected_column_types, rowValueType, answer, |
| 91 | + result, type(result), cursor.description, conf) |
| 92 | + ] |
| 93 | + return [] |
| 94 | + except Exception as e: |
| 95 | + return [ |
| 96 | + ExecFailure(full_query, columnType, rowValueType, None, None, None, |
| 97 | + cursor.description, conf, e) |
| 98 | + ] |
| 99 | + |
| 100 | + def run_range_query(self, cursor, query, columnType, rowValueType, expected, conf): |
| 101 | + full_query = "SELECT {}, id FROM RANGE({})".format(query, 5000) |
| 102 | + expected_column_types = self.expected_column_types(columnType) |
| 103 | + try: |
| 104 | + cursor.execute(full_query) |
| 105 | + while True: |
| 106 | + rows = cursor.fetchmany(1000) |
| 107 | + if len(rows) <= 0: |
| 108 | + break |
| 109 | + for index, (result, id) in enumerate(rows): |
| 110 | + if not all(cursor.description[0][1] == type for type in expected_column_types): |
| 111 | + return [ |
| 112 | + TypeFailure(full_query, expected_column_types, rowValueType, expected, |
| 113 | + result, type(result), cursor.description, conf) |
| 114 | + ] |
| 115 | + if self.validate_row_value_type and type(result) \ |
| 116 | + is not rowValueType: |
| 117 | + return [ |
| 118 | + TypeFailure(full_query, expected_column_types, rowValueType, expected, |
| 119 | + result, type(result), cursor.description, conf) |
| 120 | + ] |
| 121 | + if self.validate_result and str(expected) != str(result): |
| 122 | + return [ |
| 123 | + ResultFailure(full_query, expected_column_types, rowValueType, expected, |
| 124 | + result, type(result), cursor.description, conf) |
| 125 | + ] |
| 126 | + return [] |
| 127 | + except Exception as e: |
| 128 | + return [ |
| 129 | + ExecFailure(full_query, columnType, rowValueType, None, None, None, |
| 130 | + cursor.description, conf, e) |
| 131 | + ] |
0 commit comments