diff --git a/HISTORY.rst b/HISTORY.rst index b7e5097..55d1d46 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -14,6 +14,7 @@ UNRELEASED * add distinction for Groepering (timeseries vs. extremes) to `ddlpy.locations()` dataframe in https://github.com/deltares/ddlpy/pull/49 * drop `Tijdstip` column in `ddlpy.measurements()` output dataframe to avoid duplication with time index in https://github.com/deltares/ddlpy/pull/52 and https://github.com/deltares/ddlpy/pull/54 * add `ddlpy.measurements_amount()` to retrieve the number of available measurements grouped by day/month/year in https://github.com/Deltares/ddlpy/pull/63 +* catch accidentally switched start/end dates in https://github.com/Deltares/ddlpy/pull/65 0.1.0 (2019-01-03) ------------------ diff --git a/ddlpy/ddlpy.py b/ddlpy/ddlpy.py index 4e08080..07c9cbb 100644 --- a/ddlpy/ddlpy.py +++ b/ddlpy/ddlpy.py @@ -66,6 +66,21 @@ def locations(): return merged.set_index("Code") +def _check_convert_dates(start_date, end_date, return_str=True): + start_date = pd.Timestamp(start_date) + end_date = pd.Timestamp(end_date) + + if start_date > end_date: + raise ValueError("start_date is larger than end_date") + + if return_str: + start_date_str = pytz.UTC.localize(start_date).isoformat(timespec='milliseconds') + end_date_str = pytz.UTC.localize(end_date).isoformat(timespec='milliseconds') + return start_date_str, end_date_str + else: + return start_date, end_date + + def _get_request_dicts(location): aquometadata_dict = { "Eenheid": {"Code": location["Eenheid.Code"]}, @@ -94,11 +109,7 @@ def measurements_available(location, start_date, end_date): """ endpoint = ENDPOINTS['check_observations_available'] - start_date = pd.Timestamp(start_date) - end_date = pd.Timestamp(end_date) - - start_date_str = pytz.UTC.localize(start_date).isoformat(timespec='milliseconds') - end_date_str = pytz.UTC.localize(end_date).isoformat(timespec='milliseconds') + start_date_str, end_date_str = _check_convert_dates(start_date, end_date, return_str=True) request_dicts = _get_request_dicts(location) @@ -140,12 +151,8 @@ def measurements_amount(location, start_date, end_date, period="Jaar"): raise ValueError(f"period should be one of {accepted_period}, not '{period}'") endpoint = ENDPOINTS['collect_number_of_observations'] - - start_date = pd.Timestamp(start_date) - end_date = pd.Timestamp(end_date) - start_date_str = pytz.UTC.localize(start_date).isoformat(timespec='milliseconds') - end_date_str = pytz.UTC.localize(end_date).isoformat(timespec='milliseconds') + start_date_str, end_date_str = _check_convert_dates(start_date, end_date, return_str=True) request_dicts = _get_request_dicts(location) @@ -264,9 +271,8 @@ def _measurements_slice(location, start_date, end_date): """get measurements for location, for the period start_date, end_date, use measurements instead""" endpoint = ENDPOINTS["collect_observations"] - start_date_str = pytz.UTC.localize(start_date).isoformat(timespec="milliseconds") - end_date_str = pytz.UTC.localize(end_date).isoformat(timespec="milliseconds") - + start_date_str, end_date_str = _check_convert_dates(start_date, end_date, return_str=True) + request_dicts = _get_request_dicts(location) request = { @@ -295,8 +301,7 @@ def _measurements_slice(location, start_date, end_date): def measurements(location, start_date, end_date, clean_df=True): """return measurements for the given location and time window (start_date, end_date)""" - start_date = pd.Timestamp(start_date) - end_date = pd.Timestamp(end_date) + start_date, end_date = _check_convert_dates(start_date, end_date, return_str=False) measurements = [] diff --git a/tests/test_ddlpy.py b/tests/test_ddlpy.py index 88ce5dd..1bc78f8 100755 --- a/tests/test_ddlpy.py +++ b/tests/test_ddlpy.py @@ -68,14 +68,6 @@ def test_measurements_noindex(location): assert measurements.shape[0] > 1 -def test_measurements_string(location): - """measurements for a location """ - start_date = "1953-01-01" - end_date = "1953-04-01" - measurements = ddlpy.measurements(location, start_date=start_date, end_date=end_date) - assert measurements.shape[0] > 1 - - def test_measurements_latest(location): """measurements for a location """ latest = ddlpy.measurements_latest(location) @@ -138,7 +130,7 @@ def test_measurements_remove_duplicates_nottoomuch(location): def test_simplify_dataframe(location): - start_date = dt.datetime(2019,11,24) + start_date = dt.datetime(2019,11,24) end_date = dt.datetime(2019,12,5) meas_wathte = ddlpy.measurements(location, start_date=start_date, end_date=end_date) assert len(meas_wathte.columns) == 53 @@ -148,6 +140,37 @@ def test_simplify_dataframe(location): assert len(meas_simple.columns) == 2 +datetype_list = ["string", "pd.Timestamp", "dt.datetime", "mixed"] +@pytest.mark.parametrize("datetype", datetype_list) +def test_check_convert_dates(datetype): + if datetype == "string": + start_date = "1953-01-01" + end_date = "1953-04-01" + elif datetype == "pd.Timestamp": + start_date = pd.Timestamp("1953-01-01") + end_date = pd.Timestamp("1953-04-01") + elif datetype == "dt.datetime": + start_date = dt.datetime(1953,1,1) + end_date = dt.datetime(1953,4,1) + elif datetype == "mixed": + start_date = "1953-01-01" + end_date = dt.datetime(1953,4,1) + + # assert output + start_date_out, end_date_out = ddlpy.ddlpy._check_convert_dates(start_date, end_date) + assert start_date_out=='1953-01-01T00:00:00.000+00:00' + assert end_date_out=='1953-04-01T00:00:00.000+00:00' + + +def test_check_convert_wrongorder(): + start_date = "1953-01-01" + end_date = "1953-04-01" + + # assert output + with pytest.raises(ValueError): + start_date_out, end_date_out = ddlpy.ddlpy._check_convert_dates(end_date, start_date) + + def test_command_line_interface(): """Test the CLI.""" runner = CliRunner()