Skip to content

Commit

Permalink
Fix test detail
Browse files Browse the repository at this point in the history
  • Loading branch information
jason810496 committed Jan 11, 2025
1 parent 22f8012 commit 26d0d71
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions tests/api_fastapi/core_api/routes/public/test_dag_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.
from __future__ import annotations

import os
from unittest.mock import patch

import pytest
Expand All @@ -27,8 +28,8 @@

pytestmark = pytest.mark.db_test

TEST_DAG_FOLDER = "/opt/airflow/tests/dags"
TEST_DAG_FOLDER_WITH_SUBDIR = "/opt/airflow/tests/dags/subdir2"
TEST_DAG_FOLDER = os.environ["AIRFLOW__CORE__DAGS_FOLDER"]
TEST_DAG_FOLDER_WITH_SUBDIR = f"{TEST_DAG_FOLDER}/subdir2"
TEST_DAG_FOLDER_INVALID = "/invalid/path"
TEST_DAG_FOLDER_INVALID_2 = "/root/airflow/tests/dags/"

Expand Down Expand Up @@ -79,21 +80,18 @@ def clear_db(self):
)
def test_should_response_200(self, test_client, subdir, include_example, expected_total_entries):
with conf_vars({("core", "load_examples"): str(include_example)}):
response = test_client.get("/public/dagReport", params={"subdir": subdir})
response = test_client.get("/public/dagReports", params={"subdir": subdir})
assert response.status_code == 200
response_json = response.json()
expected_keys = {"dag_num", "dags", "duration", "file", "task_num", "warning_num"}
assert response_json["total_entries"] == expected_total_entries
for dag_report in response_json["dag_reports"]:
assert all(key in dag_report for key in expected_keys)

def test_should_response_200_with_empty_dagbag(self, test_client):
# the constructor of DagBag will call `collect_dags` method and store the result in `dagbag_stats`
def _mock_collect_dags(self, *args, **kwargs):
self.dagbag_stats = []

with patch("airflow.models.dagbag.DagBag.collect_dags", _mock_collect_dags):
response = test_client.get("/public/dagReport", params={"subdir": TEST_DAG_FOLDER})
response = test_client.get("/public/dagReports", params={"subdir": TEST_DAG_FOLDER})
assert response.status_code == 200
assert response.json() == {"dag_reports": [], "total_entries": 0}

Expand All @@ -105,12 +103,12 @@ def _mock_collect_dags(self, *args, **kwargs):
],
)
def test_should_response_400(self, test_client, subdir):
response = test_client.get("/public/dagReport", params={"subdir": subdir})
response = test_client.get("/public/dagReports", params={"subdir": subdir})
assert response.status_code == 400
assert response.json() == {"detail": "subdir should be subpath of DAGS_FOLDER settings"}

def test_should_response_422(self, test_client):
response = test_client.get("/public/dagReport")
response = test_client.get("/public/dagReports")
assert response.status_code == 422
assert response.json() == {
"detail": [
Expand Down

0 comments on commit 26d0d71

Please sign in to comment.