|
1 | | - |
2 | | -# Copyright 2025 Google LLC |
| 1 | +# Copyright 2026 Google LLC |
3 | 2 | # |
4 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 4 | # you may not use this file except in compliance with the License. |
|
18 | 17 | """Tests for Interactions API URL paths.""" |
19 | 18 |
|
20 | 19 | from unittest import mock |
21 | | -import pytest |
| 20 | +import google.auth |
| 21 | +from httpx import Client as HTTPClient |
22 | 22 | from httpx import Request, Response |
| 23 | +import pytest |
23 | 24 | from ..._api_client import AsyncHttpxClient |
24 | | -from httpx import Client as HTTPClient |
25 | 25 | from .. import pytest_helper |
26 | | -import google.auth |
27 | 26 |
|
28 | | -@mock.patch.object(google.auth, "default", autospec=True) |
29 | | -def test_interactions_paths(mock_auth_default, client): |
30 | | - interaction_id = "test-interaction-id" |
31 | 27 |
|
| 28 | +@mock.patch.object(google.auth, 'default', autospec=True) |
| 29 | +def test_interactions_paths(mock_auth_default, client): |
| 30 | + interaction_id = 'test-interaction-id' |
32 | 31 | mock_creds = mock.Mock() |
33 | | - mock_creds.token = "test-token" |
| 32 | + mock_creds.token = 'test-token' |
34 | 33 | mock_creds.expired = False |
35 | | - mock_creds.quota_project_id = "test-quota-project" |
36 | | - mock_auth_default.return_value = (mock_creds, "test-project") |
| 34 | + mock_creds.quota_project_id = 'test-quota-project' |
| 35 | + mock_auth_default.return_value = (mock_creds, 'test-project') |
37 | 36 |
|
38 | 37 | if client._api_client.vertexai: |
39 | | - expected_base_url = f'https://{client._api_client.location}-aiplatform.googleapis.com/v1beta1/projects/{client._api_client.project}/locations/{client._api_client.location}' |
| 38 | + if client._api_client.location == 'global': |
| 39 | + expected_base_url = ( |
| 40 | + 'https://aiplatform.googleapis.com' |
| 41 | + f'/v1beta1/projects/{client._api_client.project}' |
| 42 | + '/locations/global' |
| 43 | + ) |
| 44 | + else: |
| 45 | + expected_base_url = ( |
| 46 | + f'https://{client._api_client.location}-aiplatform.googleapis.com' |
| 47 | + f'/v1beta1/projects/{client._api_client.project}' |
| 48 | + f'/locations/{client._api_client.location}' |
| 49 | + ) |
40 | 50 | else: |
41 | | - expected_base_url = "https://generativelanguage.googleapis.com/v1beta" |
| 51 | + expected_base_url = 'https://generativelanguage.googleapis.com/v1beta' |
42 | 52 |
|
43 | | - with mock.patch.object(HTTPClient, "send") as mock_send: |
| 53 | + with mock.patch.object(HTTPClient, 'send') as mock_send: |
44 | 54 | mock_send.return_value = Response(200, request=Request('GET', '')) |
45 | 55 | client.interactions.get(id=interaction_id) |
46 | 56 | mock_send.assert_called_once() |
47 | 57 | request = mock_send.call_args[0][0] |
48 | | - assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}' |
| 58 | + assert str(request.url) == ( |
| 59 | + f'{expected_base_url}/interactions/{interaction_id}' |
| 60 | + ) |
49 | 61 |
|
50 | 62 | mock_send.reset_mock() |
51 | 63 | mock_send.return_value = Response(200, request=Request('POST', '')) |
52 | 64 | client.interactions.cancel(id=interaction_id) |
53 | 65 | mock_send.assert_called_once() |
54 | 66 | request = mock_send.call_args[0][0] |
55 | | - assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}/cancel' |
| 67 | + assert str(request.url) == ( |
| 68 | + f'{expected_base_url}/interactions/{interaction_id}/cancel' |
| 69 | + ) |
56 | 70 |
|
57 | 71 | mock_send.reset_mock() |
58 | 72 | mock_send.return_value = Response(200, request=Request('DELETE', '')) |
59 | 73 | client.interactions.delete(id=interaction_id) |
60 | 74 | mock_send.assert_called_once() |
61 | 75 | request = mock_send.call_args[0][0] |
62 | | - assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}' |
| 76 | + assert str(request.url) == ( |
| 77 | + f'{expected_base_url}/interactions/{interaction_id}' |
| 78 | + ) |
| 79 | + |
63 | 80 |
|
64 | 81 | @pytest.mark.asyncio |
65 | | -@mock.patch.object(google.auth, "default", autospec=True) |
| 82 | +@mock.patch.object(google.auth, 'default', autospec=True) |
66 | 83 | async def test_async_interactions_paths(mock_auth_default, client): |
67 | | - interaction_id = "test-interaction-id" |
68 | | - |
| 84 | + interaction_id = 'test-interaction-id' |
69 | 85 | mock_creds = mock.Mock() |
70 | | - mock_creds.token = "test-token" |
| 86 | + mock_creds.token = 'test-token' |
71 | 87 | mock_creds.expired = False |
72 | | - mock_creds.quota_project_id = "test-quota-project" |
73 | | - mock_auth_default.return_value = (mock_creds, "test-project") |
| 88 | + mock_creds.quota_project_id = 'test-quota-project' |
| 89 | + mock_auth_default.return_value = (mock_creds, 'test-project') |
74 | 90 |
|
75 | 91 | if client._api_client.vertexai: |
76 | | - expected_base_url = f'https://{client._api_client.location}-aiplatform.googleapis.com/v1beta1/projects/{client._api_client.project}/locations/{client._api_client.location}' |
| 92 | + if client._api_client.location == 'global': |
| 93 | + expected_base_url = ( |
| 94 | + 'https://aiplatform.googleapis.com' |
| 95 | + f'/v1beta1/projects/{client._api_client.project}' |
| 96 | + '/locations/global' |
| 97 | + ) |
| 98 | + else: |
| 99 | + expected_base_url = ( |
| 100 | + f'https://{client._api_client.location}-aiplatform.googleapis.com' |
| 101 | + f'/v1beta1/projects/{client._api_client.project}' |
| 102 | + f'/locations/{client._api_client.location}' |
| 103 | + ) |
77 | 104 | else: |
78 | | - expected_base_url = "https://generativelanguage.googleapis.com/v1beta" |
| 105 | + expected_base_url = 'https://generativelanguage.googleapis.com/v1beta' |
79 | 106 |
|
80 | | - with mock.patch.object(AsyncHttpxClient, "send") as mock_send: |
| 107 | + with mock.patch.object(AsyncHttpxClient, 'send') as mock_send: |
81 | 108 | mock_send.return_value = Response(200, request=Request('GET', '')) |
82 | 109 | await client.aio.interactions.get(id=interaction_id) |
83 | 110 | mock_send.assert_called_once() |
84 | 111 | request = mock_send.call_args[0][0] |
85 | | - assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}' |
| 112 | + assert str(request.url) == ( |
| 113 | + f'{expected_base_url}/interactions/{interaction_id}' |
| 114 | + ) |
86 | 115 |
|
87 | 116 | mock_send.reset_mock() |
88 | 117 | mock_send.return_value = Response(200, request=Request('POST', '')) |
89 | 118 | await client.aio.interactions.cancel(id=interaction_id) |
90 | 119 | mock_send.assert_called_once() |
91 | 120 | request = mock_send.call_args[0][0] |
92 | | - assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}/cancel' |
| 121 | + assert str(request.url) == ( |
| 122 | + f'{expected_base_url}/interactions/{interaction_id}/cancel' |
| 123 | + ) |
93 | 124 |
|
94 | 125 | mock_send.reset_mock() |
95 | 126 | mock_send.return_value = Response(200, request=Request('DELETE', '')) |
96 | 127 | await client.aio.interactions.delete(id=interaction_id) |
97 | 128 | mock_send.assert_called_once() |
98 | 129 | request = mock_send.call_args[0][0] |
99 | | - assert str(request.url) == f'{expected_base_url}/interactions/{interaction_id}' |
| 130 | + assert str(request.url) == ( |
| 131 | + f'{expected_base_url}/interactions/{interaction_id}' |
| 132 | + ) |
| 133 | + |
100 | 134 |
|
101 | 135 | pytestmark = pytest_helper.setup( |
102 | 136 | file=__file__, |
|
0 commit comments