Skip to content

Commit

Permalink
ensure applied_filters items are dictionaries (#168)
Browse files Browse the repository at this point in the history
* handle FilterType conversion to dict

* update tests to check applied_filter types

* add assertions for sample_history structure
  • Loading branch information
shouples authored Feb 3, 2023
1 parent bbde049 commit e11b4bb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/dx/utils/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ def generate_metadata(
dataframe_info = parent_dataframe_info
# these are set whenever store_sample_to_history() is called after a filter action from the frontend
sample_history = existing_metadata.get("datalink", {}).get("sample_history", [])
filters = parent_dxdf.filters
# we shouldn't have a mix of pydantic FilterTypes and dicts here, but just in case
filters = [f.dict() if not isinstance(f, dict) else f for f in parent_dxdf.filters]

metadata = {
"datalink": {
Expand Down
34 changes: 24 additions & 10 deletions tests/test_resampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,27 @@ def test_store_sample_to_history(
filters,
)

assert sample_dxdataframe.metadata["datalink"]["applied_filters"] == filters
assert sample_dxdataframe.metadata["datalink"]["sampling_time"] is not None
datalink_metadata = sample_dxdataframe.metadata["datalink"]

assert (
sample_dxdataframe.metadata["datalink"]["dataframe_info"]["orig_num_rows"]
== sample_dxdataframe.df.shape[0]
)
assert (
sample_dxdataframe.metadata["datalink"]["dataframe_info"]["orig_num_cols"]
== sample_dxdataframe.df.shape[1]
)
assert datalink_metadata["applied_filters"] == filters
if has_filters:
# ensure sampling history and currently applied filters are present
# and properly formatted for the frontend to reference
applied_filters = datalink_metadata["applied_filters"]
assert isinstance(applied_filters[0], dict)

sample_history = datalink_metadata["sample_history"]
assert len(sample_history) == 1
assert "filters" in sample_history[0]
assert isinstance(sample_history[0]["filters"], list)
assert isinstance(sample_history[0]["filters"][0], dict)
assert sample_history[0]["filters"][0] == filters[0]
assert sample_history[0]["sampling_time"] is not None

assert datalink_metadata["sampling_time"] is not None

assert datalink_metadata["dataframe_info"]["orig_num_rows"] == sample_dxdataframe.df.shape[0]
assert datalink_metadata["dataframe_info"]["orig_num_cols"] == sample_dxdataframe.df.shape[1]


class TestResample:
Expand Down Expand Up @@ -259,6 +269,10 @@ def test_resample_associates_subset_to_parent(
== SUBSET_HASH_TO_PARENT_DATA[resampled_dxdf.hash]["display_id"]
)

if has_filters:
applied_filter = resampled_dxdf.metadata["datalink"]["applied_filters"][0]
assert isinstance(applied_filter, dict)

@pytest.mark.parametrize("has_filters", [True, False])
@pytest.mark.parametrize("display_mode", ["simple", "enhanced"])
def test_resample_associates_subset_to_parent_after_other_execution(
Expand Down

0 comments on commit e11b4bb

Please sign in to comment.