Skip to content

Commit 80d2ce4

Browse files
Maint: test coverage
1 parent 655f9a3 commit 80d2ce4

7 files changed

Lines changed: 104 additions & 1 deletion

File tree

codecov.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ coverage:
2525

2626
ignore:
2727
- "**/setup.py"
28+
- "skrub/skrub/_sklearn_compat.py" # already tested in https://github.com/sklearn-compat/sklearn-compat

skrub/_gap_encoder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,10 @@ def _init_w(self, V, X):
338338
W2 = W2 + 0.1
339339
W = np.concatenate((W, W2), axis=0)
340340
else:
341-
raise ValueError(f"Initialization method {self.init!r} does not exist. ")
341+
raise ValueError(
342+
f"Initialization method {self.init!r} does not exist. It should be one"
343+
" of ['k-means++', 'random', 'k-means']."
344+
)
342345
W /= W.sum(axis=1, keepdims=True)
343346
A = np.ones((self.n_components, self.n_vocab)) * 1e-10
344347
B = A.copy()

skrub/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,3 +236,8 @@ def use_fit_transform(request):
236236
manually.
237237
"""
238238
return request.param
239+
240+
241+
xfail_with_download_error = pytest.mark.xfail(
242+
raises=OSError, match="Can't download the file '.*' from urls.*"
243+
)

skrub/datasets/tests/test_fetching.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
from pandas.testing import assert_frame_equal, assert_series_equal
77

88
import skrub.datasets
9+
from skrub.conftest import xfail_with_download_error
910
from skrub.datasets import _fetching, _utils
1011

1112

1213
def _get_table_names_from_bunch(bunch):
1314
return [k for k in bunch if isinstance(bunch[k], pd.DataFrame)]
1415

1516

17+
@xfail_with_download_error
1618
@pytest.mark.parametrize("dataset_name", ["employee_salaries", "drug_directory"])
1719
def test_fetching(monkeypatch, dataset_name):
1820
with TemporaryDirectory() as temp_dir:
@@ -39,6 +41,7 @@ def _error_on_get(*args, **kwargs):
3941
assert bunch["metadata"] == local_bunch["metadata"]
4042

4143

44+
@xfail_with_download_error
4245
def test_fetch_credit_fraud():
4346
data = skrub.datasets.fetch_credit_fraud()
4447
assert data.baskets.shape == (61241, 2)
@@ -52,6 +55,7 @@ def test_fetch_credit_fraud():
5255
skrub.datasets.fetch_credit_fraud(split=None)
5356

5457

58+
@xfail_with_download_error
5559
def test_fetch_employee_salaries():
5660
data = skrub.datasets.fetch_employee_salaries()
5761
assert data.employee_salaries.shape == (9228, 9)
@@ -65,6 +69,48 @@ def test_fetch_employee_salaries():
6569
skrub.datasets.fetch_employee_salaries(split=None)
6670

6771

72+
@xfail_with_download_error
73+
@pytest.mark.parametrize(
74+
"dataset_name, shape",
75+
[
76+
("medical_charge", (163065, 12)),
77+
("midwest_survey", (2494, 29)),
78+
("open_payments", (73558, 6)),
79+
("traffic_violations", (1578154, 43)),
80+
("toxicity", (1000, 2)),
81+
("videogame_sales", (16572, 11)),
82+
("bike_sharing", (17379, 11)),
83+
],
84+
)
85+
def test_datasets_without_splitting(dataset_name, shape):
86+
"Test datasets that do not have a split argument in their fetching function."
87+
data = getattr(_fetching, f"fetch_{dataset_name}")()
88+
assert data[dataset_name].shape == shape
89+
90+
91+
@xfail_with_download_error
92+
@pytest.mark.parametrize(
93+
"dataset_name, keys",
94+
[
95+
("flight_delays", ["flights", "airports", "weather", "stations", "metadata"]),
96+
(
97+
"country_happiness",
98+
[
99+
"happiness_report",
100+
"happiness_report",
101+
"life_expectancy",
102+
"legal_rights_index",
103+
],
104+
),
105+
("movielens", ["movies", "ratings", "metadata"]),
106+
],
107+
)
108+
def test_fetching_several_tables(dataset_name, keys):
109+
"Test fetching functions that return several tables."
110+
data = getattr(_fetching, f"fetch_{dataset_name}")()
111+
assert all(key in data.keys() for key in keys)
112+
113+
68114
def test_fetching_wrong_checksum(monkeypatch):
69115
dataset_info = _utils.DATASET_INFO["employee_salaries"]
70116
monkeypatch.setitem(dataset_info, "sha256", "bad_checksum")

skrub/tests/test_gap_encoder.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,27 @@ def test_empty_column_name(df_module):
306306
s = df_module.make_column("", ["one", "two"] * 10)
307307
out = GapEncoder(n_components=3, random_state=0).fit_transform(s)
308308
assert sbd.column_names(out) == ["one, two", "two, one", "one, two (2)"]
309+
310+
311+
def test_non_supported_analyzer(generate_data):
312+
n_samples = 70
313+
X = generate_data(n_samples, random_state=0)
314+
gap_encoder = GapEncoder(analyzer="unsupported")
315+
with pytest.raises(
316+
ValueError, match=r"analyzer should be one of \['word', 'char', 'char_wb']\."
317+
):
318+
gap_encoder.fit(X)
319+
320+
321+
def test_non_supported_init(generate_data):
322+
n_samples = 70
323+
X = generate_data(n_samples, random_state=0)
324+
gap_encoder = GapEncoder(init="unsupported")
325+
with pytest.raises(
326+
ValueError,
327+
match=(
328+
r"Initialization method 'unsupported' does not exist\. It should be one of"
329+
r" \['k-means\++', 'random', 'k-means']\."
330+
),
331+
):
332+
gap_encoder.fit(X)

skrub/tests/test_minhash_encoder.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,14 @@ def test_zero_padding_in_feature_names_out(df_module, n_components, expected_col
287287
feature_names = encoder.get_feature_names_out()
288288

289289
assert feature_names[: len(expected_columns)] == expected_columns
290+
291+
292+
def test_col_not_categorical(df_module):
293+
X1 = df_module.make_column("some col", [1, 2, 3, 4, 5, 6, 7, 8])
294+
X2 = df_module.make_column("col", ["a", "b", "c", "d", "e", "f", "g", "h"])
295+
encoder = MinHashEncoder()
296+
with pytest.raises(ValueError, match="does not contain strings"):
297+
encoder.fit_transform(X1)
298+
299+
with pytest.raises(ValueError, match="does not contain strings"):
300+
encoder.fit(X2).transform(X1)

skrub/tests/test_to_datetime.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,16 @@ def test_error_dispatch(func):
234234
# Make codecov happy
235235
with pytest.raises(TypeError, match="Expecting a Pandas or Polars Series"):
236236
func(np.array([1]))
237+
238+
239+
def test_specific_time_encoding():
240+
"""Test another specific time zone encoding case.
241+
Not using df_module fixture because it's not intended to test polars.
242+
"""
243+
from zoneinfo import ZoneInfo
244+
245+
col = [
246+
pd.Timestamp(1584226800, unit="s", tz=ZoneInfo("Europe/Paris")),
247+
pd.Timestamp(1584226801, unit="s", tz=ZoneInfo("Europe/Paris")),
248+
]
249+
assert _get_time_zone(pd.Series(name="dt", data=col)) == "Europe/Paris"

0 commit comments

Comments
 (0)