forked from dotnet/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIJsonSchemaTransformCacheTests.cs
More file actions
81 lines (67 loc) · 3.34 KB
/
Copy pathAIJsonSchemaTransformCacheTests.cs
File metadata and controls
81 lines (67 loc) · 3.34 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Text.Json;
using Xunit;
namespace Microsoft.Extensions.AI.Utilities;
public static class AIJsonSchemaTransformCacheTests
{
[Fact]
public static void NullOptions_ThrowsArgumentNullException()
{
Assert.Throws<ArgumentNullException>("transformOptions", () => new AIJsonSchemaTransformCache(transformOptions: null!));
}
[Fact]
public static void EmptyOptions_ThrowsArgumentException()
{
Assert.Throws<ArgumentException>("transformOptions", () => new AIJsonSchemaTransformCache(transformOptions: new()));
}
[Fact]
public static void TransformOptions_ReturnsExpectedValue()
{
AIJsonSchemaTransformOptions options = new() { ConvertBooleanSchemas = true };
AIJsonSchemaTransformCache cache = new(options);
Assert.Same(options, cache.TransformOptions);
}
[Fact]
public static void NullFunction_ThrowsArgumentNullException()
{
AIJsonSchemaTransformCache cache = new(new() { ConvertBooleanSchemas = true });
Assert.Throws<ArgumentNullException>("function", () => cache.GetOrCreateTransformedSchema(function: null!));
}
[Fact]
public static void NullResponseFormat_ThrowsArgumentNullException()
{
AIJsonSchemaTransformCache cache = new(new() { ConvertBooleanSchemas = true });
Assert.Throws<ArgumentNullException>("responseFormat", () => cache.GetOrCreateTransformedSchema(responseFormat: null!));
}
[Fact]
public static void FunctionSchema_ReturnsExpectedResults()
{
AIJsonSchemaTransformCache cache = new(new() { TransformSchemaNode = (_, node) => { node.AsObject().Add("myAwesomeKeyword", 42); return node; } });
AIFunction func = AIFunctionFactory.Create((int x, int y) => x + y);
JsonElement transformedSchema = cache.GetOrCreateTransformedSchema(func);
Assert.True(transformedSchema.TryGetProperty("myAwesomeKeyword", out _));
JsonElement transformedSchema2 = cache.GetOrCreateTransformedSchema(func);
Assert.Equal(transformedSchema, transformedSchema2);
}
[Fact]
public static void ChatResponseFormat_ReturnsExpectedResults()
{
AIJsonSchemaTransformCache cache = new(new() { TransformSchemaNode = (_, node) => { node.AsObject().Add("myAwesomeKeyword", 42); return node; } });
JsonElement schema = JsonDocument.Parse("{}").RootElement;
ChatResponseFormatJson responseFormat = ChatResponseFormat.ForJsonSchema(schema);
JsonElement? transformedSchema = cache.GetOrCreateTransformedSchema(responseFormat);
Assert.NotNull(transformedSchema);
Assert.True(transformedSchema.Value.TryGetProperty("myAwesomeKeyword", out _));
JsonElement? transformedSchema2 = cache.GetOrCreateTransformedSchema(responseFormat);
Assert.Equal(transformedSchema, transformedSchema2);
}
[Fact]
public static void ChatResponseFormat_NullFormatReturnsNullSchema()
{
AIJsonSchemaTransformCache cache = new(new() { TransformSchemaNode = (_, node) => { node.AsObject().Add("myAwesomeKeyword", 42); return node; } });
JsonElement? transformedSchema = cache.GetOrCreateTransformedSchema(ChatResponseFormat.Json);
Assert.Null(transformedSchema);
}
}