Skip to content

Commit e85c38e

Browse files
author
Chris Santero
committed
refactor model manager
1 parent 159fbc3 commit e85c38e

14 files changed

Lines changed: 589 additions & 474 deletions

File tree

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: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public void FindsIdNamedId()
3535
{
3636
// Arrange
3737
var mm = new ModelManager(new PluralizationService());
38+
mm.RegisterResourceType(typeof(Author));
3839

3940
// Act
4041
PropertyInfo idprop = mm.GetIdProperty(typeof(Author));
@@ -44,27 +45,30 @@ public void FindsIdNamedId()
4445
}
4546

4647
[TestMethod]
47-
[ExpectedException(typeof(InvalidOperationException))]
48-
public void DoesntFindMissingId()
48+
public void Cant_register_model_with_missing_id()
4949
{
5050
// Arrange
5151
var mm = new ModelManager(new PluralizationService());
5252

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

5656
// Assert
57-
Assert.Fail("An InvalidOperationException should be thrown and we shouldn't get here!");
57+
action.ShouldThrow<InvalidOperationException>()
58+
.Which.Message.Should()
59+
.Be("Unable to determine Id property for type JSONAPI.Tests.Core.ModelManagerTests+InvalidModel");
5860
}
5961

6062
[TestMethod]
6163
public void FindsIdFromAttribute()
6264
{
6365
// Arrange
6466
var mm = new ModelManager(new PluralizationService());
67+
mm.RegisterResourceType(typeof(CustomIdModel));
6568

6669
// Act
6770
PropertyInfo idprop = mm.GetIdProperty(typeof(CustomIdModel));
71+
6872
// Assert
6973
Assert.AreSame(typeof(CustomIdModel).GetProperty("Uuid"), idprop);
7074
}
@@ -194,17 +198,22 @@ public void GetPropertyForJsonKeyTest()
194198
var pluralizationService = new PluralizationService();
195199
var mm = new ModelManager(pluralizationService);
196200
Type authorType = typeof(Author);
201+
mm.RegisterResourceType(authorType);
197202

198203
// Act
199204
var idProp = mm.GetPropertyForJsonKey(authorType, "id");
200205
var nameProp = mm.GetPropertyForJsonKey(authorType, "name");
201206
var postsProp = mm.GetPropertyForJsonKey(authorType, "posts");
202207

203208
// Assert
204-
Assert.AreSame(authorType.GetProperty("Id"), idProp);
205-
Assert.AreSame(authorType.GetProperty("Name"), nameProp);
206-
Assert.AreSame(authorType.GetProperty("Posts"), postsProp);
209+
idProp.Property.Should().BeSameAs(authorType.GetProperty("Id"));
210+
idProp.Should().BeOfType<FieldModelProperty>();
211+
212+
nameProp.Property.Should().BeSameAs(authorType.GetProperty("Name"));
213+
nameProp.Should().BeOfType<FieldModelProperty>();
207214

215+
postsProp.Property.Should().BeSameAs(authorType.GetProperty("Posts"));
216+
postsProp.Should().BeOfType<RelationshipModelProperty>();
208217
}
209218

210219
[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)