pierrejeambrun commented on code in PR #45802:
URL: https://github.com/apache/airflow/pull/45802#discussion_r1925062726
##########
airflow/api_fastapi/core_api/routes/public/task_instances.py:
##########
@@ -135,9 +135,16 @@ def get_mapped_task_instances(
"map_index",
"try_number",
"logical_date",
+ "data_interval_start",
+ "data_interval_end",
"rendered_map_index",
],
TI,
+ to_replace={
+ "logical_date": DagRun.logical_date,
Review Comment:
logical_data has an `association_proxy` directly on the TaskInstance model.
So virtually `TaskInstance.logical_date` exist, you don't need to alias it.
##########
tests/api_fastapi/core_api/routes/public/test_task_instances.py:
##########
@@ -1034,38 +1034,67 @@ def test_should_respond_200_for_dag_id_filter(self,
test_client, session):
def test_should_respond_200_for_order_by(self, test_client, session):
dag_id = "example_python_operator"
+
+ dag_runs = [
+ DagRun(
+ dag_id=dag_id,
+ run_id=f"run_{i}",
+ run_type=DagRunType.MANUAL,
+ logical_date=DEFAULT_DATETIME_1 + dt.timedelta(days=i),
+ data_interval=(
+ DEFAULT_DATETIME_1 + dt.timedelta(days=i),
+ DEFAULT_DATETIME_1 + dt.timedelta(days=i, hours=1),
+ ),
+ )
+ for i in range(10)
+ ]
+ session.add_all(dag_runs)
+ session.commit()
+
self.create_task_instances(
session,
task_instances=[
- {"start_date": DEFAULT_DATETIME_1 + dt.timedelta(minutes=(i +
1))} for i in range(10)
+ {"run_id": f"run_{i}", "start_date": DEFAULT_DATETIME_1 +
dt.timedelta(minutes=(i + 1))}
+ for i in range(10)
],
dag_id=dag_id,
)
ti_count = session.query(TaskInstance).filter(TaskInstance.dag_id ==
dag_id).count()
- # Ascending order
- response_asc = test_client.get(
- "/public/dags/~/dagRuns/~/taskInstances", params={"order_by":
"start_date"}
- )
- assert response_asc.status_code == 200
- assert response_asc.json()["total_entries"] == ti_count
- assert len(response_asc.json()["task_instances"]) == ti_count
-
- # Descending order
- response_desc = test_client.get(
- "/public/dags/~/dagRuns/~/taskInstances", params={"order_by":
"-start_date"}
- )
- assert response_desc.status_code == 200
- assert response_desc.json()["total_entries"] == ti_count
- assert len(response_desc.json()["task_instances"]) == ti_count
+ for field in ["start_date", "logical_date", "data_interval_start",
"data_interval_end"]:
+ # Ascending order
Review Comment:
This should be @pytest.mark.parametrized, to avoid extra indent and for
loop. Pytest will do that for us. Also when one test fails it's easier to
understand what sorting field cause the errors. Here it's burried in the loop.
##########
tests/api_fastapi/core_api/routes/public/test_task_instances.py:
##########
@@ -1034,38 +1034,67 @@ def test_should_respond_200_for_dag_id_filter(self,
test_client, session):
def test_should_respond_200_for_order_by(self, test_client, session):
dag_id = "example_python_operator"
+
+ dag_runs = [
+ DagRun(
+ dag_id=dag_id,
+ run_id=f"run_{i}",
+ run_type=DagRunType.MANUAL,
+ logical_date=DEFAULT_DATETIME_1 + dt.timedelta(days=i),
+ data_interval=(
+ DEFAULT_DATETIME_1 + dt.timedelta(days=i),
+ DEFAULT_DATETIME_1 + dt.timedelta(days=i, hours=1),
+ ),
+ )
+ for i in range(10)
+ ]
+ session.add_all(dag_runs)
+ session.commit()
+
self.create_task_instances(
session,
task_instances=[
- {"start_date": DEFAULT_DATETIME_1 + dt.timedelta(minutes=(i +
1))} for i in range(10)
+ {"run_id": f"run_{i}", "start_date": DEFAULT_DATETIME_1 +
dt.timedelta(minutes=(i + 1))}
+ for i in range(10)
],
dag_id=dag_id,
)
ti_count = session.query(TaskInstance).filter(TaskInstance.dag_id ==
dag_id).count()
- # Ascending order
- response_asc = test_client.get(
- "/public/dags/~/dagRuns/~/taskInstances", params={"order_by":
"start_date"}
- )
- assert response_asc.status_code == 200
- assert response_asc.json()["total_entries"] == ti_count
- assert len(response_asc.json()["task_instances"]) == ti_count
-
- # Descending order
- response_desc = test_client.get(
- "/public/dags/~/dagRuns/~/taskInstances", params={"order_by":
"-start_date"}
- )
- assert response_desc.status_code == 200
- assert response_desc.json()["total_entries"] == ti_count
- assert len(response_desc.json()["task_instances"]) == ti_count
+ for field in ["start_date", "logical_date", "data_interval_start",
"data_interval_end"]:
+ # Ascending order
+ response_asc = test_client.get(
+ "/public/dags/~/dagRuns/~/taskInstances", params={"order_by":
field}
+ )
+ assert response_asc.status_code == 200
+ assert response_asc.json()["total_entries"] == ti_count
+ assert len(response_asc.json()["task_instances"]) == ti_count
- # Compare
- start_dates_asc = [ti["start_date"] for ti in
response_asc.json()["task_instances"]]
- assert len(start_dates_asc) == ti_count
- start_dates_desc = [ti["start_date"] for ti in
response_desc.json()["task_instances"]]
- assert len(start_dates_desc) == ti_count
- assert start_dates_asc == list(reversed(start_dates_desc))
+ # Descending order
+ response_desc = test_client.get(
+ "/public/dags/~/dagRuns/~/taskInstances", params={"order_by":
f"-{field}"}
+ )
+ assert response_desc.status_code == 200
+ assert response_desc.json()["total_entries"] == ti_count
+ assert len(response_desc.json()["task_instances"]) == ti_count
+
+ if field in ["start_date", "logical_date"]:
+ # Compare
+ field_asc = [ti[field] for ti in
response_asc.json()["task_instances"]]
+ assert len(field_asc) == ti_count
+ field_desc = [ti[field] for ti in
response_desc.json()["task_instances"]]
+ assert len(field_desc) == ti_count
+ assert field_asc == list(reversed(field_desc))
+
+ # Separate condition since these attributes are not returned by
the api response, so using ti["id"] to compare
+ elif field in ["data_interval_start", "data_interval_end"]:
+ # Compare
+ field_asc = [ti["id"] for ti in
response_asc.json()["task_instances"]]
+ assert len(field_asc) == ti_count
+ field_desc = [ti["id"] for ti in
response_desc.json()["task_instances"]]
+ assert len(field_desc) == ti_count
+ assert field_asc == list(reversed(field_desc))
Review Comment:
I think you can simplify things and do that also for `start_date` and
`logical_date` so you do not different set of assertions on the `field`
##########
airflow/api_fastapi/core_api/routes/public/task_instances.py:
##########
@@ -419,6 +433,7 @@ def get_task_instances(
limit=limit,
session=session,
)
+ print(task_instance_select)
Review Comment:
needs to be removed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]