Skip to content

Model manager changes #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 12, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
"text": "Comment 1",
"created": "2015-01-31T14:30:00+00:00",
"links": {
"author": "403",
"post": "201"
"post": "201",
"author": "403"
}
}
]
Expand Down
7 changes: 5 additions & 2 deletions JSONAPI.Tests/ActionFilters/EnableFilteringAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using FluentAssertions;
Expand Down Expand Up @@ -563,6 +564,8 @@ private HttpActionExecutedContext CreateActionExecutedContext(IModelManager mode
private T[] GetArray<T>(string uri)
{
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Dummy));
modelManager.RegisterResourceType(typeof(RelatedItemWithId));

var filter = new EnableFilteringAttribute(modelManager);

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

#endregion
Expand Down
1 change: 1 addition & 0 deletions JSONAPI.Tests/Core/MetadataManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public void PropertyWasPresentTest()
// Arrange
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Post));
modelManager.RegisterResourceType(typeof(Author));
JsonApiFormatter formatter = new JsonApiFormatter(modelManager);

var p = (Post) formatter.ReadFromStreamAsync(typeof(Post), inputStream, null, null).Result;
Expand Down
101 changes: 90 additions & 11 deletions JSONAPI.Tests/Core/ModelManagerTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using System;
using JSONAPI.Attributes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using JSONAPI.Core;
using JSONAPI.Tests.Models;
using System.Reflection;
using System.Collections.Generic;
using System.Collections;
using FluentAssertions;
using Newtonsoft.Json;

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

private class CustomIdModel
{
[JSONAPI.Attributes.UseAsId]
[UseAsId]
public Guid Uuid { get; set; }

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

}

private class Band
{
[UseAsId]
public string BandName { get; set; }

[JsonProperty("THE-GENRE")]
public string Genre { get; set; }
}

private class Salad
{
public string Id { get; set; }

[JsonProperty("salad-type")]
public string TheSaladType { get; set; }

[JsonProperty("salad-type")]
public string AnotherSaladType { get; set; }
}

[TestMethod]
public void FindsIdNamedId()
{
// Arrange
var mm = new ModelManager(new PluralizationService());
mm.RegisterResourceType(typeof(Author));

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

[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public void DoesntFindMissingId()
public void Cant_register_model_with_missing_id()
{
// Arrange
var mm = new ModelManager(new PluralizationService());

// Act
PropertyInfo idprop = mm.GetIdProperty(typeof(InvalidModel));
Action action = () => mm.RegisterResourceType(typeof(InvalidModel));

// Assert
Assert.Fail("An InvalidOperationException should be thrown and we shouldn't get here!");
action.ShouldThrow<InvalidOperationException>()
.Which.Message.Should()
.Be("Unable to determine Id property for type `invalid-models`.");
}

[TestMethod]
public void FindsIdFromAttribute()
{
// Arrange
var mm = new ModelManager(new PluralizationService());
mm.RegisterResourceType(typeof(CustomIdModel));

// Act
PropertyInfo idprop = mm.GetIdProperty(typeof(CustomIdModel));

// Assert
Assert.AreSame(typeof(CustomIdModel).GetProperty("Uuid"), idprop);
}
Expand Down Expand Up @@ -176,9 +202,9 @@ public void GetJsonKeyForPropertyTest()
var mm = new ModelManager(pluralizationService);

// Act
var idKey = mm.GetJsonKeyForProperty(typeof(Author).GetProperty("Id"));
var nameKey = mm.GetJsonKeyForProperty(typeof(Author).GetProperty("Name"));
var postsKey = mm.GetJsonKeyForProperty(typeof(Author).GetProperty("Posts"));
var idKey = mm.CalculateJsonKeyForProperty(typeof(Author).GetProperty("Id"));
var nameKey = mm.CalculateJsonKeyForProperty(typeof(Author).GetProperty("Name"));
var postsKey = mm.CalculateJsonKeyForProperty(typeof(Author).GetProperty("Posts"));

// Assert
Assert.AreEqual("id", idKey);
Expand All @@ -194,17 +220,70 @@ public void GetPropertyForJsonKeyTest()
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);
Type authorType = typeof(Author);
mm.RegisterResourceType(authorType);

// Act
var idProp = mm.GetPropertyForJsonKey(authorType, "id");
var nameProp = mm.GetPropertyForJsonKey(authorType, "name");
var postsProp = mm.GetPropertyForJsonKey(authorType, "posts");

// Assert
Assert.AreSame(authorType.GetProperty("Id"), idProp);
Assert.AreSame(authorType.GetProperty("Name"), nameProp);
Assert.AreSame(authorType.GetProperty("Posts"), postsProp);
idProp.Property.Should().BeSameAs(authorType.GetProperty("Id"));
idProp.Should().BeOfType<FieldModelProperty>();

nameProp.Property.Should().BeSameAs(authorType.GetProperty("Name"));
nameProp.Should().BeOfType<FieldModelProperty>();

postsProp.Property.Should().BeSameAs(authorType.GetProperty("Posts"));
postsProp.Should().BeOfType<RelationshipModelProperty>();
}

[TestMethod]
public void GetPropertyForJsonKey_returns_correct_value_for_custom_id()
{
// Arrange
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);
Type bandType = typeof(Band);
mm.RegisterResourceType(bandType);

// Act
var idProp = mm.GetPropertyForJsonKey(bandType, "id");

// Assert
idProp.Property.Should().BeSameAs(bandType.GetProperty("BandName"));
idProp.Should().BeOfType<FieldModelProperty>();
}

[TestMethod]
public void GetPropertyForJsonKey_returns_correct_value_for_JsonProperty_attribute()
{
// Arrange
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);
Type bandType = typeof(Band);
mm.RegisterResourceType(bandType);

// Act
var prop = mm.GetPropertyForJsonKey(bandType, "THE-GENRE");

// Assert
prop.Property.Should().BeSameAs(bandType.GetProperty("Genre"));
prop.Should().BeOfType<FieldModelProperty>();
}

[TestMethod]
public void Cant_register_type_with_two_properties_with_the_same_name()
{
var pluralizationService = new PluralizationService();
var mm = new ModelManager(pluralizationService);
Type saladType = typeof(Salad);

// Act
Action action = () => mm.RegisterResourceType(saladType);

// Assert
action.ShouldThrow<InvalidOperationException>().Which.Message.Should().Be("The type `salads` already contains a property keyed at `salad-type`.");
}

[TestMethod]
Expand Down
1 change: 0 additions & 1 deletion JSONAPI.Tests/Data/NonStandardIdTest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
{
"type": "non-standard-id-things",
"id": "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f",
"uuid": "0657fd6d-a4ab-43c4-84e5-0933c84b4f4f",
"data": "Swap"
}
]
Expand Down
26 changes: 3 additions & 23 deletions JSONAPI.Tests/Json/JsonApiMediaFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ public void Deserializes_collections_properly()
// Arrange
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Post));
modelManager.RegisterResourceType(typeof(Author));
modelManager.RegisterResourceType(typeof(Comment));
var formatter = new JsonApiFormatter(modelManager);

// Act
Expand Down Expand Up @@ -536,7 +538,7 @@ public void DeserializeNonStandardIdTest()

[TestMethod]
[DeploymentItem(@"Data\NonStandardIdTest.json")]
public void DeserializeNonStandardIdWithIdOnly()
public void DeserializeNonStandardId()
{
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
Expand All @@ -554,28 +556,6 @@ public void DeserializeNonStandardIdWithIdOnly()
things.Count.Should().Be(1);
things.First().Uuid.Should().Be(new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"));
}

[TestMethod]
[DeploymentItem(@"Data\NonStandardIdTest.json")]
public void DeserializeNonStandardIdWithoutId()
{
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(NonStandardIdThing));
var formatter = new JsonApiFormatter(modelManager);
string json = File.ReadAllText("NonStandardIdTest.json");
json = Regex.Replace(json, @"""id"":\s*""0657fd6d-a4ab-43c4-84e5-0933c84b4f4f""\s*,", ""); // remove the uuid attribute
var stream = new MemoryStream(System.Text.Encoding.ASCII.GetBytes(json));

// Act
IList<NonStandardIdThing> things;
things = (IList<NonStandardIdThing>)formatter.ReadFromStreamAsync(typeof(NonStandardIdThing), stream, (System.Net.Http.HttpContent)null, (System.Net.Http.Formatting.IFormatterLogger)null).Result;

// Assert
json.Should().NotContain("\"id\"", "The \"id\" attribute was supposed to be removed, test methodology problem!");
things.Count.Should().Be(1);
things.First().Uuid.Should().Be(new Guid("0657fd6d-a4ab-43c4-84e5-0933c84b4f4f"));

}

#endregion

Expand Down
1 change: 1 addition & 0 deletions JSONAPI.Tests/Json/LinkTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public void GetResourceWithLinkTemplateRelationship()
{
var modelManager = new ModelManager(new PluralizationService());
modelManager.RegisterResourceType(typeof(Post));
modelManager.RegisterResourceType(typeof(User));
var formatter = new JsonApiFormatter(modelManager);
var stream = new MemoryStream();

Expand Down
Loading