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