-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_metadata.py
More file actions
444 lines (347 loc) · 13.8 KB
/
test_metadata.py
File metadata and controls
444 lines (347 loc) · 13.8 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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# ruff: noqa: S105, S108
import glob
import os
from pathlib import Path
from duckdb import DuckDBPyConnection
from timdex_dataset_api import TIMDEXDataset
ORDERED_METADATA_COLUMN_NAMES = [
"timdex_record_id",
"source",
"run_date",
"run_type",
"action",
"run_id",
"run_record_offset",
"run_timestamp",
"filename",
]
def test_tdm_init_no_metadata_file_warning_success(caplog, tmp_path):
# creating a new TIMDEXDataset will log warning if no metadata file
caplog.set_level("WARNING")
TIMDEXDataset(str(tmp_path / "new_empty_dataset"))
assert "Static metadata database not found" in caplog.text
def test_tdm_local_dataset_structure_properties(tmp_path):
local_root = str(Path(tmp_path) / "path/to/nothing")
td_local = TIMDEXDataset(local_root)
assert td_local.metadata.location == local_root
assert td_local.metadata.location_scheme == "file"
def test_tdm_s3_dataset_structure_properties(timdex_dataset_empty):
# test that location_scheme property works correctly for local paths
# S3 tests require full mocking and are covered in other tests
assert timdex_dataset_empty.metadata.location_scheme == "file"
def test_tdm_create_metadata_database_file_success(
caplog, timdex_dataset_with_runs, timdex_metadata_empty
):
caplog.set_level("DEBUG")
# use a fresh dataset from timdex_dataset_with_runs location
td = TIMDEXDataset(timdex_dataset_with_runs.location)
td.metadata.rebuild_dataset_metadata()
def test_tdm_init_metadata_file_found_success(timdex_metadata):
assert isinstance(timdex_metadata.conn, DuckDBPyConnection)
def test_tdm_duckdb_context_creates_metadata_schema(timdex_metadata):
assert (
timdex_metadata.conn.query(
"""
select count(*)
from information_schema.schemata
where catalog_name = 'memory'
and schema_name = 'metadata';
"""
).fetchone()[0]
== 1
)
def test_tdm_connection_has_static_database_attached(timdex_metadata):
assert set(
timdex_metadata.conn.query("""show databases;""").to_df().database_name
) == {"memory", "static_db"}
def test_tdm_connection_static_database_records_table_exists(timdex_metadata):
records_df = timdex_metadata.conn.query(
"""select * from static_db.records;"""
).to_df()
assert len(records_df) > 0
def test_dataset_metadata_structure_is_idempotent(timdex_metadata):
assert os.path.exists(timdex_metadata.metadata_root)
start_file_count = glob.glob(f"{timdex_metadata.metadata_root}/**/*")
timdex_metadata.create_metadata_structure()
assert os.path.exists(timdex_metadata.metadata_root)
end_file_count = glob.glob(f"{timdex_metadata.metadata_root}/**/*")
assert start_file_count == end_file_count
def test_tdm_views_created_on_init(timdex_metadata):
views = timdex_metadata.conn.query(
"""select table_name from information_schema.tables where table_type = 'VIEW';"""
).to_df()
expected_views = {"append_deltas", "records", "current_records"}
actual_views = set(views.table_name)
assert expected_views <= actual_views
def test_tdm_records_view_structure(timdex_metadata):
records_df = timdex_metadata.conn.query(
"""select * from metadata.records limit 1;"""
).to_df()
expected_columns = {
"timdex_record_id",
"source",
"run_date",
"run_type",
"action",
"run_id",
"run_record_offset",
"run_timestamp",
"filename",
}
assert set(records_df.columns) == expected_columns
def test_tdm_current_records_view_structure(timdex_metadata):
current_records_df = timdex_metadata.conn.query(
"""select * from metadata.current_records limit 1;"""
).to_df()
expected_columns = {
"timdex_record_id",
"source",
"run_date",
"run_type",
"action",
"run_id",
"run_record_offset",
"run_timestamp",
"filename",
}
assert set(current_records_df.columns) == expected_columns
def test_tdm_append_deltas_view_empty_structure(timdex_metadata):
append_deltas_df = timdex_metadata.conn.query(
"""select * from metadata.append_deltas;"""
).to_df()
expected_columns = {
"timdex_record_id",
"source",
"run_date",
"run_type",
"action",
"run_id",
"run_record_offset",
"run_timestamp",
"filename",
}
assert set(append_deltas_df.columns) == expected_columns
assert len(append_deltas_df) == 0
def test_tdm_records_count_property(timdex_metadata):
assert timdex_metadata.records_count > 0
manual_count = timdex_metadata.conn.query(
"""select count(*) from metadata.records;"""
).fetchone()[0]
assert timdex_metadata.records_count == manual_count
def test_tdm_current_records_count_property(timdex_metadata):
assert timdex_metadata.current_records_count > 0
manual_count = timdex_metadata.conn.query(
"""select count(*) from metadata.current_records;"""
).fetchone()[0]
assert timdex_metadata.current_records_count == manual_count
def test_tdm_append_deltas_count_property_empty(timdex_metadata):
assert timdex_metadata.append_deltas_count == 0
def test_tdm_records_equals_static_without_deltas(timdex_metadata):
static_count = timdex_metadata.conn.query(
"""select count(*) from static_db.records;"""
).fetchone()[0]
records_count = timdex_metadata.conn.query(
"""select count(*) from metadata.records;"""
).fetchone()[0]
assert static_count == records_count
def test_tdm_current_records_filtering_logic(timdex_metadata):
current_count = timdex_metadata.current_records_count
total_count = timdex_metadata.records_count
assert current_count <= total_count
assert current_count > 0
def test_tdm_views_with_append_deltas(timdex_metadata_with_deltas):
views = timdex_metadata_with_deltas.conn.query(
"""select table_name from information_schema.tables where table_type = 'VIEW';"""
).to_df()
expected_views = {"append_deltas", "records", "current_records"}
actual_views = set(views.table_name)
assert expected_views.issubset(actual_views)
def test_tdm_append_deltas_view_has_data(timdex_metadata_with_deltas):
append_deltas_count = timdex_metadata_with_deltas.append_deltas_count
assert append_deltas_count > 0
def test_tdm_records_includes_deltas(timdex_metadata_with_deltas):
static_count = timdex_metadata_with_deltas.conn.query(
"""select count(*) from static_db.records;"""
).fetchone()[0]
deltas_count = timdex_metadata_with_deltas.append_deltas_count
records_count = timdex_metadata_with_deltas.records_count
assert records_count == static_count + deltas_count
assert records_count > static_count
def test_tdm_current_records_with_deltas_logic(timdex_metadata_with_deltas):
current_count = timdex_metadata_with_deltas.current_records_count
total_count = timdex_metadata_with_deltas.records_count
assert current_count <= total_count
assert current_count > 0
# verify current records view returns unique timdex_record_id values
current_records_df = timdex_metadata_with_deltas.conn.query(
"""select timdex_record_id from metadata.current_records;"""
).to_df()
unique_count = len(current_records_df.timdex_record_id.unique())
assert unique_count == current_count
def test_tdm_current_records_most_recent_version(timdex_metadata_with_deltas):
# check that for records with multiple versions, only the most recent is returned
multi_version_records = timdex_metadata_with_deltas.conn.query(
"""
select timdex_record_id, count(*) as version_count
from metadata.records
group by timdex_record_id
having count(*) > 1
limit 1;
"""
).to_df()
if len(multi_version_records) > 0:
record_id = multi_version_records.iloc[0]["timdex_record_id"]
# get most recent timestamp for this record
most_recent = timdex_metadata_with_deltas.conn.query(
f"""
select run_timestamp, run_id
from metadata.records
where timdex_record_id = '{record_id}'
order by run_timestamp desc
limit 1;
"""
).to_df()
# verify current_records contains this version
current_version = timdex_metadata_with_deltas.conn.query(
f"""
select run_timestamp, run_id
from metadata.current_records
where timdex_record_id = '{record_id}';
"""
).to_df()
assert len(current_version) == 1
assert (
current_version.iloc[0]["run_timestamp"]
== most_recent.iloc[0]["run_timestamp"]
)
assert current_version.iloc[0]["run_id"] == most_recent.iloc[0]["run_id"]
def test_tdm_merge_append_deltas_static_counts_match_records_count_before_merge(
timdex_metadata_with_deltas, timdex_metadata_merged_deltas
):
static_count_merged_deltas = timdex_metadata_merged_deltas.conn.query(
"""select count(*) as count from static_db.records;"""
).fetchone()[0]
assert static_count_merged_deltas == timdex_metadata_with_deltas.records_count
def test_tdm_merge_append_deltas_adds_records_to_static_db(
timdex_metadata_with_deltas, timdex_metadata_merged_deltas
):
append_deltas = timdex_metadata_with_deltas.conn.query(
f"""
select
{','.join(ORDERED_METADATA_COLUMN_NAMES)}
from metadata.append_deltas
"""
).to_df()
merged_static_db = timdex_metadata_merged_deltas.conn.query(
f"""
select
{','.join(ORDERED_METADATA_COLUMN_NAMES)}
from static_db.records
"""
).to_df()
assert set(map(tuple, append_deltas.to_numpy())).issubset(
set(map(tuple, merged_static_db.to_numpy()))
)
def test_tdm_merge_append_deltas_deletes_append_deltas(
timdex_metadata_with_deltas, timdex_metadata_merged_deltas
):
assert timdex_metadata_with_deltas.append_deltas_count != 0
assert os.listdir(timdex_metadata_with_deltas.append_deltas_path)
assert timdex_metadata_merged_deltas.append_deltas_count == 0
assert not os.listdir(timdex_metadata_merged_deltas.append_deltas_path)
def test_td_prepare_duckdb_secret_and_extensions_home_env_var_set_and_valid(
monkeypatch, tmp_path_factory, timdex_dataset_with_runs
):
preset_home = tmp_path_factory.mktemp("my-account")
monkeypatch.setenv("HOME", str(preset_home))
td = TIMDEXDataset(timdex_dataset_with_runs.location)
df = (
td.conn.query(
"""
select
current_setting('secret_directory') as secret_directory,
current_setting('extension_directory') as extension_directory
;
"""
)
.to_df()
.iloc[0]
)
assert "my-account" in df.secret_directory
assert df.extension_directory == "" # expected and okay when HOME set
def test_td_prepare_duckdb_secret_and_extensions_home_env_var_unset(
monkeypatch, timdex_dataset_with_runs
):
monkeypatch.delenv("HOME", raising=False)
td = TIMDEXDataset(timdex_dataset_with_runs.location)
df = (
td.conn.query(
"""
select
current_setting('secret_directory') as secret_directory,
current_setting('extension_directory') as extension_directory
;
"""
)
.to_df()
.iloc[0]
)
assert df.secret_directory == "/tmp/.duckdb/secrets"
assert df.extension_directory == "/tmp/.duckdb/extensions"
def test_td_prepare_duckdb_secret_and_extensions_home_env_var_set_but_empty(
monkeypatch, timdex_dataset_with_runs
):
monkeypatch.setenv("HOME", "") # simulate AWS Lambda environment
td = TIMDEXDataset(timdex_dataset_with_runs.location)
df = (
td.conn.query(
"""
select
current_setting('secret_directory') as secret_directory,
current_setting('extension_directory') as extension_directory
;
"""
)
.to_df()
.iloc[0]
)
assert df.secret_directory == "/tmp/.duckdb/secrets"
assert df.extension_directory == "/tmp/.duckdb/extensions"
def test_td_preload_current_records_default_false(tmp_path):
td = TIMDEXDataset(str(tmp_path))
assert td.preload_current_records is False
assert td.metadata.preload_current_records is False
def test_td_preload_current_records_flag_true(tmp_path):
td = TIMDEXDataset(str(tmp_path), preload_current_records=True)
assert td.preload_current_records is True
assert td.metadata.preload_current_records is True
def test_tdm_preload_false_no_temp_table(timdex_dataset_with_runs):
# instantiate TIMDEXDataset without preloading current records (default)
td = TIMDEXDataset(timdex_dataset_with_runs.location)
# assert that materialized, temporary table "temp.current_records" does not exist
temp_table_count = td.metadata.conn.query(
"""
select count(*)
from information_schema.tables
where table_catalog = 'temp'
and table_name = 'current_records'
and table_type = 'LOCAL TEMPORARY'
;
"""
).fetchone()[0]
assert temp_table_count == 0
def test_tdm_preload_true_has_temp_table(timdex_dataset_with_runs):
# instantiate TIMDEXDataset with preloading current records
td = TIMDEXDataset(timdex_dataset_with_runs.location, preload_current_records=True)
# assert that materialized, temporary table "temp.current_records" does exist
temp_table_count = td.metadata.conn.query(
"""
select count(*)
from information_schema.tables
where table_catalog = 'temp'
and table_name = 'current_records'
and table_type = 'LOCAL TEMPORARY'
;
"""
).fetchone()[0]
assert temp_table_count == 1