3
3
using System . Linq ;
4
4
using System . Net . Http ;
5
5
using System . Threading . Tasks ;
6
+ using Bogus ;
6
7
using JsonApiDotNetCore . Extensions ;
7
8
using JsonApiDotNetCore . Models ;
8
9
using JsonApiDotNetCoreExample ;
15
16
using Newtonsoft . Json ;
16
17
using Xunit ;
17
18
using StringExtensions = JsonApiDotNetCoreExampleTests . Helpers . Extensions . StringExtensions ;
19
+ using Person = JsonApiDotNetCoreExample . Models . Person ;
20
+ using System . Net ;
21
+ using JsonApiDotNetCore . Serialization ;
18
22
19
23
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
20
24
{
@@ -23,11 +27,22 @@ public class SparseFieldSetTests
23
27
{
24
28
private TestFixture < TestStartup > _fixture ;
25
29
private readonly AppDbContext _dbContext ;
30
+ private Faker < Person > _personFaker ;
31
+ private Faker < TodoItem > _todoItemFaker ;
26
32
27
33
public SparseFieldSetTests ( TestFixture < TestStartup > fixture )
28
34
{
29
35
_fixture = fixture ;
30
36
_dbContext = fixture . GetService < AppDbContext > ( ) ;
37
+ _personFaker = new Faker < Person > ( )
38
+ . RuleFor ( p => p . FirstName , f => f . Name . FirstName ( ) )
39
+ . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) )
40
+ . RuleFor ( p => p . Age , f => f . Random . Int ( 20 , 80 ) ) ;
41
+
42
+ _todoItemFaker = new Faker < TodoItem > ( )
43
+ . RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
44
+ . RuleFor ( t => t . Ordinal , f => f . Random . Number ( 1 , 10 ) )
45
+ . RuleFor ( t => t . CreatedDate , f => f . Date . Past ( ) ) ;
31
46
}
32
47
33
48
[ Fact ]
@@ -98,5 +113,129 @@ public async Task Fields_Query_Selects_Sparse_Field_Sets()
98
113
Assert . Equal ( todoItem . Description , deserializeBody . Data . Attributes [ "description" ] ) ;
99
114
Assert . Equal ( todoItem . CreatedDate . ToString ( "G" ) , ( ( DateTime ) deserializeBody . Data . Attributes [ "created-date" ] ) . ToString ( "G" ) ) ;
100
115
}
116
+
117
+ [ Fact ]
118
+ public async Task Fields_Query_Selects_All_Fieldset_With_HasOne ( )
119
+ {
120
+ // arrange
121
+ var owner = _personFaker . Generate ( ) ;
122
+ var ordinal = _dbContext . TodoItems . Count ( ) ;
123
+ var todoItem = new TodoItem
124
+ {
125
+ Description = "s" ,
126
+ Ordinal = ordinal ,
127
+ CreatedDate = DateTime . Now ,
128
+ Owner = owner
129
+ } ;
130
+ _dbContext . TodoItems . Add ( todoItem ) ;
131
+ _dbContext . SaveChanges ( ) ;
132
+
133
+ var builder = new WebHostBuilder ( )
134
+ . UseStartup < Startup > ( ) ;
135
+ var httpMethod = new HttpMethod ( "GET" ) ;
136
+ var server = new TestServer ( builder ) ;
137
+ var client = server . CreateClient ( ) ;
138
+
139
+ var route = $ "/api/v1/todo-items?include=owner&fields[owner]=first-name,age";
140
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
141
+
142
+ // act
143
+ var response = await client . SendAsync ( request ) ;
144
+
145
+ // assert
146
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
147
+ var body = await response . Content . ReadAsStringAsync ( ) ;
148
+ var deserializedTodoItems = _fixture
149
+ . GetService < IJsonApiDeSerializer > ( )
150
+ . DeserializeList < TodoItem > ( body ) ;
151
+
152
+ foreach ( var item in deserializedTodoItems . Where ( i => i . Owner != null ) )
153
+ {
154
+ Assert . Null ( item . Owner . LastName ) ;
155
+ Assert . NotNull ( item . Owner . FirstName ) ;
156
+ Assert . NotEqual ( 0 , item . Owner . Age ) ;
157
+ }
158
+ }
159
+
160
+ [ Fact ]
161
+ public async Task Fields_Query_Selects_Fieldset_With_HasOne ( )
162
+ {
163
+ // arrange
164
+ var owner = _personFaker . Generate ( ) ;
165
+ var todoItem = new TodoItem
166
+ {
167
+ Description = "description" ,
168
+ Ordinal = 1 ,
169
+ CreatedDate = DateTime . Now ,
170
+ Owner = owner
171
+ } ;
172
+ _dbContext . TodoItems . Add ( todoItem ) ;
173
+ _dbContext . SaveChanges ( ) ;
174
+
175
+ var builder = new WebHostBuilder ( )
176
+ . UseStartup < Startup > ( ) ;
177
+ var httpMethod = new HttpMethod ( "GET" ) ;
178
+ var server = new TestServer ( builder ) ;
179
+ var client = server . CreateClient ( ) ;
180
+
181
+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ?include=owner&fields[owner]=first-name,age";
182
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
183
+
184
+ // act
185
+ var response = await client . SendAsync ( request ) ;
186
+
187
+ // assert
188
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
189
+ var body = await response . Content . ReadAsStringAsync ( ) ;
190
+ var deserializeBody = JsonConvert . DeserializeObject < Document > ( body ) ;
191
+
192
+ // check owner attributes
193
+ var included = deserializeBody . Included . First ( ) ;
194
+ Assert . Equal ( owner . StringId , included . Id ) ;
195
+ Assert . Equal ( owner . FirstName , included . Attributes [ "first-name" ] ) ;
196
+ Assert . Equal ( ( long ) owner . Age , included . Attributes [ "age" ] ) ;
197
+ Assert . Null ( included . Attributes [ "last-name" ] ) ;
198
+
199
+ }
200
+
201
+ [ Fact ]
202
+ public async Task Fields_Query_Selects_Fieldset_With_HasMany ( )
203
+ {
204
+ // arrange
205
+ var owner = _personFaker . Generate ( ) ;
206
+ var todoItems = _todoItemFaker . Generate ( 2 ) ;
207
+
208
+ owner . TodoItems = todoItems ;
209
+
210
+ _dbContext . People . Add ( owner ) ;
211
+ _dbContext . SaveChanges ( ) ;
212
+
213
+ var builder = new WebHostBuilder ( )
214
+ . UseStartup < Startup > ( ) ;
215
+ var httpMethod = new HttpMethod ( "GET" ) ;
216
+ var server = new TestServer ( builder ) ;
217
+ var client = server . CreateClient ( ) ;
218
+
219
+ var route = $ "/api/v1/people/{ owner . Id } ?include=todo-items&fields[todo-items]=description";
220
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
221
+
222
+ // act
223
+ var response = await client . SendAsync ( request ) ;
224
+
225
+ // assert
226
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
227
+ var body = await response . Content . ReadAsStringAsync ( ) ;
228
+ var deserializeBody = JsonConvert . DeserializeObject < Document > ( body ) ;
229
+
230
+ // check owner attributes
231
+ foreach ( var includedItem in deserializeBody . Included )
232
+ {
233
+ var todoItem = todoItems . FirstOrDefault ( i => i . StringId == includedItem . Id ) ;
234
+ Assert . NotNull ( todoItem ) ;
235
+ Assert . Equal ( todoItem . Description , includedItem . Attributes [ "description" ] ) ;
236
+ Assert . Equal ( default ( long ) , includedItem . Attributes [ "ordinal" ] ) ;
237
+ Assert . Equal ( default ( DateTime ) , ( DateTime ) includedItem . Attributes [ "created-date" ] ) ;
238
+ }
239
+ }
101
240
}
102
241
}
0 commit comments