Skip to content

Commit 90acfa7

Browse files
Improve unit tests for TIMDEXDatasetMetadata.merge_append_deltas
1 parent 8cee97d commit 90acfa7

File tree

2 files changed

+68
-17
lines changed

2 files changed

+68
-17
lines changed

tests/conftest.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""tests/conftest.py"""
22

3+
import shutil
34
from collections.abc import Iterator
45

56
import boto3
@@ -265,6 +266,26 @@ def timdex_metadata_with_deltas(
265266
return TIMDEXDatasetMetadata(timdex_dataset_with_runs.location)
266267

267268

269+
@pytest.fixture
270+
def timdex_metadata_merged_deltas(
271+
tmp_path, timdex_metadata_with_deltas, timdex_dataset_with_runs
272+
):
273+
"""TIMDEXDatasetMetadata after merging append deltas to static database file."""
274+
# copy directory of a dataset with runs
275+
dataset_location = str(tmp_path / "cloned_dataset_with_runs/")
276+
shutil.copytree(timdex_metadata_with_deltas.location, dataset_location)
277+
278+
# clone dataset with runs using new dataset location
279+
td = TIMDEXDataset(dataset_location, config=timdex_dataset_with_runs.config)
280+
281+
# clone metadata and merge append deltas
282+
metadata = TIMDEXDatasetMetadata(td.location)
283+
metadata.merge_append_deltas()
284+
metadata.refresh()
285+
286+
return metadata
287+
288+
268289
# ================================================================================
269290
# Utility Fixtures
270291
# ================================================================================

tests/test_metadata.py

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66

77
from timdex_dataset_api import TIMDEXDatasetMetadata
88

9+
ORDERED_METADATA_COLUMN_NAMES = [
10+
"timdex_record_id",
11+
"source",
12+
"run_date",
13+
"run_type",
14+
"action",
15+
"run_id",
16+
"run_record_offset",
17+
"run_timestamp",
18+
"filename",
19+
]
20+
921

1022
def test_tdm_init_no_metadata_file_warning_success(caplog, timdex_dataset_with_runs):
1123
TIMDEXDatasetMetadata(timdex_dataset_with_runs.location)
@@ -264,26 +276,44 @@ def test_tdm_current_records_most_recent_version(timdex_metadata_with_deltas):
264276
assert current_version.iloc[0]["run_id"] == most_recent.iloc[0]["run_id"]
265277

266278

267-
def test_tdm_merge_append_deltas(timdex_metadata_with_deltas):
268-
# get record count from static db
269-
metadata_db_current_count = timdex_metadata_with_deltas.conn.query(
279+
def test_tdm_merge_append_deltas_static_counts_match_records_count_before_merge(
280+
timdex_metadata_with_deltas, timdex_metadata_merged_deltas
281+
):
282+
static_count_merged_deltas = timdex_metadata_merged_deltas.conn.query(
270283
"""select count(*) as count from static_db.records;"""
271284
).fetchone()[0]
272-
total_count = timdex_metadata_with_deltas.records_count
285+
assert static_count_merged_deltas == timdex_metadata_with_deltas.records_count
273286

274-
# merge append deltas into static db file
275-
timdex_metadata_with_deltas.merge_append_deltas()
276-
timdex_metadata_with_deltas.refresh()
277287

278-
# get updated record count from static db
279-
metadata_db_updated_count = timdex_metadata_with_deltas.conn.query(
280-
"""select count(*) as count from static_db.records;"""
281-
).fetchone()[0]
288+
def test_tdm_merge_append_deltas_adds_records_to_static_db(
289+
timdex_metadata_with_deltas, timdex_metadata_merged_deltas
290+
):
291+
append_deltas = timdex_metadata_with_deltas.conn.query(
292+
f"""
293+
select
294+
{','.join(ORDERED_METADATA_COLUMN_NAMES)}
295+
from metadata.append_deltas
296+
"""
297+
).to_df()
298+
299+
merged_static_db = timdex_metadata_merged_deltas.conn.query(
300+
f"""
301+
select
302+
{','.join(ORDERED_METADATA_COLUMN_NAMES)}
303+
from static_db.records
304+
"""
305+
).to_df()
306+
307+
assert set(map(tuple, append_deltas.to_numpy())).issubset(
308+
set(map(tuple, merged_static_db.to_numpy()))
309+
)
310+
282311

283-
# verify the addition of new records
284-
assert metadata_db_current_count < metadata_db_updated_count
285-
assert metadata_db_updated_count == total_count
312+
def test_tdm_merge_append_deltas_deletes_append_deltas(
313+
timdex_metadata_with_deltas, timdex_metadata_merged_deltas
314+
):
315+
assert timdex_metadata_with_deltas.append_deltas_count != 0
316+
assert os.listdir(timdex_metadata_with_deltas.append_deltas_path)
286317

287-
# verify append deltas are deleted
288-
assert timdex_metadata_with_deltas.append_deltas_count == 0
289-
assert os.listdir(timdex_metadata_with_deltas.append_deltas_path) == []
318+
assert timdex_metadata_merged_deltas.append_deltas_count == 0
319+
assert not os.listdir(timdex_metadata_merged_deltas.append_deltas_path)

0 commit comments

Comments
 (0)