def test_time_periods(self): rule = ReservationRule(start_time=datetime.time(10, 0), end_time=datetime.time(6, 0), days_changed=1, start_days=53, max_inside_border_crossed=0, machine_type=self.machine_type, max_hours=0) time_periods = rule.time_periods() correct_timeperiods = [ rule.Period(0, rule), rule.Period(2, rule), rule.Period(4, rule), rule.Period(5, rule) ] self.assertEqual(len(time_periods), len(correct_timeperiods)) for calculated_period, correct_period in zip(time_periods, correct_timeperiods): self.assertEqual(calculated_period.start_time, correct_period.start_time) self.assertEqual(calculated_period.end_time, correct_period.end_time) self.assertTrue(calculated_period.overlap(correct_period)) self.assertEqual( round( rule.Period.hours_overlap(calculated_period.start_time, calculated_period.end_time, correct_period.start_time, correct_period.end_time), 2), 20)
def clean(self): cleaned_data = super().clean() rule = ReservationRule(machine_type=cleaned_data["machine_type"], max_hours=0, max_inside_border_crossed=0, start_time=cleaned_data["start_time"], end_time=cleaned_data["end_time"], days_changed=cleaned_data["days_changed"], start_days=self.get_start_days(cleaned_data)) rule.is_valid_rule(raise_error=True) return cleaned_data
def test_is_valid_time(self): ReservationRule(start_time=datetime.time(10, 0), end_time=datetime.time(6, 0), days_changed=1, start_days=1, max_inside_border_crossed=5, machine_type=self.machine_type, max_hours=10).save() ReservationRule(start_time=datetime.time(6, 0), end_time=datetime.time(12, 0), days_changed=2, start_days=2, max_inside_border_crossed=16, machine_type=self.machine_type, max_hours=16).save() self.assertTrue( ReservationRule.valid_time(datetime.datetime(2018, 11, 5, 12, 0), datetime.datetime(2018, 11, 5, 18, 0), self.machine_type), "Periods that cover only one rule, should be valid if they are valid in that rule" ) self.assertFalse( ReservationRule.valid_time(datetime.datetime(2018, 11, 5, 12, 0), datetime.datetime(2018, 11, 5, 23, 0), self.machine_type), "Periods that cover only one rule, should be valid if they are valid in that rule" ) self.assertTrue( ReservationRule.valid_time(datetime.datetime(2018, 11, 6, 12, 0), datetime.datetime(2018, 11, 7, 3, 0), self.machine_type), "Periods that cover only one rule, should be valid if they are valid in that rule" ) self.assertFalse( ReservationRule.valid_time(datetime.datetime(2018, 11, 6, 12, 0), datetime.datetime(2018, 11, 7, 18, 0), self.machine_type), "Periods that cover only one rule, should be valid if they are valid in that rule" ) self.assertTrue( ReservationRule.valid_time(datetime.datetime(2018, 11, 6, 3, 0), datetime.datetime(2018, 11, 6, 18, 0), self.machine_type), "A period may still be valid, even though its total duration is larger than what is allowed in" "one of the rules it partially covers") self.assertFalse( ReservationRule.valid_time(datetime.datetime(2018, 11, 6, 1, 0), datetime.datetime(2018, 11, 6, 22, 0), self.machine_type), "A period may be valid in each rule, it can still be invalid due to its total duration" ) self.assertFalse( ReservationRule.valid_time(datetime.datetime(2018, 11, 6, 0, 0), datetime.datetime(2018, 11, 6, 12, 0), self.machine_type), "A period may not be valid in one of the rules is covers")
def create_reservation_rule(self, start_time, end_time): return ReservationRule(start_time=start_time, end_time=end_time, start_days=0, max_hours=0, max_inside_border_crossed=0, days_changed=0, machine_type=self.machine_type)
def test_is_valid_time_in_rule_no_border_cross(self): rule = ReservationRule(start_time=datetime.time(10, 0), end_time=datetime.time(6, 0), days_changed=1, start_days=1, max_inside_border_crossed=5, machine_type=self.machine_type, max_hours=10) self.assertTrue( rule.valid_time_in_rule(datetime.datetime(2018, 11, 5, 10, 12), datetime.datetime(2018, 11, 5, 10, 48), False)) self.assertTrue( rule.valid_time_in_rule(datetime.datetime(2018, 11, 5, 10, 0), datetime.datetime(2018, 11, 5, 20, 0), False)) self.assertTrue( rule.valid_time_in_rule(datetime.datetime(2018, 11, 5, 20, 0), datetime.datetime(2018, 11, 6, 4, 0), False)) self.assertFalse( rule.valid_time_in_rule(datetime.datetime(2018, 11, 5, 10, 0), datetime.datetime(2018, 11, 5, 21, 0), False)) self.assertFalse( rule.valid_time_in_rule(datetime.datetime(2018, 11, 5, 18, 0), datetime.datetime(2018, 11, 6, 6, 0), False))
def test_is_valid_time_max_interval(self): self.assertFalse( ReservationRule.valid_time(datetime.datetime(2018, 11, 5, 0, 0), datetime.datetime(2018, 11, 12, 0, 1), self.machine_type), """ Reservations should not be valid if they are longer than 1 week, as the logic won't work correctly. Reservations can still be longer than 1 week if they are allowed to ignore the rules. """)
def test_is_valid_rule_external(self): ReservationRule(start_time=datetime.time(10, 0), end_time=datetime.time(6, 0), days_changed=1, start_days=53, max_inside_border_crossed=0, machine_type=self.machine_type, max_hours=0).save() rule = ReservationRule(start_time=datetime.time(12, 0), end_time=datetime.time(18, 0), days_changed=0, start_days=66, max_inside_border_crossed=0, machine_type=self.machine_type, max_hours=0) self.assertTrue(rule.is_valid_rule()) rule = ReservationRule(start_time=datetime.time(5, 0), end_time=datetime.time(12, 0), days_changed=0, start_days=2, max_inside_border_crossed=0, machine_type=self.machine_type, max_hours=0) self.assertFalse(rule.is_valid_rule()) try: rule.is_valid_rule(raise_error=True) self.fail( "An exception should have been raised for a check on a non valid rule with raise_error set" ) except ValidationError: pass try: rule.save() self.fail("Valid rules should not be saveable") except ValidationError: pass rule.machine_type = MachineTypeField.get_machine_type(2) self.assertTrue( rule.is_valid_rule(), "Rules for different machine types should not effect the validity of each other" )
def test_is_valid_rule_internal(self): rule = ReservationRule(start_time=datetime.time(10, 0), end_time=datetime.time(6, 0), days_changed=1, start_days=53, max_inside_border_crossed=0, machine_type=self.machine_type, max_hours=0) self.assertTrue( rule.is_valid_rule(), "A rule with a difference of less than 24h between start_time and end_time should not overlap with it self" ) rule = ReservationRule(start_time=datetime.time(10, 0), end_time=datetime.time(12, 0), days_changed=1, start_days=3, max_inside_border_crossed=0, machine_type=self.machine_type, max_hours=0) self.assertFalse( rule.is_valid_rule(), "A rule with periods of more than 24h cannot have two successive days" ) rule = ReservationRule(start_time=datetime.time(10, 0), end_time=datetime.time(12, 0), days_changed=1, start_days=65, max_inside_border_crossed=0, machine_type=self.machine_type, max_hours=0) self.assertFalse( rule.is_valid_rule(), "A rule with periods of more than 24h cannot have two successive days" ) try: rule.is_valid_rule(raise_error=True) self.fail( "An exception should have been raised for a check on a non valid rule with raise_error set" ) except ValidationError: pass rule = ReservationRule(start_time=datetime.time(10, 0), end_time=datetime.time(12, 0), days_changed=7, start_days=1, max_inside_border_crossed=0, machine_type=self.machine_type, max_hours=0) self.assertFalse(rule.is_valid_rule(), "A rule cannot cover more than a week") try: rule.is_valid_rule(raise_error=True) self.fail( "An exception should have been raised for a check on a non valid rule with raise_error set" ) except ValidationError: pass