tests: work on adding integration tests

This commit is contained in:
Stevan Freeborn
2024-07-01 23:59:24 -05:00
parent c2788c3893
commit ebae27077e
6 changed files with 180 additions and 2 deletions
@@ -14,6 +14,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="RichardSzalay.MockHttp" Version="7.0.0" />
<PackageReference Include="SystemTextJson.JsonDiffPatch.Xunit" Version="2.0.0" />
<PackageReference Include="xunit" Version="2.5.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3" />
@@ -0,0 +1,34 @@
namespace AnthropicClient.Tests.Integration;
public class AnthropicApiClientTests : IntegrationTest
{
[Theory]
[ClassData(typeof(ErrorTestData))]
public async Task CreateChatMessageAsync_WhenCalledAndErrorReturned_ItShouldHandleError(
HttpStatusCode statusCode,
string content,
Type errorType
)
{
_mockHttpMessageHandler
.WhenCreateMessageRequest()
.Respond(
statusCode,
"application/json",
content
);
var request = new ChatMessageRequest(
model: AnthropicModels.Claude3Haiku,
messages: [new(MessageRole.User, [new TextContent("Hello!")])]
);
var result = await Client.CreateChatMessageAsync(request);
result.IsSuccess.Should().BeFalse();
result.Error.Should().BeOfType<AnthropicError>();
var actualErrorType = result.Error.Error!.GetType();
actualErrorType.Should().Be(errorType);
}
}
@@ -0,0 +1,100 @@
namespace AnthropicClient.Tests.Integration;
public class ErrorTestData : IEnumerable<object[]>
{
public IEnumerator<object[]> GetEnumerator()
{
yield return new object[]
{
HttpStatusCode.BadRequest,
@"{
""type"": ""error"",
""error"": {
""type"": ""invalid_request_error"",
""message"": ""Invalid request.""
}
}",
typeof(InvalidRequestError)
};
yield return new object[]
{
HttpStatusCode.Unauthorized,
@"{
""type"": ""error"",
""error"": {
""type"": ""authentication_error"",
""message"": ""Authentication error.""
}
}",
typeof(AuthenticationError)
};
yield return new object[]
{
HttpStatusCode.Forbidden,
@"{
""type"": ""error"",
""error"": {
""type"": ""permission_error"",
""message"": ""Permission error.""
}
}",
typeof(PermissionError)
};
yield return new object[]
{
HttpStatusCode.NotFound,
@"{
""type"": ""error"",
""error"": {
""type"": ""not_found_error"",
""message"": ""Not found error.""
}
}",
typeof(NotFoundError)
};
yield return new object[]
{
HttpStatusCode.TooManyRequests,
@"{
""type"": ""error"",
""error"": {
""type"": ""rate_limit_error"",
""message"": ""Rate limit error.""
}
}",
typeof(RateLimitError)
};
yield return new object[]
{
HttpStatusCode.InternalServerError,
@"{
""type"": ""error"",
""error"": {
""type"": ""api_error"",
""message"": ""Internal server error.""
}
}",
typeof(ApiError)
};
yield return new object[]
{
(HttpStatusCode)529,
@"{
""type"": ""error"",
""error"": {
""type"": ""overloaded_error"",
""message"": ""Overloaded error.""
}
}",
typeof(OverloadedError)
};
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
@@ -0,0 +1,40 @@
namespace AnthropicClient.Tests.Integration;
public class IntegrationTest
{
protected readonly MockHttpMessageHandler _mockHttpMessageHandler = new();
protected AnthropicApiClient Client => CreateClient();
private AnthropicApiClient CreateClient()
{
return new AnthropicApiClient("test-key", _mockHttpMessageHandler.ToHttpClient());
}
}
public static class MockHttpMessageHandlerExtensions
{
private static MockedRequest SetupBaseRequest(this MockHttpMessageHandler mockHttpMessageHandler)
{
return mockHttpMessageHandler
.When(HttpMethod.Post, "https://api.anthropic.com/v1/messages")
.WithHeaders(new Dictionary<string, string>
{
{ "anthropic-version", "2023-06-01" },
{ "x-api-key", "test-key" }
});
}
public static MockedRequest WhenCreateMessageRequest(this MockHttpMessageHandler mockHttpMessageHandler)
{
return mockHttpMessageHandler
.SetupBaseRequest()
.WithJsonContent<ChatMessageRequest>(r => r.Stream == false);
}
public static MockedRequest WhenCreateStreamMessageRequest(this MockHttpMessageHandler mockHttpMessageHandler)
{
return mockHttpMessageHandler
.SetupBaseRequest()
.WithJsonContent<ChatMessageRequest>(r => r.Stream == true);
}
}
@@ -1,4 +1,3 @@
using System.Collections;
using System.Text.Json.Nodes;
namespace AnthropicClient.Tests.Unit.Utils;
+4
View File
@@ -7,3 +7,7 @@ global using AnthropicClient.Utils;
global using FluentAssertions;
global using Microsoft.Extensions.Configuration;
global using RichardSzalay.MockHttp;
global using System.Net;
global using System.Collections;