class LongUnchangedTests(Base): """Unit tests for the 'status long unchanged' notification strategy.""" def setUp(self): """Override to set up a metric fixture.""" self.old_timestamp = "2019-02-01T00:00:00+00:00" self.new_timestamp = "2020-02-01T00:00:00+00:00" self.notification_finder = NotificationFinder(self.data_model) self.red_metric = self.metric( status="target_not_met", recent_measurements=[ dict(start=self.old_timestamp, end=self.new_timestamp) ], status_start=self.old_timestamp, ) def test_long_unchanged_status(self): """Test that a metric is notable if its status has been the same for 3 weeks.""" now = datetime.now() self.red_metric["status_start"] = str(now - timedelta(days=21, hours=1)) self.assertEqual( "status_long_unchanged", self.notification_finder.get_notification(self.red_metric, "red_metric", now)) def test_long_unchanged_status_already_notified(self): """Test that a metric is not notable if a notification has already been generated for the long status.""" now = datetime.now() self.red_metric["status_start"] = str(now - timedelta(days=21, hours=1)) self.notification_finder.already_notified.append("red_metric") self.assertEqual( None, self.notification_finder.get_notification(self.red_metric, "red_metric", now)) def test_too_long_unchanged_status(self): """Test that a metric isn't notable if its status has been the same for more than 3 weeks.""" now = datetime.now() self.red_metric["status_start"] = str(now - timedelta(days=100)) self.assertEqual( None, self.notification_finder.get_notification(self.red_metric, "red_metric", now)) def test_short_unchanged_status(self): """Test that a metric isn't notable if its current status has changed in the last 3 weeks.""" now = datetime.now() self.red_metric["status_start"] = str(now - timedelta(days=20, hours=23)) self.assertEqual( None, self.notification_finder.get_notification(self.red_metric, "red_metric", now))
class CheckIfMetricIsNotableTestCase(Base): """Unit tests for the check_if_metric_is_notable method.""" def setUp(self): """Set variables for the tests.""" self.old_timestamp = "2019-03-01T00:00:00+00:00" self.new_timestamp = "2020-03-01T00:00:00+00:00" self.metric_uuid = "metric_uuid" self.red_metric = self.metric( status="target_not_met", recent_measurements=[], status_start=str(datetime.fromisoformat(datetime.min.isoformat())), ) self.notification_finder = NotificationFinder(self.data_model) self.metric_with_recent_measurements = self.metric( recent_measurements=[ dict(start=self.old_timestamp, end=self.old_timestamp, count=dict(status="target_not_met", value="10")), dict( start=self.old_timestamp, end=self.new_timestamp, count=dict( status="target_not_met", value="0", status_start=self.old_timestamp, ), ), ], status_start=self.old_timestamp, ) def test_metric_not_notable_if_no_recent_measurements(self): """Test that a metric is not notable if it does not contain any recent measurements.""" self.assertIsNone( self.notification_finder.get_notification( self.red_metric, self.metric_uuid, datetime.min.replace(tzinfo=timezone.utc))) def test_metric_is_notable_because_status_changed(self): """Test that a metric is notable because the status changed.""" metric = self.metric(recent_measurements=[ dict(start=self.old_timestamp, end=self.old_timestamp, count=dict(status="target_not_met", value="10")), dict(start=self.old_timestamp, end=self.new_timestamp, count=dict(status="target_met", value="0")), ]) self.assertEqual( "status_changed", self.notification_finder.get_notification( metric, self.metric_uuid, datetime.min.replace(tzinfo=timezone.utc)), ) def test_metric_already_notified_is_removed_because_status_changed(self): """Test that the metric is removed from already notified when the status changes.""" metric = self.metric(recent_measurements=[ dict(start=self.old_timestamp, end=self.old_timestamp, count=dict(status="target_not_met", value="10")), dict(start=self.old_timestamp, end=self.new_timestamp, count=dict(status="target_met", value="0")), ]) self.notification_finder.already_notified.append(self.metric_uuid) self.notification_finder.get_notification( metric, self.metric_uuid, datetime.min.replace(tzinfo=timezone.utc)) self.assertEqual(self.notification_finder.already_notified, []) def test_metric_is_notable_because_status_unchanged_3weeks(self): """Test that a metric is notable for long unchanged status.""" self.assertEqual( "status_long_unchanged", self.notification_finder.get_notification( self.metric_with_recent_measurements, self.metric_uuid, datetime.fromisoformat(self.old_timestamp) + timedelta(days=21, hours=12), ), ) def test_metric_not_notable_if_already_notified(self): """Test that a metric isn't notable for long unchanged status if a notification has already been generated.""" self.notification_finder.already_notified.append(self.metric_uuid) self.assertIsNone( self.notification_finder.get_notification( self.metric_with_recent_measurements, self.metric_uuid, datetime.fromisoformat(self.old_timestamp) + timedelta(days=21, hours=12), ))