|
| 1 | +# ruff: noqa: S105, S106, SLF001, PLR2004, PD901, D209, D205 |
| 2 | + |
| 3 | +import datetime |
| 4 | +import math |
| 5 | +import os |
| 6 | + |
| 7 | +import pyarrow.dataset as ds |
| 8 | +import pytest |
| 9 | + |
| 10 | +from timdex_dataset_api.dataset import ( |
| 11 | + MAX_ROWS_PER_FILE, |
| 12 | + TIMDEX_DATASET_SCHEMA, |
| 13 | + DatasetNotLoadedError, |
| 14 | + TIMDEXDataset, |
| 15 | +) |
| 16 | +from timdex_dataset_api.record import DatasetRecord |
| 17 | + |
| 18 | + |
| 19 | +def test_dataset_record_serialization(): |
| 20 | + dataset_record = DatasetRecord( |
| 21 | + timdex_record_id="alma:123", |
| 22 | + source_record=b"<record><title>Hello World.</title></record>", |
| 23 | + transformed_record=b"""{"title":["Hello World."]}""", |
| 24 | + ) |
| 25 | + assert dataset_record.to_dict() == { |
| 26 | + "timdex_record_id": "alma:123", |
| 27 | + "source_record": b"<record><title>Hello World.</title></record>", |
| 28 | + "transformed_record": b"""{"title":["Hello World."]}""", |
| 29 | + "source": None, |
| 30 | + "run_date": None, |
| 31 | + "run_type": None, |
| 32 | + "action": None, |
| 33 | + "run_id": None, |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | +def test_dataset_record_serialization_with_partition_values_provided(): |
| 38 | + dataset_record = DatasetRecord( |
| 39 | + timdex_record_id="alma:123", |
| 40 | + source_record=b"<record><title>Hello World.</title></record>", |
| 41 | + transformed_record=b"""{"title":["Hello World."]}""", |
| 42 | + ) |
| 43 | + partition_values = { |
| 44 | + "source": "alma", |
| 45 | + "run_date": "2024-12-01", |
| 46 | + "run_type": "daily", |
| 47 | + "action": "index", |
| 48 | + "run_id": "000-111-aaa-bbb", |
| 49 | + } |
| 50 | + assert dataset_record.to_dict(partition_values=partition_values) == { |
| 51 | + "timdex_record_id": "alma:123", |
| 52 | + "source_record": b"<record><title>Hello World.</title></record>", |
| 53 | + "transformed_record": b"""{"title":["Hello World."]}""", |
| 54 | + "source": "alma", |
| 55 | + "run_date": "2024-12-01", |
| 56 | + "run_type": "daily", |
| 57 | + "action": "index", |
| 58 | + "run_id": "000-111-aaa-bbb", |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | +def test_dataset_write_records_to_new_dataset(new_temp_dataset, small_records_iter): |
| 63 | + files_written = new_temp_dataset.write(small_records_iter(10_000)) |
| 64 | + assert len(files_written) == 1 |
| 65 | + assert os.path.exists(new_temp_dataset.location) |
| 66 | + |
| 67 | + # load newly created dataset as new TIMDEXDataset instance |
| 68 | + dataset = TIMDEXDataset.load(new_temp_dataset.location) |
| 69 | + assert dataset.row_count == 10_000 |
| 70 | + |
| 71 | + |
| 72 | +def test_dataset_reload_after_write(new_temp_dataset, small_records_iter): |
| 73 | + files_written = new_temp_dataset.write(small_records_iter(10_000)) |
| 74 | + assert len(files_written) == 1 |
| 75 | + assert os.path.exists(new_temp_dataset.location) |
| 76 | + |
| 77 | + # attempt row count before reload |
| 78 | + with pytest.raises(DatasetNotLoadedError): |
| 79 | + _ = new_temp_dataset.row_count |
| 80 | + |
| 81 | + # attempt row count after reload |
| 82 | + new_temp_dataset.reload() |
| 83 | + assert new_temp_dataset.row_count == 10_000 |
| 84 | + |
| 85 | + |
| 86 | +def test_dataset_write_default_max_rows_per_file(new_temp_dataset, small_records_iter): |
| 87 | + """Default is 100k rows per file, therefore writing 200,033 records should result in |
| 88 | + 3 files (x2 @ 100k rows, x1 @ 33 rows).""" |
| 89 | + total_records = 200_033 |
| 90 | + |
| 91 | + new_temp_dataset.write(small_records_iter(total_records)) |
| 92 | + new_temp_dataset.reload() |
| 93 | + |
| 94 | + assert new_temp_dataset.row_count == total_records |
| 95 | + assert len(new_temp_dataset.dataset.files) == math.ceil( |
| 96 | + total_records / MAX_ROWS_PER_FILE |
| 97 | + ) |
| 98 | + |
| 99 | + |
| 100 | +def test_dataset_write_record_batches_uses_batch_size( |
| 101 | + new_temp_dataset, small_records_iter |
| 102 | +): |
| 103 | + total_records = 101 |
| 104 | + batch_size = 50 |
| 105 | + batches = list( |
| 106 | + new_temp_dataset.get_dataset_record_batches( |
| 107 | + small_records_iter(total_records), batch_size=batch_size |
| 108 | + ) |
| 109 | + ) |
| 110 | + assert len(batches) == math.ceil(total_records / batch_size) |
| 111 | + |
| 112 | + |
| 113 | +def test_dataset_write_to_multiple_locations_raise_error(small_records_iter): |
| 114 | + timdex_dataset = TIMDEXDataset( |
| 115 | + location=["/path/to/records-1.parquet", "/path/to/records-2.parquet"] |
| 116 | + ) |
| 117 | + with pytest.raises( |
| 118 | + TypeError, |
| 119 | + match="Dataset location must be the root of a single dataset for writing", |
| 120 | + ): |
| 121 | + timdex_dataset.write(small_records_iter(10)) |
| 122 | + |
| 123 | + |
| 124 | +def test_dataset_write_mixin_partition_values_used( |
| 125 | + new_temp_dataset, small_records_iter_without_partitions |
| 126 | +): |
| 127 | + partition_values = { |
| 128 | + "source": "alma", |
| 129 | + "run_date": "2024-12-01", |
| 130 | + "run_type": "daily", |
| 131 | + "action": "index", |
| 132 | + "run_id": "000-111-aaa-bbb", |
| 133 | + } |
| 134 | + _written_files = new_temp_dataset.write( |
| 135 | + small_records_iter_without_partitions(10), |
| 136 | + partition_values=partition_values, |
| 137 | + ) |
| 138 | + new_temp_dataset.reload() |
| 139 | + |
| 140 | + # load as pandas dataframe and assert column values |
| 141 | + df = new_temp_dataset.dataset.to_table().to_pandas() |
| 142 | + row = df.iloc[0] |
| 143 | + assert row.source == partition_values["source"] |
| 144 | + assert row.run_date == datetime.date(2024, 12, 1) |
| 145 | + assert row.run_type == partition_values["run_type"] |
| 146 | + assert row.action == partition_values["action"] |
| 147 | + assert row.action == partition_values["action"] |
| 148 | + |
| 149 | + |
| 150 | +def test_dataset_write_schema_partitions_correctly_ordered( |
| 151 | + new_temp_dataset, small_records_iter |
| 152 | +): |
| 153 | + written_files = new_temp_dataset.write( |
| 154 | + small_records_iter(10), |
| 155 | + partition_values={ |
| 156 | + "source": "alma", |
| 157 | + "run_date": "2024-12-01", |
| 158 | + "run_type": "daily", |
| 159 | + "action": "index", |
| 160 | + "run_id": "000-111-aaa-bbb", |
| 161 | + }, |
| 162 | + ) |
| 163 | + file = written_files[0] |
| 164 | + assert ( |
| 165 | + "/source=alma/run_date=2024-12-01/run_type=daily" |
| 166 | + "/action=index/run_id=000-111-aaa-bbb" in file.path |
| 167 | + ) |
| 168 | + |
| 169 | + |
| 170 | +def test_dataset_write_schema_applied_to_dataset(new_temp_dataset, small_records_iter): |
| 171 | + new_temp_dataset.write(small_records_iter(10)) |
| 172 | + |
| 173 | + # manually load dataset to confirm schema without TIMDEXDataset projecting schema |
| 174 | + # during load |
| 175 | + dataset = ds.dataset( |
| 176 | + new_temp_dataset.location, |
| 177 | + format="parquet", |
| 178 | + partitioning="hive", |
| 179 | + ) |
| 180 | + |
| 181 | + assert set(dataset.schema.names) == set(TIMDEX_DATASET_SCHEMA.names) |
0 commit comments