Skip to content

Commit

Permalink
Fixing code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sarra Nebli authored and Sarra99 committed Jan 28, 2025
1 parent 35d9300 commit add19e9
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
34 changes: 34 additions & 0 deletions tests/functions/test_adorn_percentages.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,37 @@ def test_adorn_percentages_with_ns_all():
# Should have more than one column (including percentages and raw counts)
assert "(" in result.iloc[0, 1]
# Check that raw counts are included


@pytest.mark.functions
def test_adorn_percentages_empty_pivot():
"""
Test that adorn_percentages returns an empty DataFrame if the pivot is empty.
"""
# DataFrame sans colonnes valides pour le pivot
data = {"NonExistentColumn": [], "AnotherColumn": [], "Value": []}
df = pd.DataFrame(data)

# Appel de la fonction avec des colonnes inexistantes
result = adorn_percentages(df, "NonExistentColumn", "AnotherColumn")

# Vérifie que le résultat est un DataFrame vide
assert result.empty, "Expected an empty DataFrame when pivot is empty."


@pytest.mark.functions
def test_adorn_percentages_invalid_axis():
"""
Test that adorn_percentages raises a ValueError for an invalid axis argument.
"""
data = {
"Category": ["A", "B"],
"Subcategory": ["X", "Y"],
"Value": [10, 20],
}
df = pd.DataFrame(data)

with pytest.raises(
ValueError, match="The 'axis' argument must be 'row', 'col', or 'all'."
):
adorn_percentages(df, "Category", "Subcategory", axis="invalid")
78 changes: 78 additions & 0 deletions tests/functions/test_tabyl.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,81 @@ def test_tabyl_with_percentages_all():
assert (
result_numeric.select_dtypes(include=["float", "int"]).max().max() <= 1
)


@pytest.mark.functions
def test_tabyl_missing_col1():
"""
Test that tabyl raises an error if col1 is missing from the DataFrame.
"""
data = {"Category": ["A", "B"], "Subcategory": ["X", "Y"]}
df = pd.DataFrame(data)

with pytest.raises(
ValueError, match="Column 'Region' is not in the DataFrame."
):
tabyl(df, "Region")


@pytest.mark.functions
def test_tabyl_missing_col2():
"""
Test that tabyl raises an error if col2 is missing from the DataFrame.
"""
data = {"Category": ["A", "B"], "Subcategory": ["X", "Y"]}
df = pd.DataFrame(data)

with pytest.raises(
ValueError, match="Column 'Value' is not in the DataFrame."
):
tabyl(df, "Category", "Value")


@pytest.mark.functions
def test_tabyl_missing_col3():
"""
Test that tabyl raises an error if col3 is missing from the DataFrame.
"""
data = {"Category": ["A", "B"], "Subcategory": ["X", "Y"]}
df = pd.DataFrame(data)

with pytest.raises(
ValueError, match="Column 'Region' is not in the DataFrame."
):
tabyl(df, "Category", "Subcategory", "Region")


@pytest.mark.functions
def test_tabyl_single_column():
"""
Test that tabyl works correctly with only col1 specified.
"""
data = {"Category": ["A", "B", "A", "C", "B", "A", "C"]}
df = pd.DataFrame(data)

result = tabyl(df, "Category")
assert result.shape[0] == 3 # Three unique values in 'Category'
assert (
result["count"].sum() == 7
) # Total count should match the number of rows


@pytest.mark.functions
def test_tabyl_invalid_percentage_axis():
"""
Test that tabyl raises an error for invalid percentage_axis values.
"""
data = {"Category": ["A", "B"], "Subcategory": ["X", "Y"]}
df = pd.DataFrame(data)

with pytest.raises(
ValueError,
match="`percentage_axis` must be one of 'row', 'col', or 'all'.",
):
tabyl(
df,
"Category",
"Subcategory",
show_percentages=True,
percentage_axis="invalid",
)

0 comments on commit add19e9

Please sign in to comment.