uranusjr commented on code in PR #77190:
URL: https://github.com/apache/airflow/pull/77190#discussion_r4704614167
##########
airflow-core/tests/unit/api_fastapi/execution_api/test_task_sdk_client_contract.py:
##########
@@ -245,12 +252,111 @@ def test_get(self, task_sdk_client, asset, by):
result = task_sdk_client.asset_state_store.get("watermark", **{by:
getattr(asset, by)})
assert result == AssetStateStoreResponse(value="2026-04-29")
- def test_get_not_found(self, task_sdk_client, asset):
- result = task_sdk_client.asset_state_store.get("missing",
name=asset.name)
- assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": "missing"})
+ @pytest.mark.parametrize("key", ["missing", "stored_null"])
+ def test_get_not_found(self, task_sdk_client, asset, key):
+ result = task_sdk_client.asset_state_store.get(key, name=asset.name)
+ assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": key})
@pytest.mark.parametrize("by", [{"name": "missing"}, {"uri":
"s3://bucket/missing"}])
def test_get_missing_asset(self, task_sdk_client, by):
"""An asset that doesn't exist reads the same as a missing key."""
result = task_sdk_client.asset_state_store.get("watermark", **by)
assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": "watermark"})
+
+
+class TestTaskInstanceOperations:
+ @pytest.mark.parametrize("state", [TaskInstanceState.RUNNING,
TaskInstanceState.RESTARTING])
+ def test_start_running_elsewhere(self, task_sdk_client,
create_task_instance, session, state):
+ ti = create_task_instance(state=state, hostname="other-host", pid=1,
session=session)
+ session.commit()
+ with pytest.raises(TaskAlreadyRunningError):
+ task_sdk_client.task_instances.start(ti.id, 2, timezone.utcnow())
+
+ def test_start_finished(self, task_sdk_client, create_task_instance,
session):
+ ti = create_task_instance(state=TaskInstanceState.SUCCESS,
session=session)
+ session.commit()
+ with pytest.raises(ServerResponseError):
+ task_sdk_client.task_instances.start(ti.id, 1, timezone.utcnow())
+
+
+class TestDagRunOperations:
+ @pytest.fixture(autouse=True)
+ def dag(self, dag_maker, session):
+ with dag_maker(dag_id="target", session=session, serialized=True):
+ EmptyOperator(task_id="task")
+ session.commit()
+
+ def test_trigger_existing_run(self, task_sdk_client):
+ assert task_sdk_client.dag_runs.trigger("target", "runid") ==
OKResponse(ok=True)
+ result = task_sdk_client.dag_runs.trigger("target", "runid")
+ assert result == ErrorResponse(error=ErrorType.DAGRUN_ALREADY_EXISTS)
+
+ def test_trigger_existing_run_with_reset(self, task_sdk_client):
+ task_sdk_client.dag_runs.trigger("target", "runid")
+ result = task_sdk_client.dag_runs.trigger("target", "runid",
reset_dag_run=True)
+ assert result == OKResponse(ok=True)
+
+
+class TestAssetOperations:
+ @pytest.fixture(autouse=True)
+ def asset(self, session):
+ clear_db_assets()
+ asset = AssetModel(name="asset", uri="s3://bucket/asset",
group="asset")
+ session.add(asset)
+ session.flush()
+ session.add(AssetActive.for_asset(asset))
+ session.commit()
+ yield asset
+ clear_db_assets()
+
+ @pytest.mark.parametrize("by", ["name", "uri"])
+ def test_get(self, task_sdk_client, asset, by):
+ result = task_sdk_client.assets.get(**{by: getattr(asset, by)})
+ assert result == AssetResponse(name="asset", uri="s3://bucket/asset",
group="asset", extra={})
+
+ @pytest.mark.parametrize("by", [{"name": "missing"}, {"uri":
"s3://bucket/missing"}])
+ def test_get_not_found(self, task_sdk_client, by):
+ result = task_sdk_client.assets.get(**by)
+ assert result == ErrorResponse(error=ErrorType.ASSET_NOT_FOUND,
detail=by)
+
+
+def _find_methods_handling_server_errors() -> set[str]:
+ tree = ast.parse(Path(client_module.__file__).read_text())
+ classes = {node.name: node for node in tree.body if isinstance(node,
ast.ClassDef)}
+ properties = {
+ node.returns.id: node.name
+ for node in classes["Client"].body
+ if isinstance(node, ast.FunctionDef)
+ and isinstance(node.returns, ast.Name)
+ and node.returns.id.endswith("Operations")
+ }
Review Comment:
This does not pick up `HITLOperations`. A future `except` there could go
unnoticed. Keying by return type also drops a class if two properties return
the same type.
##########
airflow-core/tests/unit/api_fastapi/execution_api/test_task_sdk_client_contract.py:
##########
@@ -245,12 +252,111 @@ def test_get(self, task_sdk_client, asset, by):
result = task_sdk_client.asset_state_store.get("watermark", **{by:
getattr(asset, by)})
assert result == AssetStateStoreResponse(value="2026-04-29")
- def test_get_not_found(self, task_sdk_client, asset):
- result = task_sdk_client.asset_state_store.get("missing",
name=asset.name)
- assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": "missing"})
+ @pytest.mark.parametrize("key", ["missing", "stored_null"])
+ def test_get_not_found(self, task_sdk_client, asset, key):
+ result = task_sdk_client.asset_state_store.get(key, name=asset.name)
+ assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": key})
@pytest.mark.parametrize("by", [{"name": "missing"}, {"uri":
"s3://bucket/missing"}])
def test_get_missing_asset(self, task_sdk_client, by):
"""An asset that doesn't exist reads the same as a missing key."""
result = task_sdk_client.asset_state_store.get("watermark", **by)
assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": "watermark"})
+
+
+class TestTaskInstanceOperations:
+ @pytest.mark.parametrize("state", [TaskInstanceState.RUNNING,
TaskInstanceState.RESTARTING])
+ def test_start_running_elsewhere(self, task_sdk_client,
create_task_instance, session, state):
+ ti = create_task_instance(state=state, hostname="other-host", pid=1,
session=session)
+ session.commit()
+ with pytest.raises(TaskAlreadyRunningError):
+ task_sdk_client.task_instances.start(ti.id, 2, timezone.utcnow())
+
+ def test_start_finished(self, task_sdk_client, create_task_instance,
session):
+ ti = create_task_instance(state=TaskInstanceState.SUCCESS,
session=session)
+ session.commit()
+ with pytest.raises(ServerResponseError):
+ task_sdk_client.task_instances.start(ti.id, 1, timezone.utcnow())
+
+
+class TestDagRunOperations:
+ @pytest.fixture(autouse=True)
+ def dag(self, dag_maker, session):
Review Comment:
```suggestion
def _create_dag(self, dag_maker, session):
```
Since this an autouse that does not return anything.
##########
airflow-core/tests/unit/api_fastapi/execution_api/test_task_sdk_client_contract.py:
##########
@@ -245,12 +252,111 @@ def test_get(self, task_sdk_client, asset, by):
result = task_sdk_client.asset_state_store.get("watermark", **{by:
getattr(asset, by)})
assert result == AssetStateStoreResponse(value="2026-04-29")
- def test_get_not_found(self, task_sdk_client, asset):
- result = task_sdk_client.asset_state_store.get("missing",
name=asset.name)
- assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": "missing"})
+ @pytest.mark.parametrize("key", ["missing", "stored_null"])
+ def test_get_not_found(self, task_sdk_client, asset, key):
+ result = task_sdk_client.asset_state_store.get(key, name=asset.name)
+ assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": key})
@pytest.mark.parametrize("by", [{"name": "missing"}, {"uri":
"s3://bucket/missing"}])
def test_get_missing_asset(self, task_sdk_client, by):
"""An asset that doesn't exist reads the same as a missing key."""
result = task_sdk_client.asset_state_store.get("watermark", **by)
assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": "watermark"})
+
+
+class TestTaskInstanceOperations:
+ @pytest.mark.parametrize("state", [TaskInstanceState.RUNNING,
TaskInstanceState.RESTARTING])
+ def test_start_running_elsewhere(self, task_sdk_client,
create_task_instance, session, state):
+ ti = create_task_instance(state=state, hostname="other-host", pid=1,
session=session)
+ session.commit()
+ with pytest.raises(TaskAlreadyRunningError):
+ task_sdk_client.task_instances.start(ti.id, 2, timezone.utcnow())
+
+ def test_start_finished(self, task_sdk_client, create_task_instance,
session):
+ ti = create_task_instance(state=TaskInstanceState.SUCCESS,
session=session)
+ session.commit()
+ with pytest.raises(ServerResponseError):
+ task_sdk_client.task_instances.start(ti.id, 1, timezone.utcnow())
+
+
+class TestDagRunOperations:
+ @pytest.fixture(autouse=True)
+ def dag(self, dag_maker, session):
+ with dag_maker(dag_id="target", session=session, serialized=True):
+ EmptyOperator(task_id="task")
+ session.commit()
+
+ def test_trigger_existing_run(self, task_sdk_client):
+ assert task_sdk_client.dag_runs.trigger("target", "runid") ==
OKResponse(ok=True)
+ result = task_sdk_client.dag_runs.trigger("target", "runid")
+ assert result == ErrorResponse(error=ErrorType.DAGRUN_ALREADY_EXISTS)
+
+ def test_trigger_existing_run_with_reset(self, task_sdk_client):
+ task_sdk_client.dag_runs.trigger("target", "runid")
+ result = task_sdk_client.dag_runs.trigger("target", "runid",
reset_dag_run=True)
+ assert result == OKResponse(ok=True)
+
+
+class TestAssetOperations:
+ @pytest.fixture(autouse=True)
+ def asset(self, session):
+ clear_db_assets()
+ asset = AssetModel(name="asset", uri="s3://bucket/asset",
group="asset")
+ session.add(asset)
+ session.flush()
+ session.add(AssetActive.for_asset(asset))
+ session.commit()
+ yield asset
+ clear_db_assets()
+
+ @pytest.mark.parametrize("by", ["name", "uri"])
+ def test_get(self, task_sdk_client, asset, by):
+ result = task_sdk_client.assets.get(**{by: getattr(asset, by)})
+ assert result == AssetResponse(name="asset", uri="s3://bucket/asset",
group="asset", extra={})
+
+ @pytest.mark.parametrize("by", [{"name": "missing"}, {"uri":
"s3://bucket/missing"}])
+ def test_get_not_found(self, task_sdk_client, by):
+ result = task_sdk_client.assets.get(**by)
+ assert result == ErrorResponse(error=ErrorType.ASSET_NOT_FOUND,
detail=by)
+
+
+def _find_methods_handling_server_errors() -> set[str]:
+ tree = ast.parse(Path(client_module.__file__).read_text())
+ classes = {node.name: node for node in tree.body if isinstance(node,
ast.ClassDef)}
+ properties = {
+ node.returns.id: node.name
+ for node in classes["Client"].body
+ if isinstance(node, ast.FunctionDef)
+ and isinstance(node.returns, ast.Name)
+ and node.returns.id.endswith("Operations")
+ }
+ return {
+ f"{prop}.{method.name}"
+ for cls_name, prop in properties.items()
+ for method in classes[cls_name].body
+ if isinstance(method, ast.FunctionDef)
+ and any(
+ isinstance(handler, ast.ExceptHandler)
+ and isinstance(handler.type, ast.Name)
Review Comment:
`handler.type` is only matched as `ast.Name`. `except (ServerResponseError,
X)` (a `Tuple`) and `except api.client.ServerResponseError` (an `Attribute`)
are missed.
##########
airflow-core/tests/unit/api_fastapi/execution_api/test_task_sdk_client_contract.py:
##########
@@ -245,12 +252,111 @@ def test_get(self, task_sdk_client, asset, by):
result = task_sdk_client.asset_state_store.get("watermark", **{by:
getattr(asset, by)})
assert result == AssetStateStoreResponse(value="2026-04-29")
- def test_get_not_found(self, task_sdk_client, asset):
- result = task_sdk_client.asset_state_store.get("missing",
name=asset.name)
- assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": "missing"})
+ @pytest.mark.parametrize("key", ["missing", "stored_null"])
+ def test_get_not_found(self, task_sdk_client, asset, key):
+ result = task_sdk_client.asset_state_store.get(key, name=asset.name)
+ assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": key})
@pytest.mark.parametrize("by", [{"name": "missing"}, {"uri":
"s3://bucket/missing"}])
def test_get_missing_asset(self, task_sdk_client, by):
"""An asset that doesn't exist reads the same as a missing key."""
result = task_sdk_client.asset_state_store.get("watermark", **by)
assert result == ErrorResponse(error=ErrorType.ASSET_STORE_NOT_FOUND,
detail={"key": "watermark"})
+
+
+class TestTaskInstanceOperations:
+ @pytest.mark.parametrize("state", [TaskInstanceState.RUNNING,
TaskInstanceState.RESTARTING])
+ def test_start_running_elsewhere(self, task_sdk_client,
create_task_instance, session, state):
+ ti = create_task_instance(state=state, hostname="other-host", pid=1,
session=session)
+ session.commit()
+ with pytest.raises(TaskAlreadyRunningError):
+ task_sdk_client.task_instances.start(ti.id, 2, timezone.utcnow())
+
+ def test_start_finished(self, task_sdk_client, create_task_instance,
session):
+ ti = create_task_instance(state=TaskInstanceState.SUCCESS,
session=session)
+ session.commit()
+ with pytest.raises(ServerResponseError):
+ task_sdk_client.task_instances.start(ti.id, 1, timezone.utcnow())
+
+
+class TestDagRunOperations:
+ @pytest.fixture(autouse=True)
+ def dag(self, dag_maker, session):
+ with dag_maker(dag_id="target", session=session, serialized=True):
+ EmptyOperator(task_id="task")
+ session.commit()
+
+ def test_trigger_existing_run(self, task_sdk_client):
+ assert task_sdk_client.dag_runs.trigger("target", "runid") ==
OKResponse(ok=True)
+ result = task_sdk_client.dag_runs.trigger("target", "runid")
+ assert result == ErrorResponse(error=ErrorType.DAGRUN_ALREADY_EXISTS)
+
+ def test_trigger_existing_run_with_reset(self, task_sdk_client):
+ task_sdk_client.dag_runs.trigger("target", "runid")
+ result = task_sdk_client.dag_runs.trigger("target", "runid",
reset_dag_run=True)
+ assert result == OKResponse(ok=True)
+
+
+class TestAssetOperations:
+ @pytest.fixture(autouse=True)
+ def asset(self, session):
+ clear_db_assets()
+ asset = AssetModel(name="asset", uri="s3://bucket/asset",
group="asset")
+ session.add(asset)
+ session.flush()
+ session.add(AssetActive.for_asset(asset))
+ session.commit()
+ yield asset
+ clear_db_assets()
Review Comment:
`TestAssetStateStoreOperations` has the same fixture. Instead of copying,
it’s better to make the fixture module-level (not autouse) and applied
class-level with `pytest.mark.usefixtures`.
--
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]