Skip to content

Commit

Permalink
Merge pull request #116 from eoyilmaz/106-convert-working-hours-to-di…
Browse files Browse the repository at this point in the history
…ctstr-tupletupleint-int

106 convert working hours to dictstr tupletupleint int
  • Loading branch information
eoyilmaz authored Nov 19, 2024
2 parents d1e2f0c + bb41373 commit 13c9185
Show file tree
Hide file tree
Showing 4 changed files with 206 additions and 381 deletions.
38 changes: 17 additions & 21 deletions src/stalker/models/studio.py
Original file line number Diff line number Diff line change
Expand Up @@ -783,21 +783,21 @@ def _validate_working_hours(self, key: str, working_hours: Dict[str, List]) -> d
)
)

for key in working_hours.keys():
if not isinstance(working_hours[key], list):
for day in working_hours:
if not isinstance(working_hours[day], list):
raise TypeError(
'{}.working_hours should be a dictionary with keys "mon, '
'tue, wed, thu, fri, sat, sun" and the values should a '
"list of lists of two integers like [[540, 720], [800, "
"1080]], not {}: '{}'".format(
self.__class__.__name__,
working_hours[key].__class__.__name__,
working_hours[key],
working_hours[day].__class__.__name__,
working_hours[day],
)
)

# validate item values
self._validate_working_hours_value(working_hours[key])
self._validate_working_hours_value(working_hours[day])

# update the default values with the supplied working_hour dictionary
# copy the defaults
Expand Down Expand Up @@ -868,35 +868,31 @@ def _validate_working_hours_value(self, value: List) -> List:
Returns:
List[List[int, int]]
"""
err1 = (
err = (
"{}.working_hours value should be a list of lists of two "
"integers between and the range of integers should be 0-1440, "
"not {}"
"integers and the range of integers should be between 0-1440, "
"not {}: '{}'".format(self.__class__.__name__, value.__class__.__name__, value)
)
err2 = f"{err1}: '{{}}'"

if not isinstance(value, list):
raise TypeError(
err2.format(self.__class__.__name__, value.__class__.__name__, value)
)
raise TypeError(err)

for i in value:
if not isinstance(i, list):
raise TypeError(
err2.format(self.__class__.__name__, i.__class__.__name__, i)
)
raise TypeError(err)

# check list length
if len(i) != 2:
raise RuntimeError(err1.format(self.__class__.__name__, value))
raise ValueError(err)

# check type
if not isinstance(i[0], int) or not isinstance(i[1], int):
raise TypeError(err1.format(self.__class__.__name__, value))
for j in range(2):
if not isinstance(i[j], int):
raise TypeError(err)

# check range
if i[0] < 0 or i[0] > 1440 or i[1] < 0 or i[1] > 1440:
raise ValueError(err1.format(self.__class__.__name__, value))
# check range
if i[j] < 0 or i[j] > 1440:
raise ValueError(err)

return value

Expand Down
13 changes: 13 additions & 0 deletions tests/config/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,16 @@ def test_update_with_studio_is_working_as_expected(setup_postgresql_db):
def test_old_style_repo_env_does_not_exist_anymore():
"""repo_env_var_template_old doesn't exist anymore."""
assert "repo_env_var_template_old" not in defaults.config_values


def test_default_working_hours_is_a_dictionary_with_list_values():
"""default working_hours is a list of lists of two integers."""
assert isinstance(defaults.working_hours, dict)
assert all(
day in defaults.working_hours
for day in ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
)
assert all(
isinstance(defaults.working_hours[day], list)
for day in ["mon", "tue", "wed", "thu", "fri", "sat", "sun"]
)
22 changes: 14 additions & 8 deletions tests/models/test_taskJuggler_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -638,17 +638,23 @@ def test_tasks_of_given_projects_are_correctly_scheduled(

assert data["test_task1"].computed_start is None
assert data["test_task1"].computed_end is None
assert data["test_task1"].computed_resources == [
data["test_user1"],
data["test_user2"],
]
assert sorted(data["test_task1"].computed_resources, key=lambda x: x.id) == sorted(
[
data["test_user1"],
data["test_user2"],
],
key=lambda x: x.id,
)

assert data["test_task2"].computed_start is None
assert data["test_task2"].computed_end is None
assert data["test_task2"].computed_resources == [
data["test_user1"],
data["test_user2"],
]
assert sorted(data["test_task2"].computed_resources, key=lambda x: x.id) == sorted(
[
data["test_user1"],
data["test_user2"],
],
key=lambda x: x.id,
)

assert dt1.computed_start == datetime.datetime(2013, 4, 16, 9, 0, tzinfo=pytz.utc)
assert dt1.computed_end == datetime.datetime(2013, 4, 16, 13, 0, tzinfo=pytz.utc)
Expand Down
Loading

0 comments on commit 13c9185

Please sign in to comment.