33using JSONAPI . Core ;
44using JSONAPI . Tests . Models ;
55using System . Reflection ;
6+ using System . Collections . Generic ;
7+ using System . Collections ;
68
79namespace JSONAPI . Tests . Core
810{
911 [ TestClass ]
1012 public class ModelManagerTests
1113 {
12- private class InvalidModel
14+ private class InvalidModel // No Id discernable!
1315 {
1416 public string Data { get ; set ; }
1517 }
@@ -18,7 +20,7 @@ private class InvalidModel
1820 public void FindsIdNamedId ( )
1921 {
2022 // Arrange
21- var mm = new ModelManager ( ) ;
23+ var mm = new ModelManager ( new PluralizationService ( ) ) ;
2224
2325 // Act
2426 PropertyInfo idprop = mm . GetIdProperty ( typeof ( Author ) ) ;
@@ -32,13 +34,119 @@ public void FindsIdNamedId()
3234 public void DoesntFindMissingId ( )
3335 {
3436 // Arrange
35- var mm = new ModelManager ( ) ;
37+ var mm = new ModelManager ( new PluralizationService ( ) ) ;
3638
3739 // Act
3840 PropertyInfo idprop = mm . GetIdProperty ( typeof ( InvalidModel ) ) ;
3941
4042 // Assert
4143 Assert . Fail ( "An InvalidOperationException should be thrown and we shouldn't get here!" ) ;
4244 }
45+
46+ [ TestMethod ]
47+ public void GetJsonKeyForTypeTest ( )
48+ {
49+ // Arrange
50+ var pluralizationService = new PluralizationService ( ) ;
51+ var mm = new ModelManager ( pluralizationService ) ;
52+
53+ // Act
54+ var postKey = mm . GetJsonKeyForType ( typeof ( Post ) ) ;
55+ var authorKey = mm . GetJsonKeyForType ( typeof ( Author ) ) ;
56+ var commentKey = mm . GetJsonKeyForType ( typeof ( Comment ) ) ;
57+
58+ // Assert
59+ Assert . AreEqual ( "posts" , postKey ) ;
60+ Assert . AreEqual ( "authors" , authorKey ) ;
61+ Assert . AreEqual ( "comments" , commentKey ) ;
62+ }
63+
64+ [ TestMethod ]
65+ public void GetJsonKeyForPropertyTest ( )
66+ {
67+ // Arrange
68+ var pluralizationService = new PluralizationService ( ) ;
69+ var mm = new ModelManager ( pluralizationService ) ;
70+
71+ // Act
72+ var idKey = mm . GetJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Id" ) ) ;
73+ var nameKey = mm . GetJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Name" ) ) ;
74+ var postsKey = mm . GetJsonKeyForProperty ( typeof ( Author ) . GetProperty ( "Posts" ) ) ;
75+
76+ // Assert
77+ Assert . AreEqual ( "id" , idKey ) ;
78+ Assert . AreEqual ( "name" , nameKey ) ;
79+ Assert . AreEqual ( "posts" , postsKey ) ;
80+
81+ }
82+
83+ [ TestMethod ]
84+ public void GetPropertyForJsonKeyTest ( )
85+ {
86+ // Arrange
87+ var pluralizationService = new PluralizationService ( ) ;
88+ var mm = new ModelManager ( pluralizationService ) ;
89+ Type authorType = typeof ( Author ) . GetType ( ) ;
90+
91+ // Act
92+ var idProp = mm . GetPropertyForJsonKey ( authorType , "id" ) ;
93+ var nameProp = mm . GetPropertyForJsonKey ( authorType , "name" ) ;
94+ var postsProp = mm . GetPropertyForJsonKey ( authorType , "posts" ) ;
95+
96+ // Assert
97+ Assert . AreSame ( authorType . GetProperty ( "Id" ) , idProp ) ;
98+ Assert . AreSame ( authorType . GetProperty ( "Name" ) , nameProp ) ;
99+ Assert . AreSame ( authorType . GetProperty ( "Posts" ) , postsProp ) ;
100+
101+ }
102+
103+ [ TestMethod ]
104+ public void IsSerializedAsManyTest ( )
105+ {
106+ // Arrange
107+ var mm = new ModelManager ( new PluralizationService ( ) ) ;
108+
109+ // Act
110+ bool isArray = mm . IsSerializedAsMany ( typeof ( Post [ ] ) ) ;
111+ bool isGenericEnumerable = mm . IsSerializedAsMany ( typeof ( IEnumerable < Post > ) ) ;
112+ bool isString = mm . IsSerializedAsMany ( typeof ( string ) ) ;
113+ bool isAuthor = mm . IsSerializedAsMany ( typeof ( Author ) ) ;
114+ bool isNonGenericEnumerable = mm . IsSerializedAsMany ( typeof ( IEnumerable ) ) ;
115+
116+ // Assert
117+ Assert . IsTrue ( isArray ) ;
118+ Assert . IsTrue ( isGenericEnumerable ) ;
119+ Assert . IsFalse ( isString ) ;
120+ Assert . IsFalse ( isAuthor ) ;
121+ Assert . IsFalse ( isNonGenericEnumerable ) ;
122+ }
123+
124+ [ TestMethod ]
125+ public void GetElementTypeTest ( )
126+ {
127+ // Arrange
128+ var mm = new ModelManager ( new PluralizationService ( ) ) ;
129+
130+ // Act
131+ Type postTypeFromArray = mm . GetElementType ( typeof ( Post [ ] ) ) ;
132+ Type postTypeFromEnumerable = mm . GetElementType ( typeof ( IEnumerable < Post > ) ) ;
133+
134+ // Assert
135+ Assert . AreSame ( typeof ( Post ) , postTypeFromArray ) ;
136+ Assert . AreSame ( typeof ( Post ) , postTypeFromEnumerable ) ;
137+ }
138+
139+ [ TestMethod ]
140+ public void GetElementTypeInvalidArgumentTest ( )
141+ {
142+ // Arrange
143+ var mm = new ModelManager ( new PluralizationService ( ) ) ;
144+
145+ // Act
146+ Type x = mm . GetElementType ( typeof ( Author ) ) ;
147+
148+ // Assert
149+ Assert . IsNull ( x , "Return value of GetElementType should be null for a non-Many type argument!" ) ;
150+ }
43151 }
44152}
0 commit comments