def test_it_only_returns_true_when_handling_many(self, mock): """ This test ensures that only the Command's handle many will return True when handling many checks """ # Assest that it returns False when handling one # create single check check = Check(user=self.alice, status="up") # Set the check a day and after the initial ping it will be in the grace period check.last_ping = timezone.now() - timedelta(days=1, minutes=45) check.save() # Assert that result is false result = Command().handle_many() self.assertFalse(result) # Create a list of checks names = ["Check %d" % i for i in range(1, 26)] # Set the time which each check will alert after overdue = timezone.now() - timedelta(days=1) # Initialize the checks list with names and the overdue alert_after for name in names: check = Check(user=self.alice, name=name, status="up") check.alert_after = overdue check.save() # Assert that result of Command's handle_many is True result = Command().handle_many() self.assertTrue(result)
def test_it_handles_grace_period(self): check = Check(user=self.alice, status="up") # 1 day 30 minutes after ping the check is in grace period: check.last_ping = timezone.now() - timedelta(days=1, minutes=30) check.save() # Expect no exceptions-- Command().handle_one(check)
def test_it_handles_grace_period(self): check = Check(user=self.alice, status="up") # 1 day 30 minutes after ping the check is in grace period: check.last_ping = now() - timedelta(days=1, minutes=30) check.alert_after = check.get_alert_after() check.save() Command().handle_going_down() self.assertEqual(Flip.objects.count(), 0)
def test_it_handles_grace_period(self): check = Check(user=self.alice, status="up") # 1 day 30 minutes after ping the check is in grace period: check.last_ping = timezone.now() - timedelta(days=1, minutes=30) check.save() # Expect no exceptions-- result = Command().handle_one(check) ### Assert when Command's handle many that when handle_many should return True self.assertEqual(result, True)
def test_it_handles_grace_period(self): check = Check(project=self.project, status="up") # 1 day 30 minutes after ping the check is in grace period: check.last_ping = now() - td(days=1, minutes=30) check.alert_after = check.last_ping + td(days=1, hours=1) check.save() Command().handle_going_down() check.refresh_from_db() self.assertEqual(check.status, "up") self.assertEqual(Flip.objects.count(), 0)
def test_it_handles_many(self, mock): yesterday = timezone.now() - timedelta(days=1) names = ["Check %d" % d for d in range(0, 100)] for name in names: check = Check(user=self.alice, name=name) check.alert_after = yesterday check.status = "up" check.save() result = Command().handle_many() self.assertTrue(result)
def test_that_handle_only_returns_true_if_it_handles_many(self, mock): # Handle one check = Check(user=self.alice, status="up") # 1 day 30 minutes after ping the check is in grace period: check.last_ping = timezone.now() - timedelta(days=1, minutes=30) check.save() # Expect no exceptions-- result = Command().handle_many() self.assertFalse(result) # Handle many yesterday = timezone.now() - timedelta(days=1) names = ["Check %d" % d for d in range(0, 10)] for name in names: check = Check(user=self.alice, name=name) check.alert_after = yesterday check.status = "up" check.save() result = Command().handle_many() self.assertTrue(result)
def test_it_notifies_when_check_goes_down(self, mock_notify): check = Check(user=self.alice, status="up") check.last_ping = timezone.now() - timedelta(days=2) check.alert_after = check.get_alert_after() check.save() result = Command().handle_one() # If it finds work, it should return True self.assertTrue(result) # It should change stored status to "down" check.refresh_from_db() self.assertEqual(check.status, "down") # It should call `notify_on_thread` self.assertTrue(mock_notify.called)
def test_it_updates_alert_after(self, mock_notify): check = Check(user=self.alice, status="up") check.last_ping = now() - timedelta(hours=1) check.alert_after = check.last_ping check.save() result = Command().handle_going_down() # If it finds work, it should return True self.assertTrue(result) # alert_after should have been increased check.refresh_from_db() self.assertTrue(check.alert_after > check.last_ping) # a flip should have not been created self.assertEqual(Flip.objects.count(), 0)
def test_it_handles_few(self, mock): yesterday = timezone.now() - timedelta(days=1) names = ["Check %d" % d for d in range(0,10)] for name in names: check = Check(user=self.alice, name=name) check.alert_after = yesterday check.status = "up" check.save() result = Command().handle_many() self.assertEqual(result, True) handled_names = [] for args, kwargs in mock.call_args_list: handled_names.append(args[0].name) self.assertEqual(len(names), len(handled_names))
def test_it_updates_alert_after(self, mock_notify): check = Check(user=self.alice, status="up") check.last_ping = timezone.now() - timedelta(hours=1) check.alert_after = check.last_ping check.save() result = Command().handle_one() # If it finds work, it should return True self.assertTrue(result) # It should change stored status to "down" check.refresh_from_db() # alert_after should have been increased self.assertTrue(check.alert_after > check.last_ping) # notify_on_thread should *not* have been called self.assertFalse(mock_notify.called)
def test_it_creates_a_flip_when_check_goes_down(self): check = Check(user=self.alice, status="up") check.last_ping = now() - timedelta(days=2) check.alert_after = check.get_alert_after() check.save() result = Command().handle_going_down() # If it finds work, it should return True self.assertTrue(result) # It should create a flip object flip = Flip.objects.get() self.assertEqual(flip.owner_id, check.id) self.assertEqual(flip.new_status, "down") # It should change stored status to "down" check.refresh_from_db() self.assertEqual(check.status, "down")
def test_it_handles_few(self, mock): yesterday = timezone.now() - timedelta(days=1) names = ["Check %d" % d for d in range(0, 10)] for name in names: check = Check(user=self.alice, name=name) check.alert_after = yesterday check.status = "up" check.save() ### Assert when Command's handle many that when handle_many should return True result = Command().handle_many() assert result, "handle_many should return True" handled_names = [] for args, kwargs in mock.call_args_list: handled_names.append(args[0].name) assert set(names) == set(handled_names)
def test_it_handles_few(self, mock): alice = User(username="******") alice.save() names = ["Check %d" % d for d in range(0, 10)] for name in names: check = Check(user=alice, name=name) check.alert_after = datetime(2000, 1, 1) check.status = "up" check.save() result = Command().handle_many() assert result, "handle_many should return True" handled_names = [] for args, kwargs in mock.call_args_list: handled_names.append(args[0].name) assert set(names) == set(handled_names)
def test_it_creates_a_flip_when_check_goes_down(self): check = Check(project=self.project, status="up") check.last_ping = now() - td(days=2) check.alert_after = check.last_ping + td(days=1, hours=1) check.save() result = Command().handle_going_down() # If it finds work, it should return True self.assertTrue(result) # It should create a flip object flip = Flip.objects.get() self.assertEqual(flip.owner_id, check.id) self.assertEqual(flip.created, check.alert_after) self.assertEqual(flip.new_status, "down") # It should change stored status to "down", and clear out alert_after check.refresh_from_db() self.assertEqual(check.status, "down") self.assertEqual(check.alert_after, None)
def test_it_processes_flip(self, mock_notify): check = Check(project=self.project, status="up") check.last_ping = now() check.alert_after = check.last_ping + td(days=1, hours=1) check.save() flip = Flip(owner=check, created=check.last_ping) flip.old_status = "down" flip.new_status = "up" flip.save() result = Command().process_one_flip() # If it finds work, it should return True self.assertTrue(result) # It should set the processed date flip.refresh_from_db() self.assertTrue(flip.processed) # It should call `notify_on_thread` self.assertTrue(mock_notify.called)