-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from MITLibraries/TIMX-417-read-from-dataset
TIMX 417 - read from dataset
- Loading branch information
Showing
4 changed files
with
220 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# ruff: noqa: PLR2004, PD901 | ||
|
||
import pandas as pd | ||
import pyarrow as pa | ||
import pytest | ||
|
||
DATASET_COLUMNS_SET = { | ||
"timdex_record_id", | ||
"source_record", | ||
"transformed_record", | ||
"source", | ||
"run_date", | ||
"run_type", | ||
"run_id", | ||
"action", | ||
"year", | ||
"month", | ||
"day", | ||
} | ||
|
||
|
||
def test_read_batches_yields_pyarrow_record_batches(fixed_local_dataset): | ||
batches = fixed_local_dataset.read_batches_iter() | ||
batch = next(batches) | ||
assert isinstance(batch, pa.RecordBatch) | ||
|
||
|
||
def test_read_batches_all_columns_by_default(fixed_local_dataset): | ||
batches = fixed_local_dataset.read_batches_iter() | ||
batch = next(batches) | ||
assert set(batch.column_names) == DATASET_COLUMNS_SET | ||
|
||
|
||
def test_read_batches_filter_columns(fixed_local_dataset): | ||
columns_subset = ["source", "transformed_record"] | ||
batches = fixed_local_dataset.read_batches_iter(columns=columns_subset) | ||
batch = next(batches) | ||
assert set(batch.column_names) == set(columns_subset) | ||
|
||
|
||
def test_read_batches_no_filters_gets_full_dataset(fixed_local_dataset): | ||
batches = fixed_local_dataset.read_batches_iter() | ||
table = pa.Table.from_batches(batches) | ||
assert len(table) == fixed_local_dataset.row_count | ||
|
||
|
||
def test_read_batches_with_filters_gets_subset_of_dataset(fixed_local_dataset): | ||
batches = fixed_local_dataset.read_batches_iter( | ||
source="libguides", | ||
run_date="2024-12-01", | ||
run_type="daily", | ||
action="index", | ||
) | ||
|
||
table = pa.Table.from_batches(batches) | ||
assert len(table) == 1_000 | ||
assert len(table) < fixed_local_dataset.row_count | ||
|
||
# assert loaded dataset is unchanged by filtering for a read method | ||
assert fixed_local_dataset.row_count == 5_000 | ||
|
||
|
||
def test_read_dataframe_batches_yields_dataframes(fixed_local_dataset): | ||
df_iter = fixed_local_dataset.read_dataframes_iter() | ||
df_batch = next(df_iter) | ||
assert isinstance(df_batch, pd.DataFrame) | ||
assert len(df_batch) == 1_000 | ||
|
||
|
||
def test_read_dataframe_reads_all_dataset_rows_after_filtering(fixed_local_dataset): | ||
df = fixed_local_dataset.read_dataframe() | ||
assert isinstance(df, pd.DataFrame) | ||
assert len(df) == fixed_local_dataset.row_count | ||
|
||
|
||
def test_read_dicts_yields_dictionary_for_each_dataset_record(fixed_local_dataset): | ||
records = fixed_local_dataset.read_dicts_iter() | ||
record = next(records) | ||
assert isinstance(record, dict) | ||
assert set(record.keys()) == DATASET_COLUMNS_SET | ||
|
||
|
||
def test_read_batches_filter_to_none_returns_empty_list(fixed_local_dataset): | ||
batches = fixed_local_dataset.read_batches_iter(source="not-gonna-find-me") | ||
assert list(batches) == [] | ||
|
||
|
||
def test_read_dicts_filter_to_none_stopiteration_immediately(fixed_local_dataset): | ||
batches = fixed_local_dataset.read_dicts_iter(source="not-gonna-find-me") | ||
with pytest.raises(StopIteration): | ||
next(batches) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.