AlejandroMorgante commented on code in PR #74386:
URL: https://github.com/apache/airflow/pull/74386#discussion_r4238136409


##########
providers/databricks/tests/unit/databricks/operators/test_agent.py:
##########
@@ -0,0 +1,254 @@
+# 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 unittest import mock
+from uuid import UUID
+
+import pytest
+
+from airflow.providers.common.compat.sdk import TaskDeferred
+from airflow.providers.databricks.exceptions import (
+    DatabricksAgentInvocationError,
+    DatabricksAgentInvocationTimeout,
+)
+from airflow.providers.databricks.hooks.agent import DatabricksAgentHook
+from airflow.providers.databricks.operators.agent import 
DatabricksAgentInvokeOperator
+from airflow.providers.databricks.triggers.agent import 
DatabricksAgentInvocationTrigger
+
+APP_URL = "https://agent.databricksapps.com";
+INVOCATION_ID = "550e8400-e29b-41d4-a716-446655440000"
+
+
[email protected]
+def operator():
+    op = DatabricksAgentInvokeOperator(
+        task_id="invoke",
+        app_url=APP_URL,
+        input={"messages": []},
+        session_id="conversation",
+        invocation_id=INVOCATION_ID,
+    )
+    op.hook = mock.create_autospec(DatabricksAgentHook, instance=True)
+    op.hook.create_invocation.return_value = {"id": INVOCATION_ID, 
"status_url": "ignored"}
+    return op
+
+
[email protected]("field", ["polling_period_seconds", "timeout"])
[email protected]("value", [0, -1])
+def test_invalid_wait_options(field, value):
+    with pytest.raises(ValueError, match="must be positive"):
+        DatabricksAgentInvokeOperator(task_id="invoke", app_url=APP_URL, 
input={}, **{field: value})
+
+
[email protected](
+    ("field", "value"),
+    [
+        ("map_index", 0),
+        ("run_id", "next"),
+        ("task_id", "other"),
+        ("dag_id", "other"),
+        ("app_url", "https://other.databricksapps.com";),
+    ],
+)
+def test_idempotency_identity(operator, field, value):
+    operator.invocation_id = None
+    ti = mock.Mock(spec=["dag_id", "task_id", "run_id", "map_index", 
"try_number"])
+    ti.dag_id, ti.task_id, ti.run_id, ti.map_index, ti.try_number = "dag", 
"task", "run", -1, 1
+    first = operator._get_invocation_id({"ti": ti})
+    assert str(UUID(first)) == first
+    ti.try_number = 2
+    assert operator._get_invocation_id({"ti": ti}) == first
+    if field == "app_url":
+        operator.app_url = value
+    else:
+        setattr(ti, field, value)
+    assert operator._get_invocation_id({"ti": ti}) != first
+
+
+def test_template_fields():
+    operator = DatabricksAgentInvokeOperator(
+        task_id="invoke",
+        app_url="https://{{ app }}.databricksapps.com",
+        input={"messages": [{"role": "user", "content": "{{ prompt }}"}]},
+        databricks_conn_id="{{ connection }}",
+        session_id="{{ run_id }}",
+        invocation_id="{{ invocation_id }}",
+    )
+    operator.render_template_fields(
+        {
+            "app": "agent",
+            "prompt": "hello",
+            "connection": "oauth",
+            "run_id": "conversation",
+            "invocation_id": INVOCATION_ID,
+        }
+    )
+    assert operator.app_url == APP_URL
+    assert operator.input == {"messages": [{"role": "user", "content": 
"hello"}]}
+    assert operator.databricks_conn_id == "oauth"
+    assert operator.session_id == "conversation"
+    assert operator.invocation_id == INVOCATION_ID
+
+
+def test_submit_without_waiting(operator):
+    operator.wait_for_termination = False
+    operator.deferrable = True
+    assert operator.execute({}) == operator.hook.create_invocation.return_value
+    operator.hook.create_invocation.assert_called_once_with(INVOCATION_ID, 
operator.input, "conversation")
+    operator.hook.get_invocation.assert_not_called()
+
+
[email protected]("status", ["completed", "interrupted"])
+def test_immediate_result(operator, status):
+    result = {"status": status, "output": {"answer": "hello"}}
+    operator.hook.create_invocation.return_value = result
+    assert operator.execute({}) == result
+    operator.hook.get_invocation.assert_not_called()
+
+
[email protected]("airflow.providers.databricks.operators.agent.time.sleep", 
autospec=True)
[email protected]("airflow.providers.databricks.operators.agent.time.monotonic", 
autospec=True, return_value=0)
+def test_waits_for_result(monotonic, sleep, operator):
+    result = {"status": "completed", "output": "answer"}
+    operator.hook.get_invocation.side_effect = [{"status": "running"}, result]
+    assert operator.execute({}) == result
+    assert (
+        operator.hook.get_invocation.call_args_list
+        == [mock.call(INVOCATION_ID, "conversation", timeout_seconds=3600)] * 2
+    )
+    sleep.assert_called_once_with(10)
+
+
[email protected]("result", [{"status": "failed"}, {}])
+def test_polling_failure(operator, result):
+    operator.hook.get_invocation.return_value = result
+    with pytest.raises(DatabricksAgentInvocationError, match="failed|missing"):
+        operator.execute({})
+
+
[email protected]("airflow.providers.databricks.operators.agent.time.monotonic", 
autospec=True)
[email protected]("elapsed", [3600, 3601])
+def test_timeout_before_poll(monotonic, operator, elapsed):
+    monotonic.side_effect = [0, elapsed]
+    operator.hook.get_invocation.return_value = {"status": "running"}
+    with pytest.raises(DatabricksAgentInvocationTimeout, match=INVOCATION_ID):
+        operator.execute({})
+    operator.hook.get_invocation.assert_not_called()
+
+
[email protected]("airflow.providers.databricks.operators.agent.time.monotonic", 
autospec=True)
[email protected]("elapsed", [3600, 3601])
+def test_rejects_late_terminal_result(monotonic, operator, elapsed):
+    monotonic.side_effect = [0, 0, elapsed]
+    operator.hook.get_invocation.return_value = {"status": "completed", 
"output": "late"}
+    with pytest.raises(DatabricksAgentInvocationTimeout, match=INVOCATION_ID):
+        operator.execute({})
+    operator.hook.get_invocation.assert_called_once_with(INVOCATION_ID, 
"conversation", timeout_seconds=3600)
+
+
[email protected]("airflow.providers.databricks.operators.agent.time.sleep", 
autospec=True)
[email protected](
+    "airflow.providers.databricks.operators.agent.time.monotonic",
+    autospec=True,
+    side_effect=[0, 3599, 3599, 3600],
+)
+def test_timeout_after_sleep(monotonic, sleep, operator):
+    operator.hook.get_invocation.return_value = {"status": "running"}
+    with pytest.raises(DatabricksAgentInvocationTimeout, match=INVOCATION_ID):
+        operator.execute({})
+    operator.hook.get_invocation.assert_called_once_with(INVOCATION_ID, 
"conversation", timeout_seconds=1)
+    sleep.assert_called_once_with(1)
+
+
+def test_defers(operator):
+    operator.deferrable = True
+    with pytest.raises(TaskDeferred) as exc:
+        operator.execute({})
+    trigger = exc.value.trigger
+    assert isinstance(trigger, DatabricksAgentInvocationTrigger)
+    assert trigger.app_url == APP_URL
+    assert trigger.invocation_id == INVOCATION_ID
+    assert trigger.session_id == "conversation"

Review Comment:
   I’ve added non-default connection and polling values and assertions for 
session forwarding in execute_complete



-- 
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]

Reply via email to