kaxil commented on code in PR #67248: URL: https://github.com/apache/airflow/pull/67248#discussion_r3307541458
########## task-sdk/tests/task_sdk/definitions/test_asset_state.py: ########## @@ -0,0 +1,174 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +from __future__ import annotations + +import pytest +from pydantic import ValidationError + +from airflow.sdk import AssetState +from airflow.sdk.definitions.asset.state import AssetState as DirectAssetState +from airflow.sdk.exceptions import AirflowRuntimeError, ErrorType +from airflow.sdk.execution_time.comms import ( + AssetStateResult, + ClearAssetStateByName, + ClearAssetStateByUri, + DeleteAssetStateByName, + DeleteAssetStateByUri, + ErrorResponse, + GetAssetStateByName, + GetAssetStateByUri, + OKResponse, + SetAssetStateByName, + SetAssetStateByUri, +) +from airflow.sdk.execution_time.context import AssetStateAccessor + + +class TestAssetState: + """Validate the public AssetState SDK interface.""" + + asset_name: str = "my_asset" + asset_uri: str = "s3://bucket/key" + + def test_lazy_import_from_airflow_sdk(self): + """Validate the lazy __init__.py alias resolves to the real class""" + assert AssetState is DirectAssetState + + def test_is_asset_state_accessor_subclass(self): + """Validate that AssetState inherits all AssetStateAccess logic""" + assert issubclass(AssetState, AssetStateAccessor) + assert isinstance(AssetState(name=self.asset_name), AssetStateAccessor) + + def test_requires_name_or_uri(self): + """Validate that constructing without either identifier must fail fast at init time""" + with pytest.raises(ValueError, match="Either `name` or `uri` must be provided"): + AssetState() + + def test_set_fails_on_non_string_key(self, mock_supervisor_comms): + """Validate that set(key, value) where isinstance(key, str) false raises a ValidationError""" + with pytest.raises(ValidationError): + AssetState(name=self.asset_name).set(123, "some_value") # type: ignore[arg-type] + + mock_supervisor_comms.send.assert_not_called() + + def test_set_fails_on_non_string_value(self, mock_supervisor_comms): + """Validate that set(key, value) where isinstance(value, str) false raises a ValidationError""" + with pytest.raises(ValidationError): + AssetState(name=self.asset_name).set("watermark", 12345) # type: ignore[arg-type] + + mock_supervisor_comms.send.assert_not_called() Review Comment: This test can't pass as written. `SetAssetStateByName.value` is typed `JsonValue` (`task-sdk/src/airflow/sdk/execution_time/comms.py:958`), which accepts `int`. Verified with pydantic that `SetAssetStateByName(name="a", key="k", value=12345)` is accepted, so neither the `ValidationError` nor `send.assert_not_called()` would hold. Either narrow the field to `str` on `SetAssetStateByName`/`SetAssetStateByUri`/`AssetStateResponse` if you want string-only values, or drop the test -- `JsonValue` matches `SetTaskState.value` and is probably the intended contract. -- 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]
