From 9c6ef416d11f043db9003dc8ad4ce70e524acfd0 Mon Sep 17 00:00:00 2001 From: Denis Kozak Date: Thu, 1 Aug 2024 18:47:59 +0300 Subject: [PATCH] test: add tests --- .../Services/OpenRpcDocumentGeneratorTests.cs | 82 ++++++++++++++++++- 1 file changed, 81 insertions(+), 1 deletion(-) diff --git a/src/tests/Tochka.JsonRpc.OpenRpc.Tests/Services/OpenRpcDocumentGeneratorTests.cs b/src/tests/Tochka.JsonRpc.OpenRpc.Tests/Services/OpenRpcDocumentGeneratorTests.cs index 776f232..d1926eb 100644 --- a/src/tests/Tochka.JsonRpc.OpenRpc.Tests/Services/OpenRpcDocumentGeneratorTests.cs +++ b/src/tests/Tochka.JsonRpc.OpenRpc.Tests/Services/OpenRpcDocumentGeneratorTests.cs @@ -93,6 +93,85 @@ public void Generate_UseArgsAndInternalMethods() documentGeneratorMock.Verify(); } + [Test] + public void GetControllersTagsReturnsTagsWithControllerName() + { + var apiDescription1 = GetValidDescription(controllerName: "JsonRpc"); + var apiDescription2 = GetValidDescription(controllerName: "Test"); + var apiDescription3 = GetValidDescription(controllerName: string.Empty); + apiDescriptionsProviderMock.Setup(static p => p.ApiDescriptionGroups) + .Returns(new ApiDescriptionGroupCollection(new List + { + new(null, new[] { apiDescription1 }), + new(null, new[] { apiDescription2 }), + new(null, new[] { apiDescription3 }), + }, + 0)) + .Verifiable(); + + var result = documentGeneratorMock.Object.GetControllersTags(); + var expected = new Dictionary() + { + { "json_rpc", new OpenRpcTag("json_rpc") }, + { "test", new OpenRpcTag("test") }, + }; + + result.Should().BeEquivalentTo(expected, static options => options.WithStrictOrdering()); + apiDescriptionsProviderMock.Verify(); + documentGeneratorMock.Verify(); + } + + [Test] + public void GetMethodTagsReturnNewRefSchemaTag() + { + var apiDescription1 = GetValidDescription(controllerName: "JsonRpc"); + var controllerName = (apiDescription1.ActionDescriptor as ControllerActionDescriptor).ControllerName; + var expected = new Dictionary() + { + { "json_rpc", new OpenRpcTag("json_rpc") }, + }; + var result = documentGeneratorMock.Object.GetMethodTags(controllerName, expected); + + result.Should().HaveCount(1); + result.Single().Keywords.Single().Should().BeOfType(); + apiDescriptionsProviderMock.Verify(); + documentGeneratorMock.Verify(); + } + + [Test] + public void GetMethodsReturnsMethodWithTags() + { + var apiDescription1 = GetValidDescription(controllerName: "JsonRpc"); + + var host = new Uri(Host); + apiDescriptionsProviderMock.Setup(static p => p.ApiDescriptionGroups) + .Returns(new ApiDescriptionGroupCollection(new List + { + new(null, new[] { apiDescription1 }) + }, + 0)) + .Verifiable(); + + var method1 = new OpenRpcMethod("a"){ Tags = new List { new JsonSchemaBuilder().Ref($"#/components/tags/json_rpc").Build() } }; + var tags = new Dictionary + { + { "json_rpc", new OpenRpcTag("json_rpc") } + }; + documentGeneratorMock.Setup(g => g.GetMethod(apiDescription1, host, tags)) + .Returns(method1) + .Verifiable(); + openRpcOptions.DocInclusionPredicate = static (_, _) => true; + + var result = documentGeneratorMock.Object.GetMethods(DocumentName, host, tags); + var expected = new[] + { + method1, + }; + result.Should().BeEquivalentTo(expected, static options => options.WithStrictOrdering()); + apiDescriptionsProviderMock.Verify(); + documentGeneratorMock.Verify(); + } + [Test] public void GetServers_ReturnsOneServerWithDefaultNameAndUrl() { @@ -835,7 +914,7 @@ public void CombineBindingStyles_BothObjectAndArray_ReturnEither() result.Should().Be(OpenRpcParamStructure.Either); } - private static ApiDescription GetValidDescription(Action? action = null) => new() + private static ApiDescription GetValidDescription(Action? action = null, string? controllerName = null) => new() { ActionDescriptor = new ControllerActionDescriptor { @@ -843,6 +922,7 @@ public void CombineBindingStyles_BothObjectAndArray_ReturnEither() { new JsonRpcControllerAttribute() }, + ControllerName = controllerName, MethodInfo = action?.Method ?? ((Action) ValidMethod).Method }, Properties =