Skip to content

Commit

Permalink
fixed all tests, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Ran Isenberg committed Nov 18, 2022
1 parent c24118c commit 4b75c98
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def _evaluate_conditions(
cond_value = condition.get(schema.CONDITION_VALUE)

# rule based actions have no user context. the context is the condition key
if cond_action == schema.RuleAction.TIME_RANGE.value or schema.RuleAction.TIME_SELECTED_DAYS:
if cond_action in [schema.RuleAction.TIME_RANGE.value, schema.RuleAction.TIME_SELECTED_DAYS.value]:
context_value = condition.get(schema.CONDITION_KEY)

if not self._match_by_action(action=cond_action, condition_value=cond_value, context_value=context_value):
Expand Down
25 changes: 20 additions & 5 deletions aws_lambda_powertools/utilities/feature_flags/time_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,22 @@
HOUR_MIN_SEPARATOR = ":"


DAY_MAPPING = {
1: TimeValues.MONDAY.value,
2: TimeValues.TUESDAY.value,
3: TimeValues.WEDNESDAY.value,
4: TimeValues.THURSDAY.value,
5: TimeValues.FRIDAY.value,
6: TimeValues.SATURDAY.value,
7: TimeValues.SUNDAY.value,
}


def time_range_compare(action: str, values: Dict) -> bool:
if action == TimeKeys.CURRENT_TIME_UTC.value:
return _time_range_compare_current_time_utc(values)
elif action == TimeKeys.CURRENT_HOUR_UTC.value:
return _time_range_compare_current_time_utc(values)
return _time_range_compare_current_hour_utc(values)
# we assume it passed validation right? so no need to raise an error
return False

Expand All @@ -22,20 +33,24 @@ def time_selected_days_compare(action: str, values: List[str]) -> bool:
return False


def _get_utc_time_now() -> datetime:
return datetime.now(timezone.utc)


def _time_selected_days_current_days_compare(values: List[str]) -> bool:
current_day: datetime = datetime.now(timezone.utc).strftime('%A').lower()
return current_day in values
current_day_number = _get_utc_time_now().isoweekday()
return DAY_MAPPING.get(current_day_number, "") in values


def _time_range_compare_current_time_utc(values: Dict) -> bool:
current_time_utc: datetime = datetime.now(timezone.utc)
current_time_utc: datetime = _get_utc_time_now()
start_date = datetime.strptime(values.get(TimeValues.START_TIME, ""), "%Y-%m-%dT%H:%M:%S%z")
end_date = datetime.strptime(values.get(TimeValues.END_TIME, ""), "%Y-%m-%dT%H:%M:%S%z")
return current_time_utc >= start_date and current_time_utc <= end_date


def _time_range_compare_current_hour_utc(values: Dict) -> bool:
current_time_utc: datetime = datetime.now(timezone.utc)
current_time_utc: datetime = _get_utc_time_now()
start_hour, start_min = values.get(TimeValues.START_TIME, HOUR_MIN_SEPARATOR).split(HOUR_MIN_SEPARATOR)
end_hour, end_min = values.get(TimeValues.END_TIME, HOUR_MIN_SEPARATOR).split(HOUR_MIN_SEPARATOR)
return (
Expand Down

0 comments on commit 4b75c98

Please sign in to comment.