Skip to content

Commit d35afbd

Browse files
committed
Expand tests
1 parent 26b1203 commit d35afbd

File tree

2 files changed

+67
-35
lines changed

2 files changed

+67
-35
lines changed

src/pandas_openscm/db/backends.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,19 @@ def guess_backend(self, data_file_name: str) -> OpenSCMDBDataBackend:
7979
ValueError
8080
The backend could not be guessed from `data_file_name`
8181
"""
82-
ext = Path(data_file_name).suffix.strip(".")
83-
try:
84-
return self.get_instance(ext)
85-
except KeyError as exc:
86-
msg = (
87-
f"Could not guess backend from filename {data_file_name}. "
88-
f"we assumed the extension was {ext}."
89-
)
90-
raise ValueError(msg) from exc
82+
ext = Path(data_file_name).suffix
83+
for _, option_cls in self.options:
84+
option = option_cls()
85+
if ext == option.ext:
86+
return option
87+
88+
known_options_and_extensions = [(v[0], v[1]().ext) for v in self.options]
89+
msg = (
90+
f"Could not guess backend from {data_file_name=!r}. "
91+
"The file's extension does not match any of the available options: "
92+
f"{known_options_and_extensions=}"
93+
)
94+
raise ValueError(msg)
9195

9296

9397
DATA_BACKENDS = DataBackendOptions(
@@ -167,15 +171,19 @@ def guess_backend(self, index_file_name: str) -> OpenSCMDBIndexBackend:
167171
ValueError
168172
The backend could not be guessed from `index_file_name`
169173
"""
170-
ext = Path(index_file_name).suffix.strip(".")
171-
try:
172-
return self.get_instance(ext)
173-
except KeyError as exc:
174-
msg = (
175-
f"Could not guess backend from filename {index_file_name}. "
176-
f"we assumed the extension was {ext}."
177-
)
178-
raise ValueError(msg) from exc
174+
ext = Path(index_file_name).suffix
175+
for _, option_cls in self.options:
176+
option = option_cls()
177+
if ext == option.ext:
178+
return option
179+
180+
known_options_and_extensions = [(v[0], v[1]().ext) for v in self.options]
181+
msg = (
182+
f"Could not guess backend from {index_file_name=!r}. "
183+
"The file's extension does not match any of the available options: "
184+
f"{known_options_and_extensions=}"
185+
)
186+
raise ValueError(msg)
179187

180188

181189
INDEX_BACKENDS = IndexBackendOptions(

tests/integration/database/test_integration_database_portability.py

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,47 @@
88
import pandas as pd
99
import pytest
1010

11-
from pandas_openscm.db import FeatherDataBackend, FeatherIndexBackend, OpenSCMDB
11+
from pandas_openscm.db import (
12+
CSVDataBackend,
13+
CSVIndexBackend,
14+
FeatherDataBackend,
15+
FeatherIndexBackend,
16+
OpenSCMDB,
17+
netCDFDataBackend,
18+
netCDFIndexBackend,
19+
)
1220
from pandas_openscm.testing import assert_frame_alike
1321

22+
pytest.importorskip("filelock")
23+
1424

1525
@pytest.mark.parametrize(
16-
"backend_data_for_class_method, backend_index_for_class_method",
26+
"backend_data, backend_index",
1727
(
1828
pytest.param(
1929
FeatherDataBackend(),
2030
FeatherIndexBackend(),
21-
id="provided",
31+
id="feather",
32+
),
33+
pytest.param(
34+
netCDFDataBackend(),
35+
netCDFIndexBackend(),
36+
id="netCDF",
2237
),
2338
pytest.param(
24-
None,
25-
None,
26-
id="guessed",
39+
CSVDataBackend(),
40+
CSVIndexBackend(),
41+
id="csv",
2742
),
2843
),
2944
)
30-
def test_move_db(
31-
backend_data_for_class_method,
32-
backend_index_for_class_method,
45+
@pytest.mark.parametrize("provide_backend_data_to_class_method", (True, False))
46+
@pytest.mark.parametrize("provide_backend_index_to_class_method", (True, False))
47+
def test_move_db( # noqa: PLR0913
48+
provide_backend_index_to_class_method,
49+
provide_backend_data_to_class_method,
50+
backend_data,
51+
backend_index,
3352
tmpdir,
3453
setup_pandas_accessor,
3554
):
@@ -39,8 +58,8 @@ def test_move_db(
3958

4059
db = OpenSCMDB(
4160
db_dir=initial_db_dir,
42-
backend_data=FeatherDataBackend(),
43-
backend_index=FeatherIndexBackend(),
61+
backend_data=backend_data,
62+
backend_index=backend_index,
4463
)
4564

4665
df_timeseries_like = pd.DataFrame(
@@ -63,19 +82,24 @@ def test_move_db(
6382
tar_archive = db.to_gzipped_tar_archive(tar_archive)
6483

6584
# Expand elsewhere
85+
from_gzipped_tar_archive_kwargs = {}
86+
if provide_backend_data_to_class_method:
87+
from_gzipped_tar_archive_kwargs["backend_data"] = backend_data
88+
89+
if provide_backend_index_to_class_method:
90+
from_gzipped_tar_archive_kwargs["backend_index"] = backend_index
91+
6692
db_other = OpenSCMDB.from_gzipped_tar_archive(
67-
tar_archive,
68-
db_dir=other_db_dir,
69-
backend_data=backend_data_for_class_method,
70-
backend_index=backend_index_for_class_method,
93+
tar_archive, db_dir=other_db_dir, **from_gzipped_tar_archive_kwargs
7194
)
7295

7396
# Delete the original
7497
db.delete()
7598

76-
assert_frame_alike(df_timeseries_like, db_other.load())
99+
assert_frame_alike(df_timeseries_like, db_other.load(out_columns_type=int))
77100

78101
locator = pd.Index(["scenario_b"], name="scenario")
79102
assert_frame_alike(
80-
df_timeseries_like.openscm.mi_loc(locator), db_other.load(locator)
103+
df_timeseries_like.openscm.mi_loc(locator),
104+
db_other.load(locator, out_columns_type=int),
81105
)

0 commit comments

Comments
 (0)