Skip to content

Commit

Permalink
Merge pull request #276 from primap-community/merge_logging_tests
Browse files Browse the repository at this point in the history
Add tests for merge function logging
  • Loading branch information
JGuetschow authored Oct 22, 2024
2 parents 242df60 + d282ce3 commit 85a42be
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/273.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The function `nir_convert_df_to_long` now retains NaN values instead of removing them. This is more consistent with the rest of our data reading where we keep NaN values to check that everything has been processed.
1 change: 1 addition & 0 deletions changelog/276.improvement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added tests on log content and format for the `pr.merge()` function
2 changes: 1 addition & 1 deletion primap2/_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def generate_log_message(da_error: xr.DataArray, tolerance: float) -> str:
scalar_dims_str = ", ".join(scalar_dims_format)
da_error_dequ = da_error.squeeze(drop=True).pint.dequantify()
if np.ndim(da_error_dequ.data) == 0:
errors_str = str(da_error_dequ.data)
errors_str = f"{da_error_dequ.data:.2f}"
else:
errors_str = da_error_dequ.to_dataframe().dropna().to_string()

Expand Down
40 changes: 39 additions & 1 deletion primap2/tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,17 @@ def test_merge_fail_tolerance(opulent_ds):
da_start.pr.merge(da_merge, tolerance=0.01)


def test_merge_fail_tolerance_warn(opulent_ds):
def test_merge_fail_tolerance_warn(opulent_ds, caplog):
da_start = opulent_ds["CO2"]
data_to_modify = opulent_ds["CO2"].pr.loc[{"area": ["ARG"]}].pr.sum("area")
data_to_modify.data = data_to_modify.data * 1.09
da_merge = opulent_ds["CO2"].pr.set("area", "ARG", data_to_modify, existing="overwrite")

da_result = da_start.pr.merge(da_merge, tolerance=0.01, error_on_discrepancy=False)
assert_aligned_equal(da_result, da_start)
assert (
"pr.merge error: found discrepancies larger than tolerance " "(1.00%) for " in caplog.text
)


def test_coords_not_matching_ds(opulent_ds):
Expand Down Expand Up @@ -198,3 +201,38 @@ def test_merge_message_time_daily(opulent_ds):
match=r"found discrepancies larger than tolerance \(1\.00%\) for time=2000-01-02",
):
da_start.pr.merge(da_merge)


def test_log_formatting(minimal_ds, caplog):
da_start = minimal_ds["CO2"]
data_to_modify = (
minimal_ds["CO2"].pr.loc[{"area": ["ARG"], "time": ["2001", "2002"]}].pr.sum("area")
)
data_to_modify.data = data_to_modify.data * 1.09
da_merge = minimal_ds["CO2"].pr.set("area", "ARG", data_to_modify, existing="overwrite")

da_result = da_start.pr.merge(da_merge, tolerance=0.01, error_on_discrepancy=False)
assert_aligned_equal(da_result, da_start)
assert (
"pr.merge error: found discrepancies larger than tolerance (1.00%) "
"for area (ISO3)=ARG, source=RAND2020:" in caplog.text
)
assert (
"(CO2)\n CO2\ntime \n2001-01-01 0.09"
"\n2002-01-01 0.09" in caplog.text
)


def test_log_formatting_single_date(minimal_ds, caplog):
da_start = minimal_ds["CO2"]
data_to_modify = minimal_ds["CO2"].pr.loc[{"area": ["ARG"], "time": ["2000"]}].pr.sum("area")
data_to_modify.data = data_to_modify.data * 1.09
da_merge = minimal_ds["CO2"].pr.set("area", "ARG", data_to_modify, existing="overwrite")

da_result = da_start.pr.merge(da_merge, tolerance=0.01, error_on_discrepancy=False)
assert_aligned_equal(da_result, da_start)
assert (
"pr.merge error: found discrepancies larger than tolerance (1.00%) "
"for time=2000, area (ISO3)=ARG, source=RAND2020:" in caplog.text
)
assert "(CO2)\n0.09" in caplog.text

0 comments on commit 85a42be

Please sign in to comment.