|
4 | 4 |
|
5 | 5 | import json |
6 | 6 | import os |
7 | | -from typing import Any |
| 7 | +from typing import Any, cast |
8 | 8 | from unittest.mock import MagicMock |
9 | 9 |
|
10 | 10 | import pytest |
11 | 11 | from agent_framework import Embedding, GeneratedEmbeddings |
| 12 | +from boto3.session import Session as Boto3Session |
12 | 13 |
|
13 | 14 | from agent_framework_bedrock import BedrockEmbeddingClient, BedrockEmbeddingOptions |
14 | 15 |
|
@@ -143,6 +144,76 @@ async def test_bedrock_embedding_default_region() -> None: |
143 | 144 | assert client.region == "us-east-1" |
144 | 145 |
|
145 | 146 |
|
| 147 | +class _FakeSession: |
| 148 | + def __init__(self, region_name: str | None = None) -> None: |
| 149 | + self.calls: list[dict[str, Any]] = [] |
| 150 | + self.region_name = region_name |
| 151 | + |
| 152 | + def client(self, service_name: str, *, region_name: str, config: Any) -> _StubBedrockEmbeddingRuntime: |
| 153 | + self.calls.append({"service_name": service_name, "region_name": region_name}) |
| 154 | + return _StubBedrockEmbeddingRuntime() |
| 155 | + |
| 156 | + |
| 157 | +@pytest.mark.parametrize( |
| 158 | + ("region", "env_region", "session_region", "expected_region"), |
| 159 | + [ |
| 160 | + (None, None, "eu-west-1", "eu-west-1"), |
| 161 | + ("us-west-2", None, "eu-west-1", "us-west-2"), |
| 162 | + (None, "ap-south-1", "eu-west-1", "ap-south-1"), |
| 163 | + (None, None, None, "us-east-1"), |
| 164 | + ], |
| 165 | + ids=["session-region", "region-argument-wins", "env-region-wins", "default-region"], |
| 166 | +) |
| 167 | +async def test_bedrock_embedding_resolves_region_with_boto3_session( |
| 168 | + monkeypatch: pytest.MonkeyPatch, |
| 169 | + region: str | None, |
| 170 | + env_region: str | None, |
| 171 | + session_region: str | None, |
| 172 | + expected_region: str, |
| 173 | +) -> None: |
| 174 | + """A configured region wins, then the provided session's region, then the default region.""" |
| 175 | + if env_region: |
| 176 | + monkeypatch.setenv("BEDROCK_REGION", env_region) |
| 177 | + else: |
| 178 | + monkeypatch.delenv("BEDROCK_REGION", raising=False) |
| 179 | + session = _FakeSession(region_name=session_region) |
| 180 | + |
| 181 | + client = BedrockEmbeddingClient( |
| 182 | + model="amazon.titan-embed-text-v2:0", region=region, boto3_session=cast(Boto3Session, session) |
| 183 | + ) |
| 184 | + |
| 185 | + assert session.calls == [{"service_name": "bedrock-runtime", "region_name": expected_region}] |
| 186 | + assert client.region == expected_region |
| 187 | + |
| 188 | + |
| 189 | +async def test_bedrock_embedding_reports_boto3_default_region(monkeypatch: pytest.MonkeyPatch) -> None: |
| 190 | + """Without a region or session, the region boto3 resolves is used and reported.""" |
| 191 | + monkeypatch.delenv("BEDROCK_REGION", raising=False) |
| 192 | + monkeypatch.setenv("AWS_DEFAULT_REGION", "eu-central-1") |
| 193 | + |
| 194 | + client = BedrockEmbeddingClient(model="amazon.titan-embed-text-v2:0") |
| 195 | + |
| 196 | + assert client.service_url() == "https://bedrock-runtime.eu-central-1.amazonaws.com" |
| 197 | + assert client.region == "eu-central-1" |
| 198 | + |
| 199 | + |
| 200 | +async def test_bedrock_embedding_ignores_boto3_session_when_runtime_client_supplied( |
| 201 | + monkeypatch: pytest.MonkeyPatch, |
| 202 | +) -> None: |
| 203 | + """A preconfigured runtime client wins, so the unused session should not change the reported region.""" |
| 204 | + monkeypatch.delenv("BEDROCK_REGION", raising=False) |
| 205 | + session = _FakeSession(region_name="eu-west-1") |
| 206 | + |
| 207 | + client = BedrockEmbeddingClient( |
| 208 | + model="amazon.titan-embed-text-v2:0", |
| 209 | + client=_StubBedrockEmbeddingRuntime(), # pyrefly: ignore[bad-argument-type] # ty: ignore[invalid-argument-type] # pyright: ignore[reportArgumentType] |
| 210 | + boto3_session=cast(Boto3Session, session), |
| 211 | + ) |
| 212 | + |
| 213 | + assert session.calls == [] |
| 214 | + assert client.region == "us-east-1" |
| 215 | + |
| 216 | + |
146 | 217 | # region: Integration Tests |
147 | 218 |
|
148 | 219 | skip_if_bedrock_embedding_integration_tests_disabled = pytest.mark.skipif( |
|
0 commit comments