def has_day_in_notification_days(self, day: Day) -> bool: try: return bool(day.value & self.notification_days) except AttributeError: error = EnumError(except_enum=Day, wrong_value=day) log.warning(error.__str__()) return False
def has_hour_in_notification_hours(self, hour: Hour) -> bool: try: return bool(hour.value & self.notification_hours) except AttributeError: error = EnumError(except_enum=Hour, wrong_value=hour) log.warning(error.__str__()) return False
def test__has_day_in_notification_days(self): to_have_day = Day.MONDAY not_to_have_day = Day.TUESDAY self.notification_days = [to_have_day] alert_notification = self.get_alert_notification() self.assertTrue( alert_notification.has_day_in_notification_days(to_have_day)) self.assertFalse( alert_notification.has_day_in_notification_days(not_to_have_day)) # Test in Multiples Flags another_day_to_have = Day.SATURDAY self.notification_days = [to_have_day, another_day_to_have] alert_notification = self.get_alert_notification() self.assertTrue( alert_notification.has_day_in_notification_days(day=to_have_day)) self.assertFalse( alert_notification.has_day_in_notification_days( day=not_to_have_day)) self.assertTrue( alert_notification.has_day_in_notification_days( day=another_day_to_have)) # ERROR : Not a Day wrong_day = "iam no day" error = EnumError(except_enum=Day, wrong_value=wrong_day) with patch("logging.warning") as mock: self.assertFalse( alert_notification.has_day_in_notification_days(day=wrong_day)) mock.assert_called_with(error.__str__())
def __init__(self, operator: str, comparator: str, data_period_type: str, data_period_quantity: int, data_period_unit: str, value_type: str, value_number: float, value_period_type: str, hour_start: int, hour_end: int, acceptable_diff: bool, last_check: datetime, today: datetime): print(self.__class__.__name__, " in creation ...") self.__last_check = last_check self.__today = today print(data_period_unit, type(data_period_unit)) self.__acceptable_diff = acceptable_diff # OPERATOR try: self.__operator = MyOperator[operator] except KeyError: raise EnumError(MyOperator, wrong_value=operator) # COMPARATOR try: self.__comparator = MyComparator[comparator] except ValueError: raise EnumError(MyComparator, wrong_value=comparator, where=self.__class__.__name__) # ALERT DATA self.__alert_data = AlertData( data_period_type=data_period_type, data_period_quantity=data_period_quantity, data_period_unit=data_period_unit, hour_start=hour_start, hour_end=hour_end, last_check=last_check, today=today) # ALERT VALUE self.__alert_value = AlertValue(value_number=value_number, value_type=value_type) self.check_non_coherent_config(value_type=value_type, value_period_type=value_period_type) self.__handle_alert_value_generator( today=today, value_period_type=value_period_type, data_period_unit=data_period_unit, data_period_quantity=data_period_quantity, operator=self.__operator)
def __generate_period_definition(self, unit, quantity: int): if not isinstance(unit, PeriodUnitDefinition): try: unit = PeriodUnitDefinition(unit) except ValueError: raise EnumError(except_enum=PeriodUnitDefinition, wrong_value=unit) self.__period_definition = PeriodDefinition(unit=unit, quantity=quantity)
def __init__(self, value_type: str, value_number: float): print(self.__class__.__name__, " in creation ...") try: self.__value_generator_type = ValueGeneratorType[value_type] except KeyError: raise EnumError(ValueGeneratorType, wrong_value=value_type, where=self.__class__.__name__) self.__value_number = value_number
def test__has_hour_in_notification_hours(self): to_have_hour = Hour.H_1 not_to_have_hour = Hour.H_2 self.notification_hours = [to_have_hour] alert_notification = self.get_alert_notification() self.assertTrue( alert_notification.has_hour_in_notification_hours(to_have_hour)) self.assertFalse( alert_notification.has_hour_in_notification_hours( not_to_have_hour)) # Test in Multiples Flags another_hour_to_have = Hour.H_3 self.notification_hours = [to_have_hour, another_hour_to_have] alert_notification = self.get_alert_notification() self.assertTrue( alert_notification.has_hour_in_notification_hours( hour=to_have_hour)) self.assertFalse( alert_notification.has_hour_in_notification_hours( hour=not_to_have_hour)) self.assertTrue( alert_notification.has_hour_in_notification_hours( hour=another_hour_to_have)) # ERROR : Not a Hour wrong_hour = "iam no hour" error = EnumError(except_enum=Hour, wrong_value=wrong_hour) with patch("logging.warning") as mock: self.assertFalse( alert_notification.has_hour_in_notification_hours( hour=wrong_hour)) mock.assert_called_with(error.__str__())
def check_non_coherent_config(self, value_type, value_period_type): try: vt = ValueGeneratorType[value_type] except KeyError: raise EnumError(ValueGeneratorType, wrong_value=value_type) if vt is ValueGeneratorType.PERIOD_BASED_VALUE and not value_period_type: raise ConfigError( self.__alert_value, "Since 'value_type' is '{}' 'value_period_type' can not be null" .format(ValueGeneratorType.PERIOD_BASED_VALUE.name)) if self.acceptable_diff and vt is ValueGeneratorType.USER_BASED_VALUE: raise ConfigError( self, "acceptable_diff and ValueGeneratorType.USER_BASED_VALUE not compatible" )
def __init__(self, data_period_type: str, data_period_quantity: int, data_period_unit: str, hour_start: int, hour_end: int, last_check: datetime, today: datetime): print(self.__class__.__name__, " in creation ...") self.__hour_start = hour_start self.__hour_end = hour_end try: self.__data_period_type = PeriodGeneratorType[data_period_type] except KeyError: raise EnumError(except_enum=PeriodGeneratorType, wrong_value=data_period_type, where=self.__class__.__name__) self.set_period_generator(last_check=last_check, today=today, data_period_quantity=data_period_quantity, data_period_unit=data_period_unit)