Skip to content

Commit add19e9

Browse files
Sarra NebliSarra99
authored andcommitted
Fixing code coverage
1 parent 35d9300 commit add19e9

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

tests/functions/test_adorn_percentages.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,3 +149,37 @@ def test_adorn_percentages_with_ns_all():
149149
# Should have more than one column (including percentages and raw counts)
150150
assert "(" in result.iloc[0, 1]
151151
# Check that raw counts are included
152+
153+
154+
@pytest.mark.functions
155+
def test_adorn_percentages_empty_pivot():
156+
"""
157+
Test that adorn_percentages returns an empty DataFrame if the pivot is empty.
158+
"""
159+
# DataFrame sans colonnes valides pour le pivot
160+
data = {"NonExistentColumn": [], "AnotherColumn": [], "Value": []}
161+
df = pd.DataFrame(data)
162+
163+
# Appel de la fonction avec des colonnes inexistantes
164+
result = adorn_percentages(df, "NonExistentColumn", "AnotherColumn")
165+
166+
# Vérifie que le résultat est un DataFrame vide
167+
assert result.empty, "Expected an empty DataFrame when pivot is empty."
168+
169+
170+
@pytest.mark.functions
171+
def test_adorn_percentages_invalid_axis():
172+
"""
173+
Test that adorn_percentages raises a ValueError for an invalid axis argument.
174+
"""
175+
data = {
176+
"Category": ["A", "B"],
177+
"Subcategory": ["X", "Y"],
178+
"Value": [10, 20],
179+
}
180+
df = pd.DataFrame(data)
181+
182+
with pytest.raises(
183+
ValueError, match="The 'axis' argument must be 'row', 'col', or 'all'."
184+
):
185+
adorn_percentages(df, "Category", "Subcategory", axis="invalid")

tests/functions/test_tabyl.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,81 @@ def test_tabyl_with_percentages_all():
179179
assert (
180180
result_numeric.select_dtypes(include=["float", "int"]).max().max() <= 1
181181
)
182+
183+
184+
@pytest.mark.functions
185+
def test_tabyl_missing_col1():
186+
"""
187+
Test that tabyl raises an error if col1 is missing from the DataFrame.
188+
"""
189+
data = {"Category": ["A", "B"], "Subcategory": ["X", "Y"]}
190+
df = pd.DataFrame(data)
191+
192+
with pytest.raises(
193+
ValueError, match="Column 'Region' is not in the DataFrame."
194+
):
195+
tabyl(df, "Region")
196+
197+
198+
@pytest.mark.functions
199+
def test_tabyl_missing_col2():
200+
"""
201+
Test that tabyl raises an error if col2 is missing from the DataFrame.
202+
"""
203+
data = {"Category": ["A", "B"], "Subcategory": ["X", "Y"]}
204+
df = pd.DataFrame(data)
205+
206+
with pytest.raises(
207+
ValueError, match="Column 'Value' is not in the DataFrame."
208+
):
209+
tabyl(df, "Category", "Value")
210+
211+
212+
@pytest.mark.functions
213+
def test_tabyl_missing_col3():
214+
"""
215+
Test that tabyl raises an error if col3 is missing from the DataFrame.
216+
"""
217+
data = {"Category": ["A", "B"], "Subcategory": ["X", "Y"]}
218+
df = pd.DataFrame(data)
219+
220+
with pytest.raises(
221+
ValueError, match="Column 'Region' is not in the DataFrame."
222+
):
223+
tabyl(df, "Category", "Subcategory", "Region")
224+
225+
226+
@pytest.mark.functions
227+
def test_tabyl_single_column():
228+
"""
229+
Test that tabyl works correctly with only col1 specified.
230+
"""
231+
data = {"Category": ["A", "B", "A", "C", "B", "A", "C"]}
232+
df = pd.DataFrame(data)
233+
234+
result = tabyl(df, "Category")
235+
assert result.shape[0] == 3 # Three unique values in 'Category'
236+
assert (
237+
result["count"].sum() == 7
238+
) # Total count should match the number of rows
239+
240+
241+
@pytest.mark.functions
242+
def test_tabyl_invalid_percentage_axis():
243+
"""
244+
Test that tabyl raises an error for invalid percentage_axis values.
245+
"""
246+
data = {"Category": ["A", "B"], "Subcategory": ["X", "Y"]}
247+
df = pd.DataFrame(data)
248+
249+
with pytest.raises(
250+
ValueError,
251+
match="`percentage_axis` must be one of 'row', 'col', or 'all'.",
252+
):
253+
tabyl(
254+
df,
255+
"Category",
256+
"Subcategory",
257+
show_percentages=True,
258+
percentage_axis="invalid",
259+
)

0 commit comments

Comments
 (0)