tests: add tests for overloaded constructor

This commit is contained in:
Stevan Freeborn
2024-08-17 17:53:20 -05:00
parent f2f150791e
commit 0253794235
2 changed files with 92 additions and 0 deletions
@@ -11,6 +11,16 @@ public class ImageContentTests : SerializationTest
""type"": ""image"" ""type"": ""image""
}"; }";
private readonly string _testJsonWithCacheControl = @"{
""source"": {
""media_type"": ""image/png"",
""data"": ""data"",
""type"": ""base64""
},
""cache_control"": { ""type"": ""ephemeral"" },
""type"": ""image""
}";
[Fact] [Fact]
public void Constructor_WhenCalled_ItShouldInitializeSource() public void Constructor_WhenCalled_ItShouldInitializeSource()
{ {
@@ -53,6 +63,41 @@ public class ImageContentTests : SerializationTest
action.Should().Throw<ArgumentException>(); action.Should().Throw<ArgumentException>();
} }
[Fact]
public void Constructor_WhenCalledWithCacheControl_ItShouldInitializeSourceAndCacheControl()
{
var expectedMediaType = "image/png";
var expectedData = "data";
var cacheControl = new EphemeralCacheControl();
var result = new ImageContent(expectedMediaType, expectedData, cacheControl);
result.Source.Should().BeEquivalentTo(new ImageSource(expectedMediaType, expectedData));
result.CacheControl.Should().BeSameAs(cacheControl);
}
[Fact]
public void Constructor_WhenCalledWithCacheControlAndMediatTypeIsNull_ItShouldThrowArgumentNullException()
{
var expectedData = "data";
var cacheControl = new EphemeralCacheControl();
var action = () => new ImageContent(null!, expectedData, cacheControl);
action.Should().Throw<ArgumentNullException>();
}
[Fact]
public void Constructor_WhenCalledWithCacheControlAndDataIsNull_ItShouldThrowArgumentNullException()
{
var expectedMediaType = "image/png";
var cacheControl = new EphemeralCacheControl();
var action = () => new ImageContent(expectedMediaType, null!, cacheControl);
action.Should().Throw<ArgumentNullException>();
}
[Fact] [Fact]
public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape() public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape()
{ {
@@ -63,6 +108,16 @@ public class ImageContentTests : SerializationTest
JsonAssert.Equal(_testJson, actual); JsonAssert.Equal(_testJson, actual);
} }
[Fact]
public void JsonSerialization_WhenSerializedWithCacheControl_ItShouldHaveExpectedShape()
{
var content = new ImageContent("image/png", "data", new EphemeralCacheControl());
var actual = Serialize(content);
JsonAssert.Equal(_testJsonWithCacheControl, actual);
}
[Fact] [Fact]
public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedShape() public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedShape()
{ {
@@ -3,6 +3,11 @@ namespace AnthropicClient.Tests.Unit.Models;
public class TextContentTests : SerializationTest public class TextContentTests : SerializationTest
{ {
private readonly string _testJson = @"{ ""text"": ""text"", ""type"": ""text"" }"; private readonly string _testJson = @"{ ""text"": ""text"", ""type"": ""text"" }";
private readonly string _testJsonWithCacheControl = @"{
""text"": ""text"",
""cache_control"": { ""type"": ""ephemeral"" },
""type"": ""text""
}";
[Fact] [Fact]
public void Constructor_WhenCalled_ItShouldInitializeText() public void Constructor_WhenCalled_ItShouldInitializeText()
@@ -14,6 +19,18 @@ public class TextContentTests : SerializationTest
result.Text.Should().Be(expectedText); result.Text.Should().Be(expectedText);
} }
[Fact]
public void Constructor_WhenCalledWithCacheControl_ItShouldInitializeProperties()
{
var expectedText = "text";
var cacheControl = new EphemeralCacheControl();
var result = new TextContent(expectedText, cacheControl);
result.Text.Should().Be(expectedText);
result.CacheControl.Should().BeSameAs(cacheControl);
}
[Fact] [Fact]
public void Constructor_WhenCalledAndTextIsNull_ItShouldThrowArgumentNullException() public void Constructor_WhenCalledAndTextIsNull_ItShouldThrowArgumentNullException()
{ {
@@ -22,6 +39,16 @@ public class TextContentTests : SerializationTest
action.Should().Throw<ArgumentNullException>(); action.Should().Throw<ArgumentNullException>();
} }
[Fact]
public void Constructor_WhenCalledWithCacheControlAndTextIsNull_ItShouldThrowArgumentNullException()
{
var cacheControl = new EphemeralCacheControl();
var action = () => new TextContent(null!, cacheControl);
action.Should().Throw<ArgumentNullException>();
}
[Fact] [Fact]
public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape() public void JsonSerialization_WhenSerialized_ItShouldHaveExpectedShape()
{ {
@@ -32,6 +59,16 @@ public class TextContentTests : SerializationTest
JsonAssert.Equal(_testJson, actual); JsonAssert.Equal(_testJson, actual);
} }
[Fact]
public void JsonSerialization_WhenSerializedWithCacheControl_ItShouldHaveExpectedShape()
{
var content = new TextContent("text", new EphemeralCacheControl());
var actual = Serialize(content);
JsonAssert.Equal(_testJsonWithCacheControl, actual);
}
[Fact] [Fact]
public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedShape() public void JsonDeserialization_WhenDeserialized_ItShouldHaveExpectedShape()
{ {