|
| 1 | +using System.Linq; |
| 2 | +using System.Net; |
| 3 | +using System.Net.Http; |
| 4 | +using System.Text.Json.Serialization; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using FluentAssertions; |
| 7 | +using FluentAssertions.Extensions; |
| 8 | +using JsonApiDotNetCore.Configuration; |
| 9 | +using JsonApiDotNetCore.Serialization.Objects; |
| 10 | +using Microsoft.Extensions.DependencyInjection; |
| 11 | +using TestBuildingBlocks; |
| 12 | +using Xunit; |
| 13 | + |
| 14 | +namespace JsonApiDotNetCoreTests.IntegrationTests.QueryStrings |
| 15 | +{ |
| 16 | + public sealed class SerializerIgnoreConditionTests : IntegrationTestContext<TestableStartup<QueryStringDbContext>, QueryStringDbContext> |
| 17 | + { |
| 18 | + private readonly QueryStringFakers _fakers = new(); |
| 19 | + |
| 20 | + public SerializerIgnoreConditionTests() |
| 21 | + { |
| 22 | + UseController<CalendarsController>(); |
| 23 | + } |
| 24 | + |
| 25 | + [Theory] |
| 26 | + [InlineData(JsonIgnoreCondition.Never, true, true)] |
| 27 | + [InlineData(JsonIgnoreCondition.WhenWritingDefault, false, false)] |
| 28 | + [InlineData(JsonIgnoreCondition.WhenWritingNull, false, true)] |
| 29 | + public async Task Applies_configuration_for_ignore_condition(JsonIgnoreCondition configurationValue, bool expectNullValueInDocument, |
| 30 | + bool expectDefaultValueInDocument) |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var options = (JsonApiOptions)Factory.Services.GetRequiredService<IJsonApiOptions>(); |
| 34 | + options.SerializerOptions.DefaultIgnoreCondition = configurationValue; |
| 35 | + |
| 36 | + Calendar calendar = _fakers.Calendar.Generate(); |
| 37 | + calendar.TimeZone = null; |
| 38 | + calendar.DefaultAppointmentDurationInMinutes = default; |
| 39 | + calendar.ShowWeekNumbers = true; |
| 40 | + calendar.Appointments = _fakers.Appointment.Generate(1).ToHashSet(); |
| 41 | + calendar.Appointments.Single().Title = null; |
| 42 | + calendar.Appointments.Single().StartTime = default; |
| 43 | + calendar.Appointments.Single().EndTime = 1.January(2001); |
| 44 | + |
| 45 | + await RunOnDatabaseAsync(async dbContext => |
| 46 | + { |
| 47 | + dbContext.Calendars.Add(calendar); |
| 48 | + await dbContext.SaveChangesAsync(); |
| 49 | + }); |
| 50 | + |
| 51 | + string route = $"/calendars/{calendar.StringId}?include=appointments"; |
| 52 | + |
| 53 | + // Act |
| 54 | + (HttpResponseMessage httpResponse, Document responseDocument) = await ExecuteGetAsync<Document>(route); |
| 55 | + |
| 56 | + // Assert |
| 57 | + httpResponse.Should().HaveStatusCode(HttpStatusCode.OK); |
| 58 | + |
| 59 | + responseDocument.SingleData.Should().NotBeNull(); |
| 60 | + responseDocument.Included.Should().HaveCount(1); |
| 61 | + |
| 62 | + if (expectNullValueInDocument) |
| 63 | + { |
| 64 | + responseDocument.SingleData.Attributes.Should().ContainKey("timeZone"); |
| 65 | + responseDocument.Included[0].Attributes.Should().ContainKey("title"); |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + responseDocument.SingleData.Attributes.Should().NotContainKey("timeZone"); |
| 70 | + responseDocument.Included[0].Attributes.Should().NotContainKey("title"); |
| 71 | + } |
| 72 | + |
| 73 | + if (expectDefaultValueInDocument) |
| 74 | + { |
| 75 | + responseDocument.SingleData.Attributes.Should().ContainKey("defaultAppointmentDurationInMinutes"); |
| 76 | + responseDocument.Included[0].Attributes.Should().ContainKey("startTime"); |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + responseDocument.SingleData.Attributes.Should().NotContainKey("defaultAppointmentDurationInMinutes"); |
| 81 | + responseDocument.Included[0].Attributes.Should().NotContainKey("startTime"); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | +} |
0 commit comments