|
| 1 | +from collections import Counter |
| 2 | +from datetime import timedelta |
| 3 | +from unittest.mock import patch |
| 4 | + |
| 5 | +from orchestra.bots.sanitybot import create_and_handle_sanity_checks |
| 6 | +from orchestra.models import SanityCheck |
| 7 | +from orchestra.tests.helpers import OrchestraTestCase |
| 8 | +from orchestra.tests.helpers.fixtures import setup_models |
| 9 | +from orchestra.utils.task_lifecycle import create_subsequent_tasks |
| 10 | + |
| 11 | + |
| 12 | +class SanityBotTestCase(OrchestraTestCase): |
| 13 | + |
| 14 | + def setUp(self): |
| 15 | + super().setUp() |
| 16 | + setup_models(self) |
| 17 | + |
| 18 | + @patch('orchestra.bots.sanitybot.message_experts_slack_group') |
| 19 | + def test_create_and_handle_sanity_checks(self, mock_slack): |
| 20 | + # Initialize relevant project. |
| 21 | + create_subsequent_tasks(self.projects['sanitybot']) |
| 22 | + # Initialize irrelevant project. |
| 23 | + create_subsequent_tasks(self.projects['test_human_and_machine']) |
| 24 | + |
| 25 | + # No sanity checks exist, and Slack hasn't received a message. |
| 26 | + sanity_checks = SanityCheck.objects.all() |
| 27 | + self.assertEqual(sanity_checks.count(), 0) |
| 28 | + self.assertEqual(mock_slack.call_count, 0) |
| 29 | + |
| 30 | + create_and_handle_sanity_checks() |
| 31 | + # Of the four sanity checks for this project, three were |
| 32 | + # triggered by orchestra.tests.helpers.workflow.check_project. |
| 33 | + sanity_checks = SanityCheck.objects.all() |
| 34 | + self.assertEqual( |
| 35 | + dict(Counter(sanity_checks.values_list('check_slug', flat=True))), |
| 36 | + { |
| 37 | + 'frequently_repeating_check': 1, |
| 38 | + 'infrequently_repeating_check': 1, |
| 39 | + 'onetime_check': 1 |
| 40 | + }) |
| 41 | + for sanity_check in sanity_checks: |
| 42 | + self.assertEqual( |
| 43 | + sanity_check.project.workflow_version.slug, |
| 44 | + 'sanitybot_workflow') |
| 45 | + # Three slack messages for three sanity checks. |
| 46 | + self.assertEqual(mock_slack.call_count, 3) |
| 47 | + |
| 48 | + # Too little time has passed, so we expect no new sanity checks. |
| 49 | + create_and_handle_sanity_checks() |
| 50 | + # Still only three sanity check exists for the relevant project. |
| 51 | + sanity_checks = SanityCheck.objects.all() |
| 52 | + self.assertEqual(sanity_checks.count(), 3) |
| 53 | + # Slack didn't get called again. |
| 54 | + self.assertEqual(mock_slack.call_count, 3) |
| 55 | + |
| 56 | + # Mark the sanity checks as having happened two days ago. Only |
| 57 | + # the `frequently_repeating_check` should trigger. |
| 58 | + for sanity_check in sanity_checks: |
| 59 | + sanity_check.created_at = ( |
| 60 | + sanity_check.created_at - timedelta(days=2)) |
| 61 | + sanity_check.save() |
| 62 | + create_and_handle_sanity_checks() |
| 63 | + # Look for the three old sanity checks and a new |
| 64 | + # `frequently_repeating_check`. |
| 65 | + sanity_checks = SanityCheck.objects.all() |
| 66 | + self.assertEqual( |
| 67 | + dict(Counter(sanity_checks.values_list('check_slug', flat=True))), |
| 68 | + { |
| 69 | + 'frequently_repeating_check': 2, |
| 70 | + 'infrequently_repeating_check': 1, |
| 71 | + 'onetime_check': 1 |
| 72 | + }) |
| 73 | + for sanity_check in sanity_checks: |
| 74 | + self.assertEqual( |
| 75 | + sanity_check.project.workflow_version.slug, |
| 76 | + 'sanitybot_workflow') |
| 77 | + # Slack got called another time. |
| 78 | + self.assertEqual(mock_slack.call_count, 4) |
| 79 | + |
| 80 | + create_and_handle_sanity_checks() |
| 81 | + # Still only four sanity checks exists for the relevant project. |
| 82 | + sanity_checks = SanityCheck.objects.all() |
| 83 | + self.assertEqual(sanity_checks.count(), 4) |
| 84 | + # Slack didn't get called again. |
| 85 | + self.assertEqual(mock_slack.call_count, 4) |
0 commit comments