Skip to content

Commit e48a975

Browse files
authoredMar 20, 2025
.Net: Fix Amazon Bedrock UT (microsoft#11079)
### Motivation and Context The given test was giving a false negative in environments where Bedrock Runtime was property configured and could be retrieved from the DI container. This change ensures this test only executes when the environment has no runtime configured to be recovered from the DI container.
1 parent f80e080 commit e48a975

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed
 

‎dotnet/src/Connectors/Connectors.Amazon.UnitTests/Services/BedrockTextEmbeddingGenerationServiceTests.cs

+27-7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
using System.Collections.Generic;
55
using System.Threading.Tasks;
66
using Amazon.BedrockRuntime;
7+
using Amazon.Runtime;
8+
using Microsoft.Extensions.DependencyInjection;
79
using Microsoft.SemanticKernel.Embeddings;
810
using Microsoft.SemanticKernel.Services;
911
using Moq;
@@ -72,19 +74,37 @@ public void ShouldThrowExceptionForEmptyModelId()
7274
/// Checks that an invalid BedrockRuntime object will throw an exception.
7375
/// </summary>
7476
[Fact]
75-
public async Task ShouldThrowExceptionForNullBedrockRuntimeAsync()
77+
public async Task ShouldThrowExceptionForNullBedrockRuntimeWhenNotConfiguredAsync()
7678
{
7779
// Arrange
7880
string modelId = "amazon.titan-embed-text-v2:0";
7981
List<string> prompts = new() { "King", "Queen", "Prince" };
8082
IAmazonBedrockRuntime? nullBedrockRuntime = null;
83+
bool notConfigured = false;
8184

82-
// Act & Assert
83-
await Assert.ThrowsAnyAsync<Exception>(async () =>
85+
try
8486
{
85-
var kernel = Kernel.CreateBuilder().AddBedrockTextEmbeddingGenerationService(modelId, nullBedrockRuntime).Build();
86-
var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>();
87-
await service.GenerateEmbeddingsAsync(prompts).ConfigureAwait(true);
88-
}).ConfigureAwait(true);
87+
var runtime = new ServiceCollection()
88+
.TryAddAWSService<IAmazonBedrockRuntime>()
89+
.BuildServiceProvider()
90+
.GetService<IAmazonBedrockRuntime>();
91+
}
92+
catch (AmazonClientException)
93+
{
94+
// If cannot grab the runtime from the container then we are not configured
95+
notConfigured = true;
96+
}
97+
98+
// Act
99+
if (notConfigured)
100+
{
101+
// If No RegionEndpoint or ServiceURL is configured, the runtime will throw an exception
102+
await Assert.ThrowsAnyAsync<Exception>(async () =>
103+
{
104+
var kernel = Kernel.CreateBuilder().AddBedrockTextEmbeddingGenerationService(modelId, nullBedrockRuntime).Build();
105+
var service = kernel.GetRequiredService<ITextEmbeddingGenerationService>();
106+
await service.GenerateEmbeddingsAsync(prompts).ConfigureAwait(true);
107+
}).ConfigureAwait(true);
108+
}
89109
}
90110
}

0 commit comments

Comments
 (0)
Please sign in to comment.