tests: add additional test coverage
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
namespace AnthropicClient.Tests.Unit.Json;
|
||||
|
||||
public class SourceConverterTests : SerializationTest
|
||||
{
|
||||
[Fact]
|
||||
public void JsonDeserialization_WhenTypeIsUnknown_ItThrowsException()
|
||||
{
|
||||
var json = @"{ ""type"": ""unknown"" }";
|
||||
|
||||
var action = () => Deserialize<Source>(json);
|
||||
|
||||
action.Should().Throw<JsonException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonSerialization_WhenSourceIsNotKnown_ItShouldHaveExpectedShape()
|
||||
{
|
||||
var source = new TestSource();
|
||||
|
||||
var result = Serialize<Source>(source);
|
||||
|
||||
JsonAssert.Equal(@"{ ""type"": ""test"" }", result);
|
||||
}
|
||||
|
||||
private class TestSource : Source
|
||||
{
|
||||
public TestSource() : base("test")
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,13 @@
|
||||
namespace AnthropicClient.Tests.Unit.Models;
|
||||
|
||||
public class Base64SourceTests
|
||||
public class Base64SourceTests : SerializationTest
|
||||
{
|
||||
private readonly string _testJson = @"{
|
||||
""type"": ""base64"",
|
||||
""media_type"": ""application/pdf"",
|
||||
""data"": ""base64data""
|
||||
}";
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalledWithValidArguments_ItShouldSetProperties()
|
||||
{
|
||||
@@ -36,4 +42,47 @@ public class Base64SourceTests
|
||||
|
||||
action.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape()
|
||||
{
|
||||
var mediaType = "application/pdf";
|
||||
var data = "base64data";
|
||||
var source = new Base64Source(mediaType, data);
|
||||
|
||||
var result = Serialize<Source>(source);
|
||||
|
||||
JsonAssert.Equal(_testJson, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedShape()
|
||||
{
|
||||
var source = Deserialize<Source>(_testJson);
|
||||
|
||||
var base64Source = source.As<Base64Source>();
|
||||
base64Source!.Type.Should().Be("base64");
|
||||
base64Source.MediaType.Should().Be("application/pdf");
|
||||
base64Source.Data.Should().Be("base64data");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonDeserialization_WhenMediaTypeIsMissing_ItShouldThrowException()
|
||||
{
|
||||
var json = @"{ ""type"": ""base64"", ""data"": ""base64data"" }";
|
||||
|
||||
var action = () => Deserialize<Source>(json);
|
||||
|
||||
action.Should().Throw<JsonException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonDeserialization_WhenMediaTypeIsNull_ItShouldThrowException()
|
||||
{
|
||||
var json = @"{ ""type"": ""base64"", ""media_type"": null, ""data"": ""base64data"" }";
|
||||
|
||||
var action = () => Deserialize<Source>(json);
|
||||
|
||||
action.Should().Throw<JsonException>();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,17 @@
|
||||
namespace AnthropicClient.Tests.Unit.Models;
|
||||
|
||||
public class CustomSourceTests
|
||||
public class CustomSourceTests : SerializationTest
|
||||
{
|
||||
private readonly string _testJson = @"{
|
||||
""type"": ""content"",
|
||||
""content"": [
|
||||
{
|
||||
""type"": ""text"",
|
||||
""text"": ""Sample text""
|
||||
}
|
||||
]
|
||||
}";
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldSetProperties()
|
||||
{
|
||||
@@ -32,4 +42,30 @@ public class CustomSourceTests
|
||||
|
||||
act.Should().Throw<ArgumentNullException>();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape()
|
||||
{
|
||||
var content = new List<TextContent>
|
||||
{
|
||||
new("Sample text")
|
||||
};
|
||||
var source = new CustomSource(content);
|
||||
|
||||
var result = Serialize<Source>(source);
|
||||
|
||||
JsonAssert.Equal(_testJson, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedShape()
|
||||
{
|
||||
var source = Deserialize<Source>(_testJson);
|
||||
|
||||
var customSource = source.As<CustomSource>();
|
||||
customSource!.Type.Should().Be("content");
|
||||
customSource.Content.Should().HaveCount(1);
|
||||
customSource.Content[0].Type.Should().Be("text");
|
||||
customSource.Content[0].Text.Should().Be("Sample text");
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,13 @@
|
||||
namespace AnthropicClient.Tests.Unit.Models;
|
||||
|
||||
public class TextSourceTests
|
||||
public class TextSourceTests : SerializationTest
|
||||
{
|
||||
private readonly string _testJson = @"{
|
||||
""type"": ""text"",
|
||||
""media_type"": ""text/plain"",
|
||||
""data"": ""data""
|
||||
}";
|
||||
|
||||
[Fact]
|
||||
public void Constructor_WhenCalled_ItShouldSetProperties()
|
||||
{
|
||||
@@ -11,4 +17,25 @@ public class TextSourceTests
|
||||
result.MediaType.Should().Be("text/plain");
|
||||
result.Data.Should().Be("data");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape()
|
||||
{
|
||||
var source = new TextSource("data");
|
||||
|
||||
var result = Serialize<Source>(source);
|
||||
|
||||
JsonAssert.Equal(_testJson, result);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedShape()
|
||||
{
|
||||
var source = Deserialize<Source>(_testJson);
|
||||
|
||||
var textSource = source.As<TextSource>();
|
||||
textSource!.Type.Should().Be("text");
|
||||
textSource.MediaType.Should().Be("text/plain");
|
||||
textSource.Data.Should().Be("data");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user