1
1
using System ;
2
+ using JSONAPI . Attributes ;
2
3
using Microsoft . VisualStudio . TestTools . UnitTesting ;
3
4
using JSONAPI . Core ;
4
5
using JSONAPI . Tests . Models ;
5
6
using System . Reflection ;
6
7
using System . Collections . Generic ;
7
8
using System . Collections ;
8
9
using FluentAssertions ;
10
+ using Newtonsoft . Json ;
9
11
10
12
namespace JSONAPI . Tests . Core
11
13
{
@@ -19,7 +21,7 @@ private class InvalidModel // No Id discernable!
19
21
20
22
private class CustomIdModel
21
23
{
22
- [ JSONAPI . Attributes . UseAsId ]
24
+ [ UseAsId ]
23
25
public Guid Uuid { get ; set ; }
24
26
25
27
public string Data { get ; set ; }
@@ -30,11 +32,32 @@ private class DerivedPost : Post
30
32
31
33
}
32
34
35
+ private class Band
36
+ {
37
+ [ UseAsId ]
38
+ public string BandName { get ; set ; }
39
+
40
+ [ JsonProperty ( "THE-GENRE" ) ]
41
+ public string Genre { get ; set ; }
42
+ }
43
+
44
+ private class Salad
45
+ {
46
+ public string Id { get ; set ; }
47
+
48
+ [ JsonProperty ( "salad-type" ) ]
49
+ public string TheSaladType { get ; set ; }
50
+
51
+ [ JsonProperty ( "salad-type" ) ]
52
+ public string AnotherSaladType { get ; set ; }
53
+ }
54
+
33
55
[ TestMethod ]
34
56
public void FindsIdNamedId ( )
35
57
{
36
58
// Arrange
37
59
var mm = new ModelManager ( new PluralizationService ( ) ) ;
60
+ mm . RegisterResourceType ( typeof ( Author ) ) ;
38
61
39
62
// Act
40
63
PropertyInfo idprop = mm . GetIdProperty ( typeof ( Author ) ) ;
@@ -44,27 +67,30 @@ public void FindsIdNamedId()
44
67
}
45
68
46
69
[ TestMethod ]
47
- [ ExpectedException ( typeof ( InvalidOperationException ) ) ]
48
- public void DoesntFindMissingId ( )
70
+ public void Cant_register_model_with_missing_id ( )
49
71
{
50
72
// Arrange
51
73
var mm = new ModelManager ( new PluralizationService ( ) ) ;
52
74
53
75
// Act
54
- PropertyInfo idprop = mm . GetIdProperty ( typeof ( InvalidModel ) ) ;
76
+ Action action = ( ) => mm . RegisterResourceType ( typeof ( InvalidModel ) ) ;
55
77
56
78
// Assert
57
- Assert . Fail ( "An InvalidOperationException should be thrown and we shouldn't get here!" ) ;
79
+ action . ShouldThrow < InvalidOperationException > ( )
80
+ . Which . Message . Should ( )
81
+ . Be ( "Unable to determine Id property for type `invalid-models`." ) ;
58
82
}
59
83
60
84
[ TestMethod ]
61
85
public void FindsIdFromAttribute ( )
62
86
{
63
87
// Arrange
64
88
var mm = new ModelManager ( new PluralizationService ( ) ) ;
89
+ mm . RegisterResourceType ( typeof ( CustomIdModel ) ) ;
65
90
66
91
// Act
67
92
PropertyInfo idprop = mm . GetIdProperty ( typeof ( CustomIdModel ) ) ;
93
+
68
94
// Assert
69
95
Assert . AreSame ( typeof ( CustomIdModel ) . GetProperty ( "Uuid" ) , idprop ) ;
70
96
}
@@ -176,9 +202,9 @@ public void GetJsonKeyForPropertyTest()
176
202
var mm = new ModelManager ( pluralizationService ) ;
177
203
178
204
// Act
179
- var idKey = mm . GetJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Id" ) ) ;
180
- var nameKey = mm . GetJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Name" ) ) ;
181
- var postsKey = mm . GetJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Posts" ) ) ;
205
+ var idKey = mm . CalculateJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Id" ) ) ;
206
+ var nameKey = mm . CalculateJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Name" ) ) ;
207
+ var postsKey = mm . CalculateJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Posts" ) ) ;
182
208
183
209
// Assert
184
210
Assert . AreEqual ( "id" , idKey ) ;
@@ -194,17 +220,70 @@ public void GetPropertyForJsonKeyTest()
194
220
var pluralizationService = new PluralizationService ( ) ;
195
221
var mm = new ModelManager ( pluralizationService ) ;
196
222
Type authorType = typeof ( Author ) ;
223
+ mm . RegisterResourceType ( authorType ) ;
197
224
198
225
// Act
199
226
var idProp = mm . GetPropertyForJsonKey ( authorType , "id" ) ;
200
227
var nameProp = mm . GetPropertyForJsonKey ( authorType , "name" ) ;
201
228
var postsProp = mm . GetPropertyForJsonKey ( authorType , "posts" ) ;
202
229
203
230
// Assert
204
- Assert . AreSame ( authorType . GetProperty ( "Id" ) , idProp ) ;
205
- Assert . AreSame ( authorType . GetProperty ( "Name" ) , nameProp ) ;
206
- Assert . AreSame ( authorType . GetProperty ( "Posts" ) , postsProp ) ;
231
+ idProp . Property . Should ( ) . BeSameAs ( authorType . GetProperty ( "Id" ) ) ;
232
+ idProp . Should ( ) . BeOfType < FieldModelProperty > ( ) ;
233
+
234
+ nameProp . Property . Should ( ) . BeSameAs ( authorType . GetProperty ( "Name" ) ) ;
235
+ nameProp . Should ( ) . BeOfType < FieldModelProperty > ( ) ;
236
+
237
+ postsProp . Property . Should ( ) . BeSameAs ( authorType . GetProperty ( "Posts" ) ) ;
238
+ postsProp . Should ( ) . BeOfType < RelationshipModelProperty > ( ) ;
239
+ }
240
+
241
+ [ TestMethod ]
242
+ public void GetPropertyForJsonKey_returns_correct_value_for_custom_id ( )
243
+ {
244
+ // Arrange
245
+ var pluralizationService = new PluralizationService ( ) ;
246
+ var mm = new ModelManager ( pluralizationService ) ;
247
+ Type bandType = typeof ( Band ) ;
248
+ mm . RegisterResourceType ( bandType ) ;
249
+
250
+ // Act
251
+ var idProp = mm . GetPropertyForJsonKey ( bandType , "id" ) ;
252
+
253
+ // Assert
254
+ idProp . Property . Should ( ) . BeSameAs ( bandType . GetProperty ( "BandName" ) ) ;
255
+ idProp . Should ( ) . BeOfType < FieldModelProperty > ( ) ;
256
+ }
257
+
258
+ [ TestMethod ]
259
+ public void GetPropertyForJsonKey_returns_correct_value_for_JsonProperty_attribute ( )
260
+ {
261
+ // Arrange
262
+ var pluralizationService = new PluralizationService ( ) ;
263
+ var mm = new ModelManager ( pluralizationService ) ;
264
+ Type bandType = typeof ( Band ) ;
265
+ mm . RegisterResourceType ( bandType ) ;
266
+
267
+ // Act
268
+ var prop = mm . GetPropertyForJsonKey ( bandType , "THE-GENRE" ) ;
269
+
270
+ // Assert
271
+ prop . Property . Should ( ) . BeSameAs ( bandType . GetProperty ( "Genre" ) ) ;
272
+ prop . Should ( ) . BeOfType < FieldModelProperty > ( ) ;
273
+ }
207
274
275
+ [ TestMethod ]
276
+ public void Cant_register_type_with_two_properties_with_the_same_name ( )
277
+ {
278
+ var pluralizationService = new PluralizationService ( ) ;
279
+ var mm = new ModelManager ( pluralizationService ) ;
280
+ Type saladType = typeof ( Salad ) ;
281
+
282
+ // Act
283
+ Action action = ( ) => mm . RegisterResourceType ( saladType ) ;
284
+
285
+ // Assert
286
+ action . ShouldThrow < InvalidOperationException > ( ) . Which . Message . Should ( ) . Be ( "The type `salads` already contains a property keyed at `salad-type`." ) ;
208
287
}
209
288
210
289
[ TestMethod ]
0 commit comments