aaron-y-chen commented on code in PR #71350: URL: https://github.com/apache/airflow/pull/71350#discussion_r3859309099
########## providers/microsoft/azure/tests/unit/microsoft/azure/operators/test_analysis_services.py: ########## @@ -0,0 +1,179 @@ +# 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 + +from datetime import timedelta +from unittest import mock + +import pytest + +from airflow.providers.common.compat.sdk import TaskDeferred +from airflow.providers.microsoft.azure.hooks.analysis_services import ( + AzureAnalysisServicesRefreshException, + AzureAnalysisServicesRefreshStatus, + RefreshType, +) +from airflow.providers.microsoft.azure.operators.analysis_services import ( + AzureAnalysisServicesRefreshOperator, +) +from airflow.providers.microsoft.azure.triggers.analysis_services import ( + AzureAnalysisServicesRefreshTrigger, +) + +CONN_ID = "azure_analysis_services_test" +SERVER_NAME = "testserver" +DATABASE = "adventureworks" +REFRESH_ID = "refresh-id" +REFRESH_TYPE: RefreshType = "calculate" +CHECK_INTERVAL = 5 +TIMEOUT = 120 +REQUEST_TIMEOUT = 30 + + +def build_operator( + *, + wait_for_termination: bool = True, + check_interval: float = CHECK_INTERVAL, + timeout: float = TIMEOUT, + request_timeout: float = REQUEST_TIMEOUT, +) -> AzureAnalysisServicesRefreshOperator: + """Build an operator with standard test arguments.""" + return AzureAnalysisServicesRefreshOperator( + task_id="refresh_model", + server_name=SERVER_NAME, + database=DATABASE, + azure_analysis_services_conn_id=CONN_ID, + refresh_type=REFRESH_TYPE, + wait_for_termination=wait_for_termination, + check_interval=check_interval, + timeout=timeout, + request_timeout=request_timeout, + ) + + +def build_context() -> dict: + """Build a context whose task instance records XCom pushes.""" + return {"ti": mock.MagicMock()} + + +def success_event(refresh_status: str | None) -> dict: + return { + "status": "success", + "refresh_status": refresh_status, + "message": "ok", + "refresh_id": REFRESH_ID, + } + + +class TestAzureAnalysisServicesRefreshOperator: + @pytest.mark.parametrize( + ("check_interval", "timeout", "request_timeout", "message"), + [ + (0, TIMEOUT, REQUEST_TIMEOUT, "check_interval"), + (CHECK_INTERVAL, 0, REQUEST_TIMEOUT, "timeout"), + (CHECK_INTERVAL, TIMEOUT, 0, "request_timeout"), + ], + ) + def test_rejects_invalid_polling_arguments(self, check_interval, timeout, request_timeout, message): + with pytest.raises(ValueError, match=message): + build_operator( + check_interval=check_interval, + timeout=timeout, + request_timeout=request_timeout, + ) + + def test_defines_template_fields(self): + assert AzureAnalysisServicesRefreshOperator.template_fields == ( + "azure_analysis_services_conn_id", + "server_name", + "database", + "refresh_type", + ) + + def test_execute_defers_without_refresh_id(self): + with pytest.raises(TaskDeferred) as deferred: + build_operator().execute(context=build_context()) Review Comment: Thanks, another thing I learned today! I kept `test_execute_defers_without_refresh_id` because it verifies deferral details that `execute_operator` does not expose. I added `test_execute_operator_full_lifecycle` to exercise the complete deferred lifecycle, along with equivalent lifecycle coverage for the sensor. -- 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]
