openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
codegen/generator/test/Visitors/OpenAILibraryVisitorTests.cs
98lines · modecode
| 1 | using Microsoft.TypeSpec.Generator.ClientModel; |
| 2 | using Microsoft.TypeSpec.Generator.ClientModel.Providers; |
| 3 | using Microsoft.TypeSpec.Generator.Input; |
| 4 | using Microsoft.TypeSpec.Generator.Providers; |
| 5 | using NUnit.Framework; |
| 6 | using OpenAILibraryPlugin.Tests.Common; |
| 7 | using OpenAILibraryPlugin.Tests.TestHelpers; |
| 8 | using System.Linq; |
| 9 | |
| 10 | namespace OpenAILibraryPlugin.Tests.Visitors |
| 11 | { |
| 12 | [Category("Visitor")] |
| 13 | public class OpenAILibraryVisitorTests |
| 14 | { |
| 15 | [SetUp] |
| 16 | public void Setup() |
| 17 | { |
| 18 | MockHelpers.LoadMockGenerator(configurationJson: "{ \"package-name\": \"TestLibrary\" }"); |
| 19 | } |
| 20 | |
| 21 | // This test validates that the serialization is updated correctly for both dynamic and non-dynamic models. |
| 22 | [TestCase(true)] |
| 23 | [TestCase(false)] |
| 24 | public void TestVisitMethod_JsonModelWriteCore(bool isDynamicModel) |
| 25 | { |
| 26 | var visitor = new TestOpenAILibraryVisitor(); |
| 27 | |
| 28 | var inputType = InputFactory.Model("TestModel", "Samples", isDynamicModel: isDynamicModel, properties: [ |
| 29 | InputFactory.Property("cat", InputPrimitiveType.String), |
| 30 | InputFactory.Property("requiredDog", InputPrimitiveType.String, isRequired: true) |
| 31 | ]); |
| 32 | var model = ScmCodeModelGenerator.Instance.TypeFactory.CreateModel(inputType); |
| 33 | Assert.That(model, Is.Not.Null); |
| 34 | |
| 35 | var jsonWriteCoreMethod = model!.SerializationProviders |
| 36 | .OfType<MrwSerializationTypeDefinition>() |
| 37 | .FirstOrDefault()? |
| 38 | .Methods |
| 39 | .OfType<MethodProvider>() |
| 40 | .First(m => m.Signature.Name == "JsonModelWriteCore"); |
| 41 | Assert.That(jsonWriteCoreMethod, Is.Not.Null); |
| 42 | |
| 43 | // Invoke the visitor |
| 44 | jsonWriteCoreMethod = visitor.InvokeVisitMethod(jsonWriteCoreMethod!); |
| 45 | Assert.That(jsonWriteCoreMethod!.BodyStatements, Is.Not.Null); |
| 46 | |
| 47 | var methodBody = jsonWriteCoreMethod!.BodyStatements!.ToDisplayString(); |
| 48 | Assert.That(methodBody, Is.EqualTo(Helpers.GetExpectedFromFile(isDynamicModel.ToString()))); |
| 49 | } |
| 50 | |
| 51 | // This test validates that the serialization for known properties that should have additional conditions |
| 52 | // is updated correctly. |
| 53 | [TestCase(true)] |
| 54 | [TestCase(false)] |
| 55 | public void TestVisitMethod_JsonModelWriteCore_CustomConditions(bool isDynamicModel) |
| 56 | { |
| 57 | var visitor = new TestOpenAILibraryVisitor(); |
| 58 | |
| 59 | var inputType = InputFactory.Model("ChatCompletionOptions", "Samples", isDynamicModel: isDynamicModel, properties: [ |
| 60 | InputFactory.Property("model", InputPrimitiveType.String, isRequired: true), |
| 61 | ]); |
| 62 | var model = ScmCodeModelGenerator.Instance.TypeFactory.CreateModel(inputType); |
| 63 | Assert.That(model, Is.Not.Null); |
| 64 | |
| 65 | var jsonWriteCoreMethod = model!.SerializationProviders |
| 66 | .OfType<MrwSerializationTypeDefinition>() |
| 67 | .FirstOrDefault()? |
| 68 | .Methods |
| 69 | .OfType<MethodProvider>() |
| 70 | .First(m => m.Signature.Name == "JsonModelWriteCore"); |
| 71 | Assert.That(jsonWriteCoreMethod, Is.Not.Null); |
| 72 | |
| 73 | // Invoke the visitor |
| 74 | jsonWriteCoreMethod = visitor.InvokeVisitMethod(jsonWriteCoreMethod!); |
| 75 | Assert.That(jsonWriteCoreMethod!.BodyStatements, Is.Not.Null); |
| 76 | |
| 77 | var methodBody = jsonWriteCoreMethod!.BodyStatements!.ToDisplayString(); |
| 78 | Assert.That(methodBody, Is.EqualTo(Helpers.GetExpectedFromFile(isDynamicModel.ToString()))); |
| 79 | } |
| 80 | |
| 81 | private class TestOpenAILibraryVisitor : OpenAILibraryVisitor |
| 82 | { |
| 83 | public MethodProvider? InvokeVisitMethod(MethodProvider method) |
| 84 | { |
| 85 | return base.VisitMethod(method); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | private class TestTypeProvider : TypeProvider |
| 90 | { |
| 91 | protected override string BuildNamespace() => "Samples"; |
| 92 | |
| 93 | protected override string BuildRelativeFilePath() => $"{Name}.cs"; |
| 94 | |
| 95 | protected override string BuildName() => "TestModel"; |
| 96 | } |
| 97 | } |
| 98 | } |