Skip to content

Commit

Permalink
Add unit test about the trigger_on_each_run behavior #1325
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Jul 25, 2024
1 parent fe1b5b1 commit b6ecf4b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
4 changes: 4 additions & 0 deletions scanpipe/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,6 +1207,10 @@ def test_scanpipe_run_model_append_to_log(self):
def test_scanpipe_run_model_deliver_project_subscriptions(self, mock_deliver):
self.project1.add_webhook_subscription(target_url="https://localhost")
run1 = self.create_run()

run1.deliver_project_subscriptions(has_next_run=True)
mock_deliver.assert_not_called()

run1.deliver_project_subscriptions()
mock_deliver.assert_called_once_with(pipeline_run=run1)

Expand Down
35 changes: 35 additions & 0 deletions scanpipe/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,38 @@ def test_scanpipe_tasks_execute_pipeline_no_run_next_on_failure(self, mock_execu
self.assertEqual("", run2.task_output)
self.assertIsNone(run2.task_start_date)
self.assertIsNone(run2.task_end_date)

@mock.patch("requests.post")
@mock.patch("scanpipe.pipelines.Pipeline.execute")
def test_scanpipe_tasks_execute_pipeline_task_subscriptions(
self, mock_execute, mock_post
):
project = Project.objects.create(name="my_project")
project.add_webhook_subscription(target_url="https://localhost")
run_p1 = project.add_pipeline("do_nothing")
project.add_pipeline("do_nothing")

mock_post.return_value = mock.Mock(status_code=200, text="Ok")

mock_execute.return_value = 0, ""
tasks.execute_pipeline_task(run_p1.pk)
self.assertEqual(2, mock_execute.call_count)
# By default, trigger_on_each_run is False
self.assertEqual(1, mock_post.call_count)
self.assertEqual(1, project.webhookdeliveries.count())

project2 = Project.objects.create(name="my_project2")
project2.add_webhook_subscription(
target_url="https://localhost",
trigger_on_each_run=True,
)
run_p2 = project2.add_pipeline("do_nothing")
project2.add_pipeline("do_nothing")

mock_post.reset_mock()
mock_execute.reset_mock()
tasks.execute_pipeline_task(run_p2.pk)
self.assertEqual(2, mock_execute.call_count)
# This time trigger_on_each_run is True
self.assertEqual(2, mock_post.call_count)
self.assertEqual(2, project2.webhookdeliveries.count())

0 comments on commit b6ecf4b

Please sign in to comment.