|
| 1 | +using System.Text.Json; |
| 2 | +using TestBuildingBlocks; |
| 3 | +using Xunit; |
| 4 | +using Xunit.Abstractions; |
| 5 | +using RequestType = OpenApiTests.AttributeTypes.CapabilitiesUtils.RequestType; |
| 6 | + |
| 7 | +namespace OpenApiTests.AttributeTypes; |
| 8 | + |
| 9 | +public sealed class RelationshipCapabilitiesTests : IClassFixture<OpenApiTestContext<AttributeTypesStartup, AttributeTypesDbContext>> |
| 10 | +{ |
| 11 | + private static readonly Dictionary<RequestType, string> SchemaPathByRequestType = new() |
| 12 | + { |
| 13 | + { RequestType.Response, "components.schemas.relationshipsInBookResponse.allOf[1].properties" }, |
| 14 | + { RequestType.Create, "components.schemas.relationshipsInCreateBookRequest.allOf[1].properties" }, |
| 15 | + { RequestType.Update, "components.schemas.relationshipsInUpdateBookRequest.allOf[1].properties" } |
| 16 | + }; |
| 17 | + |
| 18 | + private static readonly Dictionary<string, List<string>> BookModelRelsByCapability = new() |
| 19 | + { |
| 20 | + { "AllowView", ["author"] }, |
| 21 | + { "AllowSet", ["reviews"] } |
| 22 | + }; |
| 23 | + |
| 24 | + private static readonly Dictionary<RequestType, string> CapabilitiesByRequestType = new() |
| 25 | + { |
| 26 | + { RequestType.Response, "AllowView" }, |
| 27 | + { RequestType.Create, "AllowSet" }, |
| 28 | + { RequestType.Update, "AllowSet" } |
| 29 | + }; |
| 30 | + |
| 31 | + private readonly OpenApiTestContext<AttributeTypesStartup, AttributeTypesDbContext> _testContext; |
| 32 | + |
| 33 | + public RelationshipCapabilitiesTests( |
| 34 | + OpenApiTestContext<AttributeTypesStartup, AttributeTypesDbContext> testContext, |
| 35 | + ITestOutputHelper output) |
| 36 | + { |
| 37 | + _testContext = testContext; |
| 38 | + _testContext.UseController<BooksController>(); |
| 39 | + _testContext.SetTestOutputHelper(output); |
| 40 | + } |
| 41 | + |
| 42 | + [Theory] |
| 43 | + [InlineData(RequestType.Response)] |
| 44 | + [InlineData(RequestType.Create)] |
| 45 | + [InlineData(RequestType.Update)] |
| 46 | + public async Task Generated_Schema_Includes_Only_Expected_Relationships_For_Request_Type_Async(RequestType requestType) |
| 47 | + { |
| 48 | + // Arrange |
| 49 | + JsonElement document = await _testContext.GetSwaggerDocumentAsync(); |
| 50 | + JsonElement properties = document.Should().ContainPath(SchemaPathByRequestType[requestType]); |
| 51 | + string selectedCapability = CapabilitiesByRequestType[requestType]; |
| 52 | + |
| 53 | + IList<string> expected = BookModelRelsByCapability[selectedCapability]; |
| 54 | + |
| 55 | + IList<string> unexpected = BookModelRelsByCapability.SelectMany(kvp => kvp.Value) |
| 56 | + .Except(expected).ToList(); |
| 57 | + |
| 58 | + // Assert |
| 59 | + foreach (string relName in expected) |
| 60 | + { |
| 61 | + properties.Should().ContainProperty(relName); |
| 62 | + } |
| 63 | + |
| 64 | + foreach (string relName in unexpected) |
| 65 | + { |
| 66 | + properties.Should().NotContainPath($"properties.{relName}"); |
| 67 | + } |
| 68 | + } |
| 69 | +} |
0 commit comments