Skip to content

Commit 6998358

Browse files
committed
Add more tests
1 parent 915c7a4 commit 6998358

File tree

4 files changed

+59
-8
lines changed

4 files changed

+59
-8
lines changed

src/pandas_openscm/db/netcdf.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,21 @@ def save_data(self, data: pd.DataFrame, data_file: Path) -> None:
9696
# Resetting the index will also give each timeseries a unique ID
9797
data_rs = data.reset_index()
9898
timeseries_coord_info = {self.timeseries_dim: data_rs.index.values}
99-
time_coord_info = {"time": data.columns}
99+
if data.columns.name is None:
100+
time_dim = "time"
101+
else:
102+
time_dim = data.columns.name
103+
104+
time_coord_info = {time_dim: data.columns.values}
105+
100106
data_index_xr = metadata_df_to_xr(
101107
data_rs[data.index.names],
102108
timeseries_id_coord=xr.Coordinates(timeseries_coord_info),
103109
timeseries_dim=self.timeseries_dim,
104110
)
105111
data_values_xr = xr.DataArray(
106112
data,
107-
dims=[self.timeseries_dim, "time"],
113+
dims=[self.timeseries_dim, time_dim],
108114
coords=xr.Coordinates(timeseries_coord_info | time_coord_info),
109115
)
110116
data_xr = xr.merge([data_index_xr, data_values_xr.to_dataset(name="values")])

src/pandas_openscm/db/openscm_db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,11 @@ def from_gzipped_tar_archive(
339339
):
340340
backend_data = DATA_BACKENDS.guess_backend(member.name)
341341

342-
if backend_data is None: # pragma: noqa
342+
if backend_data is None: # pragma: no cover
343343
# Should be impossible to get here
344344
raise TypeError(backend_data)
345345

346-
if backend_index is None: # pragma: noqa
346+
if backend_index is None: # pragma: no cover
347347
# Should be impossible to get here
348348
raise TypeError(backend_index)
349349

tests/integration/database/test_integration_database_save_load.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def test_save_and_load_basic(tmpdir, db_data_backend, db_index_backend):
4343

4444
df_timeseries_like = pd.DataFrame(
4545
np.arange(12).reshape(4, 3),
46-
columns=[2010, 2015, 2025],
46+
columns=pd.Index([2010, 2015, 2025], name="year"),
4747
index=pd.MultiIndex.from_tuples(
4848
[
4949
("scenario_a", "climate_model_a", "Temperature", "K"),
@@ -70,7 +70,10 @@ def test_save_and_load_basic(tmpdir, db_data_backend, db_index_backend):
7070
df_timeseries_like.index, metadata_compare, exact="equiv", check_order=False
7171
)
7272

73-
loaded = db.load(out_columns_type=df_timeseries_like.columns.dtype)
73+
loaded = db.load(
74+
out_columns_type=df_timeseries_like.columns.dtype,
75+
out_columns_name=df_timeseries_like.columns.name,
76+
)
7477

7578
assert_frame_alike(df_timeseries_like, loaded)
7679

tests/unit/database/test_database.py

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,31 @@ def test_available_backends_data():
4040

4141

4242
def test_unavailable_data_backend():
43-
with pytest.raises(KeyError):
43+
with pytest.raises(
44+
KeyError, match=re.escape("option='junk' is not supported. Available options:")
45+
):
4446
DATA_BACKENDS.get_instance("junk")
4547

4648

49+
def test_guess_backend_data():
50+
assert isinstance(DATA_BACKENDS.guess_backend("0.csv"), CSVDataBackend)
51+
assert isinstance(DATA_BACKENDS.guess_backend("0.feather"), FeatherDataBackend)
52+
assert isinstance(DATA_BACKENDS.guess_backend("0.in-mem"), InMemoryDataBackend)
53+
assert isinstance(DATA_BACKENDS.guess_backend("0.nc"), netCDFDataBackend)
54+
55+
56+
def test_guess_data_backend_error():
57+
with pytest.raises(
58+
ValueError,
59+
match=re.escape(
60+
"Could not guess backend from data_file_name='0.junk'. "
61+
"The file's extension does not match any of the available options: "
62+
"known_options_and_extensions="
63+
),
64+
):
65+
DATA_BACKENDS.guess_backend("0.junk")
66+
67+
4768
def test_available_backends_index():
4869
assert isinstance(INDEX_BACKENDS.get_instance("csv"), CSVIndexBackend)
4970
assert isinstance(INDEX_BACKENDS.get_instance("feather"), FeatherIndexBackend)
@@ -52,10 +73,31 @@ def test_available_backends_index():
5273

5374

5475
def test_unavailable_index_backend():
55-
with pytest.raises(KeyError):
76+
with pytest.raises(
77+
KeyError, match=re.escape("option='junk' is not supported. Available options:")
78+
):
5679
INDEX_BACKENDS.get_instance("junk")
5780

5881

82+
def test_guess_backend_index():
83+
assert isinstance(INDEX_BACKENDS.guess_backend("0.csv"), CSVIndexBackend)
84+
assert isinstance(INDEX_BACKENDS.guess_backend("0.feather"), FeatherIndexBackend)
85+
assert isinstance(INDEX_BACKENDS.guess_backend("0.in-mem"), InMemoryIndexBackend)
86+
assert isinstance(INDEX_BACKENDS.guess_backend("0.nc"), netCDFIndexBackend)
87+
88+
89+
def test_guess_index_backend_error():
90+
with pytest.raises(
91+
ValueError,
92+
match=re.escape(
93+
"Could not guess backend from index_file_name='index.junk'. "
94+
"The file's extension does not match any of the available options: "
95+
"known_options_and_extensions="
96+
),
97+
):
98+
INDEX_BACKENDS.guess_backend("index.junk")
99+
100+
59101
def test_filelock_not_available_default_initialisation(tmpdir):
60102
with patch.dict(sys.modules, {"filelock": None}):
61103
with pytest.raises(

0 commit comments

Comments
 (0)