Skip to content

Commit f7e0a30

Browse files
committed
Merge pull request #88 from csantero/mm-caches
Model manager changes
2 parents 159fbc3 + 6536a5d commit f7e0a30

File tree

14 files changed

+678
-491
lines changed

14 files changed

+678
-491
lines changed

JSONAPI.EntityFramework.Tests/Acceptance/Fixtures/Heterogeneous/Responses/GetSearchResultsResponse.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"text": "Comment 1",
1919
"created": "2015-01-31T14:30:00+00:00",
2020
"links": {
21-
"author": "403",
22-
"post": "201"
21+
"post": "201",
22+
"author": "403"
2323
}
2424
}
2525
]

JSONAPI.Tests/ActionFilters/EnableFilteringAttributeTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net;
55
using System.Net.Http;
66
using System.Net.Http.Formatting;
7+
using System.Web.Http;
78
using System.Web.Http.Controllers;
89
using System.Web.Http.Filters;
910
using FluentAssertions;
@@ -563,6 +564,8 @@ private HttpActionExecutedContext CreateActionExecutedContext(IModelManager mode
563564
private T[] GetArray<T>(string uri)
564565
{
565566
var modelManager = new ModelManager(new PluralizationService());
567+
modelManager.RegisterResourceType(typeof(Dummy));
568+
modelManager.RegisterResourceType(typeof(RelatedItemWithId));
566569

567570
var filter = new EnableFilteringAttribute(modelManager);
568571

@@ -1130,8 +1133,8 @@ public void Filters_by_missing_nullable_double_property()
11301133
[TestMethod]
11311134
public void Does_not_filter_unknown_type()
11321135
{
1133-
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?unknownTypeField=asdfasd");
1134-
returnedArray.Length.Should().Be(_fixtures.Count);
1136+
Action action = () => GetArray<Dummy>("http://api.example.com/dummies?unknownTypeField=asdfasd");
1137+
action.ShouldThrow<HttpResponseException>().Which.Response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
11351138
}
11361139

11371140
#endregion

JSONAPI.Tests/Core/MetadataManagerTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public void PropertyWasPresentTest()
1818
// Arrange
1919
var modelManager = new ModelManager(new PluralizationService());
2020
modelManager.RegisterResourceType(typeof(Post));
21+
modelManager.RegisterResourceType(typeof(Author));
2122
JsonApiFormatter formatter = new JsonApiFormatter(modelManager);
2223

2324
var p = (Post) formatter.ReadFromStreamAsync(typeof(Post), inputStream, null, null).Result;

JSONAPI.Tests/Core/ModelManagerTests.cs

Lines changed: 90 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
using System;
2+
using JSONAPI.Attributes;
23
using Microsoft.VisualStudio.TestTools.UnitTesting;
34
using JSONAPI.Core;
45
using JSONAPI.Tests.Models;
56
using System.Reflection;
67
using System.Collections.Generic;
78
using System.Collections;
89
using FluentAssertions;
10+
using Newtonsoft.Json;
911

1012
namespace JSONAPI.Tests.Core
1113
{
@@ -19,7 +21,7 @@ private class InvalidModel // No Id discernable!
1921

2022
private class CustomIdModel
2123
{
22-
[JSONAPI.Attributes.UseAsId]
24+
[UseAsId]
2325
public Guid Uuid { get; set; }
2426

2527
public string Data { get; set; }
@@ -30,11 +32,32 @@ private class DerivedPost : Post
3032

3133
}
3234

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+
3355
[TestMethod]
3456
public void FindsIdNamedId()
3557
{
3658
// Arrange
3759
var mm = new ModelManager(new PluralizationService());
60+
mm.RegisterResourceType(typeof(Author));
3861

3962
// Act
4063
PropertyInfo idprop = mm.GetIdProperty(typeof(Author));
@@ -44,27 +67,30 @@ public void FindsIdNamedId()
4467
}
4568

4669
[TestMethod]
47-
[ExpectedException(typeof(InvalidOperationException))]
48-
public void DoesntFindMissingId()
70+
public void Cant_register_model_with_missing_id()
4971
{
5072
// Arrange
5173
var mm = new ModelManager(new PluralizationService());
5274

5375
// Act
54-
PropertyInfo idprop = mm.GetIdProperty(typeof(InvalidModel));
76+
Action action = () => mm.RegisterResourceType(typeof(InvalidModel));
5577

5678
// 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`.");
5882
}
5983

6084
[TestMethod]
6185
public void FindsIdFromAttribute()
6286
{
6387
// Arrange
6488
var mm = new ModelManager(new PluralizationService());
89+
mm.RegisterResourceType(typeof(CustomIdModel));
6590

6691
// Act
6792
PropertyInfo idprop = mm.GetIdProperty(typeof(CustomIdModel));
93+
6894
// Assert
6995
Assert.AreSame(typeof(CustomIdModel).GetProperty("Uuid"), idprop);
7096
}
@@ -176,9 +202,9 @@ public void GetJsonKeyForPropertyTest()
176202
var mm = new ModelManager(pluralizationService);
177203

178204
// 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"));
182208

183209
// Assert
184210
Assert.AreEqual("id", idKey);
@@ -194,17 +220,70 @@ public void GetPropertyForJsonKeyTest()
194220
var pluralizationService = new PluralizationService();
195221
var mm = new ModelManager(pluralizationService);
196222
Type authorType = typeof(Author);
223+
mm.RegisterResourceType(authorType);
197224

198225
// Act
199226
var idProp = mm.GetPropertyForJsonKey(authorType, "id");
200227
var nameProp = mm.GetPropertyForJsonKey(authorType, "name");
201228
var postsProp = mm.GetPropertyForJsonKey(authorType, "posts");
202229

203230
// 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+
}
207274

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`.");
208287
}
209288

210289
[TestMethod]

JSONAPI.Tests/Data/NonStandardIdTest.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
{
44
"type": "non-standard-id-things",
55
"id": "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f",
6-
"uuid": "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f",
76
"data": "Swap"
87
}
98
]

JSONAPI.Tests/Json/JsonApiMediaFormatterTests.cs

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ public void Deserializes_collections_properly()
392392
// Arrange
393393
var modelManager = new ModelManager(new PluralizationService());
394394
modelManager.RegisterResourceType(typeof(Post));
395+
modelManager.RegisterResourceType(typeof(Author));
396+
modelManager.RegisterResourceType(typeof(Comment));
395397
var formatter = new JsonApiFormatter(modelManager);
396398

397399
// Act
@@ -536,7 +538,7 @@ public void DeserializeNonStandardIdTest()
536538

537539
[TestMethod]
538540
[DeploymentItem(@"Data\NonStandardIdTest.json")]
539-
public void DeserializeNonStandardIdWithIdOnly()
541+
public void DeserializeNonStandardId()
540542
{
541543
var modelManager = new ModelManager(new PluralizationService());
542544
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
@@ -554,28 +556,6 @@ public void DeserializeNonStandardIdWithIdOnly()
554556
things.Count.Should().Be(1);
555557
things.First().Uuid.Should().Be(new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"));
556558
}
557-
558-
[TestMethod]
559-
[DeploymentItem(@"Data\NonStandardIdTest.json")]
560-
public void DeserializeNonStandardIdWithoutId()
561-
{
562-
var modelManager = new ModelManager(new PluralizationService());
563-
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
564-
var formatter = new JsonApiFormatter(modelManager);
565-
string json = File.ReadAllText("NonStandardIdTest.json");
566-
json = Regex.Replace(json, @"""id"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*,", ""); // remove the uuid attribute
567-
var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json));
568-
569-
// Act
570-
IList<NonStandardIdThing> things;
571-
things = (IList<NonStandardIdThing>)formatter.ReadFromStreamAsync(typeof(NonStandardIdThing), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;
572-
573-
// Assert
574-
json.Should().NotContain("\"id\"", "The \"id\" attribute was supposed to be removed, test methodology problem!");
575-
things.Count.Should().Be(1);
576-
things.First().Uuid.Should().Be(new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"));
577-
578-
}
579559

580560
#endregion
581561

JSONAPI.Tests/Json/LinkTemplateTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void GetResourceWithLinkTemplateRelationship()
5252
{
5353
var modelManager = new ModelManager(new PluralizationService());
5454
modelManager.RegisterResourceType(typeof(Post));
55+
modelManager.RegisterResourceType(typeof(User));
5556
var formatter = new JsonApiFormatter(modelManager);
5657
var stream = new MemoryStream();
5758

0 commit comments

Comments
 (0)