Skip to content

Commit

Permalink
Avoid pytest warnings (#1064)
Browse files Browse the repository at this point in the history
* prevent parsing of multiple datetime columns

* captured cdsapi warnings
  • Loading branch information
veenstrajelmer authored Jan 16, 2025
1 parent af2ea58 commit 26f86db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 6 additions & 1 deletion dfm_tools/observations.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,12 @@ def gesla3_ssh_retrieve_data(row, dir_output, time_min=None, time_max=None,
with gesla3_zip.open(file_gesla, "r") as f:
data = pd.read_csv(f, comment='#', sep="\\s+",
names=["date", "time", "sea_level", "qc_flag", "use_flag"],
parse_dates=[[0, 1]], index_col=0)
)
# set datetimes as index
dates = data.pop("date")
times = data.pop("time")
data_dt = dates + " " + times
data.index = pd.to_datetime(data_dt)

# clean up time duplicates
data.index.name = 'time'
Expand Down
16 changes: 12 additions & 4 deletions tests/test_download.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,18 +130,22 @@ def test_cds_credentials_oldurl_incorrectkey_rcfile():
cds_apikey_temp = "INCORRECT-APIKEY"
cds_set_credentials_rcfile(cds_url_temp, cds_apikey_temp)
with pytest.raises(ValueError) as e:
cds_credentials()
with pytest.warns(UserWarning) as w:
cds_credentials()
assert "Old CDS URL found" in str(e.value)
assert "The CDS/ECMWF apikey file (~/.cdsapirc) was deleted" in str(e.value)
assert "404 Client Error: Not Found for url:" in str(w[0].message)

# test
cds_url_temp = "https://cds-beta.climate.copernicus.eu/api"
cds_apikey_temp = "INCORRECT-APIKEY"
cds_set_credentials_rcfile(cds_url_temp, cds_apikey_temp)
with pytest.raises(ValueError) as e:
cds_credentials()
with pytest.warns(UserWarning) as w:
cds_credentials()
assert "Old CDS URL found" in str(e.value)
assert "The CDS/ECMWF apikey file (~/.cdsapirc) was deleted" in str(e.value)
assert "certificate verify failed" in str(w[0].message)

# restore credentials file/envvars
set_cds_credentials_ifnot_none(cds_url, cds_apikey)
Expand All @@ -158,17 +162,21 @@ def test_cds_credentials_oldurl_incorrectkey_envvars():
os.environ["CDSAPI_URL"] = "https://cds.climate.copernicus.eu/api/v2"
os.environ["CDSAPI_KEY"] = "INCORRECT-APIKEY"
with pytest.raises(ValueError) as e:
cds_credentials()
with pytest.warns(UserWarning) as w:
cds_credentials()
assert "Old CDS URL found" in str(e.value)
assert "The CDS/ECMWF apikey file (~/.cdsapirc) was deleted" in str(e.value)
assert "404 Client Error: Not Found for url:" in str(w[0].message)

# test
os.environ["CDSAPI_URL"] = "https://cds-beta.climate.copernicus.eu/api"
os.environ["CDSAPI_KEY"] = "INCORRECT-APIKEY"
with pytest.raises(ValueError) as e:
cds_credentials()
with pytest.warns(UserWarning) as w:
cds_credentials()
assert "Old CDS URL found" in str(e.value)
assert "The CDS/ECMWF apikey file (~/.cdsapirc) was deleted" in str(e.value)
assert "certificate verify failed" in str(w[0].message)

# restore credentials file/envvars
set_cds_credentials_ifnot_none(cds_url, cds_apikey)
Expand Down

0 comments on commit 26f86db

Please sign in to comment.