-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_table_format.py
More file actions
323 lines (274 loc) · 12 KB
/
test_table_format.py
File metadata and controls
323 lines (274 loc) · 12 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import json
import pytest
from feast.table_format import (
DeltaFormat,
HudiFormat,
IcebergFormat,
TableFormatType,
create_table_format,
table_format_from_dict,
table_format_from_json,
)
class TestTableFormat:
"""Test core TableFormat classes and functionality."""
def test_iceberg_table_format_creation(self):
"""Test IcebergFormat creation and properties."""
iceberg_format = IcebergFormat(
catalog="my_catalog",
namespace="my_namespace",
properties={"catalog.uri": "s3://bucket/warehouse", "format-version": "2"},
)
assert iceberg_format.format_type == TableFormatType.ICEBERG
assert iceberg_format.catalog == "my_catalog"
assert iceberg_format.namespace == "my_namespace"
assert iceberg_format.get_property("iceberg.catalog") == "my_catalog"
assert iceberg_format.get_property("iceberg.namespace") == "my_namespace"
assert iceberg_format.get_property("catalog.uri") == "s3://bucket/warehouse"
assert iceberg_format.get_property("format-version") == "2"
def test_iceberg_table_format_minimal(self):
"""Test IcebergFormat with minimal config."""
iceberg_format = IcebergFormat()
assert iceberg_format.format_type == TableFormatType.ICEBERG
assert iceberg_format.catalog is None
assert iceberg_format.namespace is None
assert len(iceberg_format.properties) == 0
def test_delta_table_format_creation(self):
"""Test DeltaFormat creation and properties."""
delta_format = DeltaFormat(
checkpoint_location="s3://bucket/checkpoints",
properties={"delta.autoOptimize.optimizeWrite": "true"},
)
assert delta_format.format_type == TableFormatType.DELTA
assert delta_format.checkpoint_location == "s3://bucket/checkpoints"
assert (
delta_format.get_property("delta.checkpointLocation")
== "s3://bucket/checkpoints"
)
assert delta_format.get_property("delta.autoOptimize.optimizeWrite") == "true"
def test_hudi_table_format_creation(self):
"""Test HudiFormat creation and properties."""
hudi_format = HudiFormat(
table_type="COPY_ON_WRITE",
record_key="id",
precombine_field="timestamp",
properties={
"hoodie.compaction.strategy": "org.apache.hudi.table.action.compact.strategy.LogFileSizeBasedCompactionStrategy"
},
)
assert hudi_format.format_type == TableFormatType.HUDI
assert hudi_format.table_type == "COPY_ON_WRITE"
assert hudi_format.record_key == "id"
assert hudi_format.precombine_field == "timestamp"
assert (
hudi_format.get_property("hoodie.datasource.write.table.type")
== "COPY_ON_WRITE"
)
assert (
hudi_format.get_property("hoodie.datasource.write.recordkey.field") == "id"
)
assert (
hudi_format.get_property("hoodie.datasource.write.precombine.field")
== "timestamp"
)
def test_table_format_property_methods(self):
"""Test property getter/setter methods."""
iceberg_format = IcebergFormat()
# Test setting and getting properties
iceberg_format.set_property("snapshot-id", "123456789")
assert iceberg_format.get_property("snapshot-id") == "123456789"
# Test default value
assert iceberg_format.get_property("non-existent-key", "default") == "default"
assert iceberg_format.get_property("non-existent-key") is None
def test_table_format_serialization(self):
"""Test table format serialization to/from dict."""
# Test Iceberg
iceberg_format = IcebergFormat(
catalog="test_catalog",
namespace="test_namespace",
properties={"key1": "value1", "key2": "value2"},
)
iceberg_dict = iceberg_format.to_dict()
iceberg_restored = IcebergFormat.from_dict(iceberg_dict)
assert iceberg_restored.format_type == iceberg_format.format_type
assert iceberg_restored.catalog == iceberg_format.catalog
assert iceberg_restored.namespace == iceberg_format.namespace
assert iceberg_restored.properties == iceberg_format.properties
# Test Delta
delta_format = DeltaFormat(
checkpoint_location="s3://bucket/checkpoints",
properties={"key": "value"},
)
delta_dict = delta_format.to_dict()
delta_restored = DeltaFormat.from_dict(delta_dict)
assert delta_restored.format_type == delta_format.format_type
assert delta_restored.properties == delta_format.properties
assert delta_restored.checkpoint_location == delta_format.checkpoint_location
# Test Hudi
hudi_format = HudiFormat(
table_type="MERGE_ON_READ",
record_key="uuid",
precombine_field="ts",
)
hudi_dict = hudi_format.to_dict()
hudi_restored = HudiFormat.from_dict(hudi_dict)
assert hudi_restored.format_type == hudi_format.format_type
assert hudi_restored.table_type == hudi_format.table_type
assert hudi_restored.record_key == hudi_format.record_key
assert hudi_restored.precombine_field == hudi_format.precombine_field
def test_factory_function(self):
"""Test create_table_format factory function."""
# Test Iceberg
iceberg_format = create_table_format(
TableFormatType.ICEBERG,
catalog="test_catalog",
namespace="test_ns",
)
assert isinstance(iceberg_format, IcebergFormat)
assert iceberg_format.catalog == "test_catalog"
assert iceberg_format.namespace == "test_ns"
# Test Delta
delta_format = create_table_format(
TableFormatType.DELTA,
checkpoint_location="s3://test",
)
assert isinstance(delta_format, DeltaFormat)
assert delta_format.checkpoint_location == "s3://test"
# Test Hudi
hudi_format = create_table_format(
TableFormatType.HUDI,
table_type="COPY_ON_WRITE",
)
assert isinstance(hudi_format, HudiFormat)
assert hudi_format.table_type == "COPY_ON_WRITE"
# Test invalid format type
with pytest.raises(ValueError, match="Unknown table format type"):
create_table_format("invalid_format")
def test_table_format_from_dict(self):
"""Test table_format_from_dict function."""
# Test Iceberg
iceberg_dict = {
"format_type": "iceberg",
"catalog": "test_catalog",
"namespace": "test_namespace",
"properties": {"key1": "value1", "key2": "value2"},
}
iceberg_format = table_format_from_dict(iceberg_dict)
assert isinstance(iceberg_format, IcebergFormat)
assert iceberg_format.catalog == "test_catalog"
# Test Delta
delta_dict = {
"format_type": "delta",
"properties": {"key": "value"},
"checkpoint_location": "s3://bucket/checkpoints",
}
delta_format = table_format_from_dict(delta_dict)
assert isinstance(delta_format, DeltaFormat)
assert delta_format.checkpoint_location == "s3://bucket/checkpoints"
# Test Hudi
hudi_dict = {
"format_type": "hudi",
"table_type": "MERGE_ON_READ",
"record_key": "id",
"precombine_field": "ts",
"properties": {},
}
hudi_format = table_format_from_dict(hudi_dict)
assert isinstance(hudi_format, HudiFormat)
assert hudi_format.table_type == "MERGE_ON_READ"
# Test invalid format type
with pytest.raises(ValueError, match="Unknown table format type"):
table_format_from_dict({"format_type": "invalid"})
def test_table_format_from_json(self):
"""Test table_format_from_json function."""
iceberg_dict = {
"format_type": "iceberg",
"catalog": "test_catalog",
"namespace": "test_namespace",
"properties": {},
}
json_str = json.dumps(iceberg_dict)
iceberg_format = table_format_from_json(json_str)
assert isinstance(iceberg_format, IcebergFormat)
assert iceberg_format.catalog == "test_catalog"
assert iceberg_format.namespace == "test_namespace"
def test_table_format_error_handling(self):
"""Test error handling in table format operations."""
# Test invalid format type - create mock enum value
class MockFormat:
value = "invalid_format"
with pytest.raises(ValueError, match="Unknown table format type"):
create_table_format(MockFormat())
# Test invalid format type in from_dict
with pytest.raises(ValueError, match="Unknown table format type"):
table_format_from_dict({"format_type": "invalid"})
# Test missing format_type
with pytest.raises(KeyError):
table_format_from_dict({})
# Test invalid JSON
with pytest.raises(json.JSONDecodeError):
table_format_from_json("invalid json")
def test_table_format_property_edge_cases(self):
"""Test edge cases for table format properties."""
iceberg_format = IcebergFormat()
# Test property overwriting
iceberg_format.set_property("snapshot-id", "123")
assert iceberg_format.get_property("snapshot-id") == "123"
iceberg_format.set_property("snapshot-id", "456")
assert iceberg_format.get_property("snapshot-id") == "456"
# Test empty properties
delta_format = DeltaFormat(properties=None)
assert len(delta_format.properties) == 0
# Test None values in constructors
hudi_format = HudiFormat(
table_type=None,
record_key=None,
precombine_field=None,
properties=None,
)
assert hudi_format.table_type is None
assert hudi_format.record_key is None
assert hudi_format.precombine_field is None
def test_hudi_format_comprehensive(self):
"""Test comprehensive Hudi format functionality."""
# Test with all properties
hudi_format = HudiFormat(
table_type="COPY_ON_WRITE",
record_key="id,uuid",
precombine_field="ts",
properties={"custom.prop": "value"},
)
assert (
hudi_format.get_property("hoodie.datasource.write.table.type")
== "COPY_ON_WRITE"
)
assert (
hudi_format.get_property("hoodie.datasource.write.recordkey.field")
== "id,uuid"
)
assert (
hudi_format.get_property("hoodie.datasource.write.precombine.field") == "ts"
)
assert hudi_format.get_property("custom.prop") == "value"
# Test serialization roundtrip with complex data
serialized = hudi_format.to_dict()
restored = HudiFormat.from_dict(serialized)
assert restored.table_type == hudi_format.table_type
assert restored.record_key == hudi_format.record_key
assert restored.precombine_field == hudi_format.precombine_field
def test_table_format_with_special_characters(self):
"""Test table formats with special characters and edge values."""
# Test with unicode and special characters
iceberg_format = IcebergFormat(
catalog="测试目录", # Chinese
namespace="тест_ns", # Cyrillic
properties={"special.key": "value with spaces & symbols!@#$%^&*()"},
)
# Serialization roundtrip should preserve special characters
serialized = iceberg_format.to_dict()
restored = IcebergFormat.from_dict(serialized)
assert restored.catalog == "测试目录"
assert restored.namespace == "тест_ns"
assert (
restored.properties["special.key"]
== "value with spaces & symbols!@#$%^&*()"
)