Skip to content

Commit

Permalink
added _check_convert_dates including testcase (#65)
Browse files Browse the repository at this point in the history
* added _check_convert_dates

* added testcase
  • Loading branch information
veenstrajelmer authored Mar 11, 2024
1 parent 2f2f594 commit 896d50b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 24 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)
------------------
Expand Down
35 changes: 20 additions & 15 deletions ddlpy/ddlpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]},
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = []

Expand Down
41 changes: 32 additions & 9 deletions tests/test_ddlpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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()
Expand Down

0 comments on commit 896d50b

Please sign in to comment.