|
| 1 | +""" |
| 2 | +Tests for the conversion module in the SEA backend. |
| 3 | +
|
| 4 | +This module contains tests for the SqlType and SqlTypeConverter classes. |
| 5 | +""" |
| 6 | + |
| 7 | +import pytest |
| 8 | +import datetime |
| 9 | +import decimal |
| 10 | +from unittest.mock import Mock, patch |
| 11 | + |
| 12 | +from databricks.sql.backend.sea.conversion import SqlType, SqlTypeConverter |
| 13 | + |
| 14 | + |
| 15 | +class TestSqlType: |
| 16 | + """Test suite for the SqlType class.""" |
| 17 | + |
| 18 | + def test_is_numeric(self): |
| 19 | + """Test the is_numeric method.""" |
| 20 | + assert SqlType.is_numeric(SqlType.BYTE) |
| 21 | + assert SqlType.is_numeric(SqlType.SHORT) |
| 22 | + assert SqlType.is_numeric(SqlType.INT) |
| 23 | + assert SqlType.is_numeric(SqlType.LONG) |
| 24 | + assert SqlType.is_numeric(SqlType.FLOAT) |
| 25 | + assert SqlType.is_numeric(SqlType.DOUBLE) |
| 26 | + assert SqlType.is_numeric(SqlType.DECIMAL) |
| 27 | + |
| 28 | + # Test with uppercase |
| 29 | + assert SqlType.is_numeric("INT") |
| 30 | + assert SqlType.is_numeric("DECIMAL") |
| 31 | + |
| 32 | + # Test non-numeric types |
| 33 | + assert not SqlType.is_numeric(SqlType.STRING) |
| 34 | + assert not SqlType.is_numeric(SqlType.BOOLEAN) |
| 35 | + assert not SqlType.is_numeric(SqlType.DATE) |
| 36 | + |
| 37 | + def test_is_boolean(self): |
| 38 | + """Test the is_boolean method.""" |
| 39 | + assert SqlType.is_boolean(SqlType.BOOLEAN) |
| 40 | + assert SqlType.is_boolean("BOOLEAN") |
| 41 | + |
| 42 | + # Test non-boolean types |
| 43 | + assert not SqlType.is_boolean(SqlType.STRING) |
| 44 | + assert not SqlType.is_boolean(SqlType.INT) |
| 45 | + |
| 46 | + def test_is_datetime(self): |
| 47 | + """Test the is_datetime method.""" |
| 48 | + assert SqlType.is_datetime(SqlType.DATE) |
| 49 | + assert SqlType.is_datetime(SqlType.TIMESTAMP) |
| 50 | + assert SqlType.is_datetime(SqlType.INTERVAL) |
| 51 | + assert SqlType.is_datetime("DATE") |
| 52 | + assert SqlType.is_datetime("TIMESTAMP") |
| 53 | + |
| 54 | + # Test non-datetime types |
| 55 | + assert not SqlType.is_datetime(SqlType.STRING) |
| 56 | + assert not SqlType.is_datetime(SqlType.INT) |
| 57 | + |
| 58 | + def test_is_string(self): |
| 59 | + """Test the is_string method.""" |
| 60 | + assert SqlType.is_string(SqlType.STRING) |
| 61 | + assert SqlType.is_string(SqlType.CHAR) |
| 62 | + assert SqlType.is_string("STRING") |
| 63 | + assert SqlType.is_string("CHAR") |
| 64 | + |
| 65 | + # Test non-string types |
| 66 | + assert not SqlType.is_string(SqlType.INT) |
| 67 | + assert not SqlType.is_string(SqlType.BOOLEAN) |
| 68 | + |
| 69 | + def test_is_binary(self): |
| 70 | + """Test the is_binary method.""" |
| 71 | + assert SqlType.is_binary(SqlType.BINARY) |
| 72 | + assert SqlType.is_binary("BINARY") |
| 73 | + |
| 74 | + # Test non-binary types |
| 75 | + assert not SqlType.is_binary(SqlType.STRING) |
| 76 | + assert not SqlType.is_binary(SqlType.INT) |
| 77 | + |
| 78 | + def test_is_complex(self): |
| 79 | + """Test the is_complex method.""" |
| 80 | + assert SqlType.is_complex(SqlType.ARRAY) |
| 81 | + assert SqlType.is_complex(SqlType.MAP) |
| 82 | + assert SqlType.is_complex(SqlType.STRUCT) |
| 83 | + assert SqlType.is_complex(SqlType.USER_DEFINED_TYPE) |
| 84 | + assert SqlType.is_complex("ARRAY<int>") |
| 85 | + assert SqlType.is_complex("MAP<string,int>") |
| 86 | + assert SqlType.is_complex("STRUCT<name:string,age:int>") |
| 87 | + |
| 88 | + # Test non-complex types |
| 89 | + assert not SqlType.is_complex(SqlType.STRING) |
| 90 | + assert not SqlType.is_complex(SqlType.INT) |
| 91 | + |
| 92 | + |
| 93 | +class TestSqlTypeConverter: |
| 94 | + """Test suite for the SqlTypeConverter class.""" |
| 95 | + |
| 96 | + def test_convert_value_null(self): |
| 97 | + """Test converting null values.""" |
| 98 | + assert SqlTypeConverter.convert_value(None, SqlType.INT) is None |
| 99 | + assert SqlTypeConverter.convert_value(None, SqlType.STRING) is None |
| 100 | + assert SqlTypeConverter.convert_value(None, SqlType.BOOLEAN) is None |
| 101 | + |
| 102 | + def test_convert_numeric_types(self): |
| 103 | + """Test converting numeric types.""" |
| 104 | + # Test integer types |
| 105 | + assert SqlTypeConverter.convert_value("123", SqlType.BYTE) == 123 |
| 106 | + assert SqlTypeConverter.convert_value("456", SqlType.SHORT) == 456 |
| 107 | + assert SqlTypeConverter.convert_value("789", SqlType.INT) == 789 |
| 108 | + assert SqlTypeConverter.convert_value("1234567890", SqlType.LONG) == 1234567890 |
| 109 | + |
| 110 | + # Test floating point types |
| 111 | + assert SqlTypeConverter.convert_value("123.45", SqlType.FLOAT) == 123.45 |
| 112 | + assert SqlTypeConverter.convert_value("678.90", SqlType.DOUBLE) == 678.90 |
| 113 | + |
| 114 | + # Test decimal type |
| 115 | + decimal_value = SqlTypeConverter.convert_value("123.45", SqlType.DECIMAL) |
| 116 | + assert isinstance(decimal_value, decimal.Decimal) |
| 117 | + assert decimal_value == decimal.Decimal("123.45") |
| 118 | + |
| 119 | + # Test decimal with precision and scale |
| 120 | + decimal_value = SqlTypeConverter.convert_value( |
| 121 | + "123.45", SqlType.DECIMAL, precision=5, scale=2 |
| 122 | + ) |
| 123 | + assert isinstance(decimal_value, decimal.Decimal) |
| 124 | + assert decimal_value == decimal.Decimal("123.45") |
| 125 | + |
| 126 | + # Test invalid numeric input |
| 127 | + result = SqlTypeConverter.convert_value("not_a_number", SqlType.INT) |
| 128 | + assert result == "not_a_number" # Returns original value on error |
| 129 | + |
| 130 | + def test_convert_boolean_type(self): |
| 131 | + """Test converting boolean types.""" |
| 132 | + # True values |
| 133 | + assert SqlTypeConverter.convert_value("true", SqlType.BOOLEAN) is True |
| 134 | + assert SqlTypeConverter.convert_value("True", SqlType.BOOLEAN) is True |
| 135 | + assert SqlTypeConverter.convert_value("t", SqlType.BOOLEAN) is True |
| 136 | + assert SqlTypeConverter.convert_value("1", SqlType.BOOLEAN) is True |
| 137 | + assert SqlTypeConverter.convert_value("yes", SqlType.BOOLEAN) is True |
| 138 | + assert SqlTypeConverter.convert_value("y", SqlType.BOOLEAN) is True |
| 139 | + |
| 140 | + # False values |
| 141 | + assert SqlTypeConverter.convert_value("false", SqlType.BOOLEAN) is False |
| 142 | + assert SqlTypeConverter.convert_value("False", SqlType.BOOLEAN) is False |
| 143 | + assert SqlTypeConverter.convert_value("f", SqlType.BOOLEAN) is False |
| 144 | + assert SqlTypeConverter.convert_value("0", SqlType.BOOLEAN) is False |
| 145 | + assert SqlTypeConverter.convert_value("no", SqlType.BOOLEAN) is False |
| 146 | + assert SqlTypeConverter.convert_value("n", SqlType.BOOLEAN) is False |
| 147 | + |
| 148 | + def test_convert_datetime_types(self): |
| 149 | + """Test converting datetime types.""" |
| 150 | + # Test date type |
| 151 | + date_value = SqlTypeConverter.convert_value("2023-01-15", SqlType.DATE) |
| 152 | + assert isinstance(date_value, datetime.date) |
| 153 | + assert date_value == datetime.date(2023, 1, 15) |
| 154 | + |
| 155 | + # Test timestamp type |
| 156 | + timestamp_value = SqlTypeConverter.convert_value( |
| 157 | + "2023-01-15T12:30:45", SqlType.TIMESTAMP |
| 158 | + ) |
| 159 | + assert isinstance(timestamp_value, datetime.datetime) |
| 160 | + assert timestamp_value.year == 2023 |
| 161 | + assert timestamp_value.month == 1 |
| 162 | + assert timestamp_value.day == 15 |
| 163 | + assert timestamp_value.hour == 12 |
| 164 | + assert timestamp_value.minute == 30 |
| 165 | + assert timestamp_value.second == 45 |
| 166 | + |
| 167 | + # Test interval type (currently returns as string) |
| 168 | + interval_value = SqlTypeConverter.convert_value( |
| 169 | + "1 day 2 hours", SqlType.INTERVAL |
| 170 | + ) |
| 171 | + assert interval_value == "1 day 2 hours" |
| 172 | + |
| 173 | + # Test invalid date input |
| 174 | + result = SqlTypeConverter.convert_value("not_a_date", SqlType.DATE) |
| 175 | + assert result == "not_a_date" # Returns original value on error |
| 176 | + |
| 177 | + def test_convert_string_types(self): |
| 178 | + """Test converting string types.""" |
| 179 | + # String types don't need conversion, they should be returned as-is |
| 180 | + assert ( |
| 181 | + SqlTypeConverter.convert_value("test string", SqlType.STRING) |
| 182 | + == "test string" |
| 183 | + ) |
| 184 | + assert SqlTypeConverter.convert_value("test char", SqlType.CHAR) == "test char" |
| 185 | + |
| 186 | + def test_convert_binary_type(self): |
| 187 | + """Test converting binary type.""" |
| 188 | + # Test valid hex string |
| 189 | + binary_value = SqlTypeConverter.convert_value("48656C6C6F", SqlType.BINARY) |
| 190 | + assert isinstance(binary_value, bytes) |
| 191 | + assert binary_value == b"Hello" |
| 192 | + |
| 193 | + # Test invalid binary input |
| 194 | + result = SqlTypeConverter.convert_value("not_hex", SqlType.BINARY) |
| 195 | + assert result == "not_hex" # Returns original value on error |
| 196 | + |
| 197 | + def test_convert_unsupported_type(self): |
| 198 | + """Test converting an unsupported type.""" |
| 199 | + # Should return the original value |
| 200 | + assert SqlTypeConverter.convert_value("test", "unsupported_type") == "test" |
| 201 | + |
| 202 | + # Complex types should return as-is |
| 203 | + assert ( |
| 204 | + SqlTypeConverter.convert_value("complex_value", SqlType.ARRAY) |
| 205 | + == "complex_value" |
| 206 | + ) |
| 207 | + assert ( |
| 208 | + SqlTypeConverter.convert_value("complex_value", SqlType.MAP) |
| 209 | + == "complex_value" |
| 210 | + ) |
| 211 | + assert ( |
| 212 | + SqlTypeConverter.convert_value("complex_value", SqlType.STRUCT) |
| 213 | + == "complex_value" |
| 214 | + ) |
0 commit comments