File: test_agents_mock_overloads.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (139 lines) | stat: -rw-r--r-- 6,043 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
from unittest.mock import patch

import pytest
from azure.ai.agents import AgentsClient
from azure.ai.agents.aio import AgentsClient as AsyncAgentsClient
from typing import List, Dict, MutableMapping, Any

from overload_assert_utils import (
    assert_same_http_requests,
    OverloadAssertion,
    dict_to_io_bytes,
    get_mock_fn,
)

from azure.ai.agents.models import ThreadMessageOptions, ToolResources, VectorStore


JSON = MutableMapping[str, Any]  # pylint: disable=unsubscriptable-object


class TestSignatures:

    @pytest.mark.asyncio
    @assert_same_http_requests
    async def test_create_agent(
        self, agent: AgentsClient, async_agent: AsyncAgentsClient, assertion: OverloadAssertion
    ):
        model = "gpt-4-1106-preview"
        name = "first"
        instructions = "You are a helpful agent"
        body = {"model": model, "name": name, "instructions": instructions}

        agent.create_agent(model=model, name=name, instructions=instructions)
        agent.create_agent(body=body)
        agent.create_agent(body=dict_to_io_bytes(body))

        await async_agent.create_agent(model=model, name=name, instructions=instructions)
        await async_agent.create_agent(body=body)
        await async_agent.create_agent(body=dict_to_io_bytes(body))

        assertion.same_http_requests_from(operation_count=6, api_per_operation_count=1)

    @pytest.mark.asyncio
    @assert_same_http_requests
    async def test_create_vector_store_and_poll(
        self, agent: AgentsClient, async_agent: AsyncAgentsClient, assertion: OverloadAssertion
    ):
        file_ids = ["file_id"]
        body = {"file_ids": file_ids}

        with patch(
            "azure.ai.agents.operations._operations.VectorStoresOperations.create",
            wraps=get_mock_fn(
                agent.vector_stores.create, return_val=VectorStore({"id": "store_1", "status": "in_progress"})
            ),
        ), patch(
            "azure.ai.agents.operations._operations.VectorStoresOperations.get",
            wraps=get_mock_fn(
                agent.vector_stores.get, return_val=VectorStore({"id": "store_1", "status": "completed"})
            ),
        ):

            agent.vector_stores.create_and_poll(file_ids=file_ids, polling_interval=0)
            agent.vector_stores.create_and_poll(body=body, polling_interval=0)
            agent.vector_stores.create_and_poll(body=dict_to_io_bytes(body), polling_interval=0)

        with patch(
            "azure.ai.agents.aio.operations._operations.VectorStoresOperations.create",
            wraps=get_mock_fn(
                async_agent.vector_stores.create, return_val=VectorStore({"id": "store_1", "status": "in_progress"})
            ),
        ), patch(
            "azure.ai.agents.aio.operations._operations.VectorStoresOperations.get",
            wraps=get_mock_fn(
                async_agent.vector_stores.get, return_val=VectorStore({"id": "store_1", "status": "completed"})
            ),
        ):
            await async_agent.vector_stores.create_and_poll(file_ids=file_ids, polling_interval=0)
            await async_agent.vector_stores.create_and_poll(body=body, polling_interval=0)
            await async_agent.vector_stores.create_and_poll(body=dict_to_io_bytes(body), polling_interval=0)
        assertion.same_http_requests_from(operation_count=6, api_per_operation_count=2)

    @pytest.mark.asyncio
    @assert_same_http_requests
    async def test_create_thread(
        self, agent: AgentsClient, async_agent: AsyncAgentsClient, assertion: OverloadAssertion
    ):
        messages: List[ThreadMessageOptions] = []
        tool_resources: ToolResources = ToolResources()
        metadata: Dict[str, str] = {}
        body = {"messages": messages, "tool_resources": tool_resources, "metadata": metadata}

        agent.threads.create(messages=messages, tool_resources=tool_resources, metadata=metadata)
        agent.threads.create(body=body)
        agent.threads.create(body=dict_to_io_bytes(body))

        await async_agent.threads.create(messages=messages, tool_resources=tool_resources, metadata=metadata)
        await async_agent.threads.create(body=body)
        await async_agent.threads.create(body=dict_to_io_bytes(body))

        assertion.same_http_requests_from(operation_count=6, api_per_operation_count=1)

    @pytest.mark.asyncio
    @pytest.mark.skip("Defect: during body as JSON and IO Bytes don't, backend not called with stream=False")
    @assert_same_http_requests
    async def test_create_run(self, agent: AgentsClient, async_agent: AsyncAgentsClient, assertion: OverloadAssertion):
        thread_id = "thread_id"
        agent_id = "agent_id"
        body = {"agent_id": agent_id}

        agent.runs.create(thread_id, agent_id=agent_id)
        agent.runs.create(thread_id, body=body)
        agent.runs.create(thread_id, body=dict_to_io_bytes(body))

        await async_agent.runs.create(thread_id, agent_id=agent_id)
        await async_agent.runs.create(thread_id, body=body)
        await async_agent.runs.create(thread_id, body=dict_to_io_bytes(body))

        assertion.same_http_requests_from(operation_count=6, api_per_operation_count=1)

    @pytest.mark.asyncio
    @pytest.mark.skip("Defect: during body as JSON and IO Bytes don't, backend not called with stream=True")
    @assert_same_http_requests
    async def test_create_stream(
        self, agent: AgentsClient, async_agent: AsyncAgentsClient, assertion: OverloadAssertion
    ):
        thread_id = "thread_id"
        agent_id = "agent_id"
        body = {"agent_id": agent_id}

        agent.runs.stream(thread_id, agent_id=agent_id)
        agent.runs.stream(thread_id, body=body)
        agent.runs.stream(thread_id, body=dict_to_io_bytes(body))

        await async_agent.runs.stream(thread_id, agent_id=agent_id)
        await async_agent.runs.stream(thread_id, body=body)
        await async_agent.runs.stream(thread_id, body=dict_to_io_bytes(body))

        assertion.same_http_requests_from(operation_count=6, api_per_operation_count=1)