-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathtest_type_aliases.py
More file actions
184 lines (153 loc) · 6.65 KB
/
test_type_aliases.py
File metadata and controls
184 lines (153 loc) · 6.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
Tests for numeric type aliases (float32, float64, int8, int16, int32, int64, etc.)
"""
import pytest
from datajoint.declare import SQL_TYPE_ALIASES, SPECIAL_TYPES, match_type
from .schema_type_aliases import TypeAliasTable, TypeAliasPrimaryKey, TypeAliasNullable
class TestTypeAliasPatterns:
"""Test that type alias patterns are correctly defined and matched."""
@pytest.mark.parametrize(
"alias,expected_category",
[
("float32", "FLOAT32"),
("float64", "FLOAT64"),
("int64", "INT64"),
("uint64", "UINT64"),
("int32", "INT32"),
("uint32", "UINT32"),
("int16", "INT16"),
("uint16", "UINT16"),
("int8", "INT8"),
("uint8", "UINT8"),
("bool", "BOOL"),
],
)
def test_type_alias_pattern_matching(self, alias, expected_category):
"""Test that type aliases are matched to correct categories."""
category = match_type(alias)
assert category == expected_category
assert category in SPECIAL_TYPES
assert category in SQL_TYPE_ALIASES
@pytest.mark.parametrize(
"alias,expected_mysql_type",
[
("float32", "float"),
("float64", "double"),
("int64", "bigint"),
("uint64", "bigint unsigned"),
("int32", "int"),
("uint32", "int unsigned"),
("int16", "smallint"),
("uint16", "smallint unsigned"),
("int8", "tinyint"),
("uint8", "tinyint unsigned"),
("bool", "tinyint"),
],
)
def test_type_alias_mysql_mapping(self, alias, expected_mysql_type):
"""Test that type aliases map to correct MySQL types."""
category = match_type(alias)
mysql_type = SQL_TYPE_ALIASES[category]
assert mysql_type == expected_mysql_type
@pytest.mark.parametrize(
"native_type,expected_category",
[
("int", "INTEGER"),
("bigint", "INTEGER"),
("smallint", "INTEGER"),
("tinyint", "INTEGER"),
("float", "FLOAT"),
("double", "FLOAT"),
],
)
def test_native_types_still_work(self, native_type, expected_category):
"""Test that native MySQL types still match correctly."""
category = match_type(native_type)
assert category == expected_category
class TestTypeAliasTableCreation:
"""Test table creation with type aliases."""
def test_create_table_with_all_aliases(self, schema_type_aliases):
"""Test that tables with all type aliases can be created."""
assert TypeAliasTable().full_table_name is not None
def test_create_table_with_alias_primary_key(self, schema_type_aliases):
"""Test that tables with type aliases in primary key can be created."""
assert TypeAliasPrimaryKey().full_table_name is not None
def test_create_table_with_nullable_aliases(self, schema_type_aliases):
"""Test that tables with nullable type alias columns can be created."""
assert TypeAliasNullable().full_table_name is not None
class TestTypeAliasHeading:
"""Test that headings correctly preserve type alias information."""
def test_heading_preserves_type_aliases(self, schema_type_aliases):
"""Test that heading shows original type aliases."""
heading = TypeAliasTable().heading
heading_str = repr(heading)
# Check that type aliases appear in the heading representation
assert "float32" in heading_str
assert "float64" in heading_str
assert "int64" in heading_str
assert "uint64" in heading_str
assert "int32" in heading_str
assert "uint32" in heading_str
assert "int16" in heading_str
assert "uint16" in heading_str
assert "int8" in heading_str
assert "uint8" in heading_str
assert "bool" in heading_str
class TestTypeAliasInsertFetch:
"""Test inserting and fetching data with type aliases."""
def test_insert_and_fetch(self, schema_type_aliases):
"""Test inserting and fetching values with type aliases."""
table = TypeAliasTable()
table.delete()
test_data = dict(
id=1,
val_float32=3.14,
val_float64=2.718281828,
val_int64=9223372036854775807, # max int64
val_uint64=18446744073709551615, # max uint64
val_int32=2147483647, # max int32
val_uint32=4294967295, # max uint32
val_int16=32767, # max int16
val_uint16=65535, # max uint16
val_int8=127, # max int8
val_uint8=255, # max uint8
val_bool=1, # boolean true
)
table.insert1(test_data)
fetched = table.fetch1()
assert fetched["id"] == test_data["id"]
assert abs(fetched["val_float32"] - test_data["val_float32"]) < 0.001
assert abs(fetched["val_float64"] - test_data["val_float64"]) < 1e-9
assert fetched["val_int64"] == test_data["val_int64"]
assert fetched["val_uint64"] == test_data["val_uint64"]
assert fetched["val_int32"] == test_data["val_int32"]
assert fetched["val_uint32"] == test_data["val_uint32"]
assert fetched["val_int16"] == test_data["val_int16"]
assert fetched["val_uint16"] == test_data["val_uint16"]
assert fetched["val_int8"] == test_data["val_int8"]
assert fetched["val_uint8"] == test_data["val_uint8"]
assert fetched["val_bool"] == test_data["val_bool"]
def test_insert_primary_key_with_aliases(self, schema_type_aliases):
"""Test using type aliases in primary key."""
table = TypeAliasPrimaryKey()
table.delete()
table.insert1(dict(pk_int32=100, pk_uint16=200, value="test"))
fetched = (table & dict(pk_int32=100, pk_uint16=200)).fetch1()
assert fetched["pk_int32"] == 100
assert fetched["pk_uint16"] == 200
assert fetched["value"] == "test"
def test_nullable_type_aliases(self, schema_type_aliases):
"""Test nullable columns with type aliases."""
table = TypeAliasNullable()
table.delete()
# Insert with NULL values
table.insert1(dict(id=1, nullable_float32=None, nullable_int64=None))
fetched = table.fetch1()
assert fetched["id"] == 1
assert fetched["nullable_float32"] is None
assert fetched["nullable_int64"] is None
# Insert with actual values
table.insert1(dict(id=2, nullable_float32=1.5, nullable_int64=999))
fetched = (table & dict(id=2)).fetch1()
assert fetched["nullable_float32"] == 1.5
assert fetched["nullable_int64"] == 999