Skip to content

Commit

Permalink
Merge pull request #113 from SedativeEffect/feature/C-SHARP-207-metho…
Browse files Browse the repository at this point in the history
…d-tags

test: add tests
  • Loading branch information
BigDaddy1337 authored Aug 1, 2024
2 parents 179f2db + 9c6ef41 commit 2c0dc73
Showing 1 changed file with 81 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ApiDescriptionGroup>
{
new(null, new[] { apiDescription1 }),
new(null, new[] { apiDescription2 }),
new(null, new[] { apiDescription3 }),
},
0))
.Verifiable();

var result = documentGeneratorMock.Object.GetControllersTags();
var expected = new Dictionary<string, OpenRpcTag>()
{
{ "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<string, OpenRpcTag>()
{
{ "json_rpc", new OpenRpcTag("json_rpc") },
};
var result = documentGeneratorMock.Object.GetMethodTags(controllerName, expected);

result.Should().HaveCount(1);
result.Single().Keywords.Single().Should().BeOfType<RefKeyword>();
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<ApiDescriptionGroup>
{
new(null, new[] { apiDescription1 })
},
0))
.Verifiable();

var method1 = new OpenRpcMethod("a"){ Tags = new List<JsonSchema> { new JsonSchemaBuilder().Ref($"#/components/tags/json_rpc").Build() } };
var tags = new Dictionary<string, OpenRpcTag>
{
{ "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()
{
Expand Down Expand Up @@ -835,14 +914,15 @@ 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
{
EndpointMetadata = new List<object>
{
new JsonRpcControllerAttribute()
},
ControllerName = controllerName,

Check warning on line 925 in src/tests/Tochka.JsonRpc.OpenRpc.Tests/Services/OpenRpcDocumentGeneratorTests.cs

View workflow job for this annotation

GitHub Actions / test

Possible null reference assignment.
MethodInfo = action?.Method ?? ((Action) ValidMethod).Method
},
Properties =
Expand Down

0 comments on commit 2c0dc73

Please sign in to comment.