Skip to content

Commit

Permalink
first test :)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hussein-Mahfouz committed Apr 30, 2024
1 parent 89bcc83 commit e51a04e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/acbm/assigning.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ def _map_day_to_wkday_binary(day: int) -> int:
# if day in range 1: 5, it is a weekday
if day in [1, 2, 3, 4, 5]:
return 1
return 0
elif day in [6, 7]:
return 0
else:
# if day is not in the range 1: 7, raise an error
raise ValueError("Day should be numeric and in the range 1-7")


# function to filter travel_times df specifically for pt mode
Expand Down
34 changes: 34 additions & 0 deletions tests/test_assigning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import pandas as pd
import pytest

from acbm.assigning import _map_day_to_wkday_binary

# applying to a single value


@pytest.mark.parametrize(
("day", "expected"), [(1, 1), (2, 1), (3, 1), (4, 1), (5, 1), (6, 0), (7, 0)]
)
def test_map_day_to_wkday_binary_valid(day, expected):
assert _map_day_to_wkday_binary(day) == expected


def test_map_day_to_wkday_binary_invalid():
with pytest.raises(ValueError, match="Day should be numeric and in the range 1-7"):
_map_day_to_wkday_binary(8)


# applying to a df


def test_map_day_to_wkday_binary_df():
df = pd.DataFrame({"day": [1, 2, 3, 4, 5, 6, 7]})
df["wkday"] = df["day"].apply(_map_day_to_wkday_binary)
assert df["wkday"].tolist() == [1, 1, 1, 1, 1, 0, 0]


# test applying the function to a df column with an invalid value
def test_map_day_to_wkday_binary_df_invalid():
df = pd.DataFrame({"day": [1, 2, 3, 4, 5, 6, 7, 8]})
with pytest.raises(ValueError, match="Day should be numeric and in the range 1-7"):
df["wkday"] = df["day"].apply(_map_day_to_wkday_binary)

0 comments on commit e51a04e

Please sign in to comment.