Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Commit

Permalink
to fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DonHaul committed Jul 10, 2024
1 parent 3076a14 commit 803336f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .envs/local/.django
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ OPENSEARCH_HOST=opensearch:9200
OPENSEARCH_INDEX_PREFIX=backoffice-backend-local

# Airflow
AIRFLOW_BASE_URL=http://host.docker.internal:8082
AIRFLOW_BASE_URL=http://host.docker.internal:8080
AIRFLOW_TOKEN=CHANGE_ME
1 change: 1 addition & 0 deletions backoffice/workflows/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ def restart(self, request, pk=None):
)
if response.status_code != 204:
return Response({"error": "Failed to restart"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
return Response({"error": "Failed to restart"}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)


class WorkflowDocumentView(BaseDocumentViewSet):
Expand Down
42 changes: 29 additions & 13 deletions backoffice/workflows/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import contextmanager
from unittest.mock import patch

from django.apps import apps
Expand Down Expand Up @@ -273,22 +274,17 @@ def test_reject_author(self, mock_post):

self.assertEqual(response.status_code, 200)

@patch("backoffice.workflows.airflow_utils.requests.post")
def test_restart_full_dagrun(self, mock_post):

mock_response = mock_post.return_value
mock_response.status_code = status.HTTP_200_OK
mock_response.json.return_value = {"key": "value"}

self.api_client.force_authenticate(user=self.curator)
url = reverse(
"api:workflows-authors-restart",
kwargs={"pk": self.workflow.id},
)
with patch_requests() as (mock_post, mock_get, mock_delete):
response = self.api_client.post(url)

response = self.api_client.post(url)

self.assertEqual(response.status_code, 200)
self.assertEqual(response.status_code, 200)

@patch("backoffice.workflows.airflow_utils.requests.post")
def test_restart_a_task(self, mock_post):
Expand All @@ -302,9 +298,9 @@ def test_restart_a_task(self, mock_post):
"api:workflows-authors-restart",
kwargs={"pk": self.workflow.id},
)

response = self.api_client.post(url, json={"task_ids": ["set_workflow_status_to_running"]})
self.assertEqual(response.status_code, 200)
with patch_requests() as (mock_post, mock_get, mock_delete):
response = self.api_client.post(url, json={"task_ids": ["set_workflow_status_to_running"]})
self.assertEqual(response.status_code, 200)

@patch("backoffice.workflows.airflow_utils.requests.post")
def test_restart_with_params(self, mock_post):
Expand All @@ -317,6 +313,26 @@ def test_restart_with_params(self, mock_post):
"api:workflows-authors-restart",
kwargs={"pk": self.workflow.id},
)
with patch_requests() as (mock_post, mock_get, mock_delete):
response = self.api_client.post(url, json={"params": {"workflow_id": self.workflow.id}})
self.assertEqual(response.status_code, 200)

response = self.api_client.post(url, json={"params": {"workflow_id": self.workflow.id}})
self.assertEqual(response.status_code, 200)

@contextmanager
def patch_requests():
with patch("requests.post") as mock_post, patch("requests.get") as mock_get, patch(
"requests.delete"
) as mock_delete:

# Configure the mock for requests.post
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {"key": "value"}

# Configure the mock for requests.get
mock_get.return_value.status_code = 200
mock_get.return_value.json.return_value = {"data": "some_data"}

# Configure the mock for requests.delete
mock_delete.return_value.status_code = 204

yield mock_post, mock_get, mock_delete

0 comments on commit 803336f

Please sign in to comment.