-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathToolChoiceJsonConverterTests.cs
68 lines (56 loc) · 2.04 KB
/
ToolChoiceJsonConverterTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System.Text.Json;
using Cnblogs.DashScope.Core;
using FluentAssertions;
namespace Cnblogs.DashScope.Sdk.UnitTests;
public class ToolChoiceJsonConverterTests
{
private static readonly JsonSerializerOptions SerializerOptions =
new() { PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower };
[Theory]
[MemberData(nameof(Data))]
public void TextGenerationStopConvertor_Serialize_Success(ToolChoice? choice, string json)
{
// Arrange
var obj = new TestObj(choice);
// Act
var actual = JsonSerializer.Serialize(obj, SerializerOptions);
// Assert
actual.Should().Be(json);
}
[Theory]
[MemberData(nameof(Data))]
public void TextGenerationStopConvertor_Deserialize_Success(ToolChoice? choice, string json)
{
// Act
var obj = JsonSerializer.Deserialize<TestObj>(json, SerializerOptions);
// Assert
obj.Should().BeEquivalentTo(new TestObj(choice));
}
[Theory]
[MemberData(nameof(InvalidJson))]
public void TextGenerationStopConvertor_InvalidJson_Exception(string json)
{
// Act
var act = () => JsonSerializer.Deserialize<TestObj>(json, SerializerOptions);
// Assert
act.Should().Throw<JsonException>();
}
public record TestObj(ToolChoice? Choice);
public static TheoryData<ToolChoice?, string> Data
=> new()
{
{ ToolChoice.AutoChoice, """{"choice":"auto"}""" },
{ ToolChoice.NoneChoice, """{"choice":"none"}""" },
{ ToolChoice.FunctionChoice("weather"), """{"choice":{"type":"function","function":{"name":"weather"}}}""" },
{ null, """{"choice":null}""" }
};
public static TheoryData<string> InvalidJson
=> new()
{
"""{"choice":{}}""",
"""{"choice":"other"}""",
"""{"choice":{"type":"other"}}""",
"""{"choice":{"type":"other", "function":{"name": "weather"}}}""",
"""{"choice":{"type":"function", "function": "other"}}"""
};
}