Skip to content
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
164 changes: 133 additions & 31 deletions JSONAPI.Tests/ActionFilters/EnableFilteringAttributeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Web.Http.Filters;
using FluentAssertions;
using JSONAPI.ActionFilters;
using JSONAPI.Core;
using JSONAPI.Json;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace JSONAPI.Tests.ActionFilters
Expand All @@ -24,7 +26,13 @@ private enum SomeEnum

private class SomeUnknownType
{


}

private class RelatedItemWithId
{
public string Id { get; set; }
public string Name { get; set; }
}

private class Dummy
Expand Down Expand Up @@ -62,10 +70,13 @@ private class Dummy
public Single SingleField { get; set; }
public Single? NullableSingleField { get; set; }
public SomeUnknownType UnknownTypeField { get; set; }
public RelatedItemWithId ToOneRelatedItem { get; set; }
public ICollection<RelatedItemWithId> ToManyRelatedItems { get; set; }
}

private IQueryable<Dummy> _fixtures;

private IList<Dummy> _fixtures;
private IQueryable<Dummy> _fixturesQuery;

[TestInitialize]
public void SetupFixtures()
{
Expand Down Expand Up @@ -113,7 +124,7 @@ public void SetupFixtures()
{
Id = "120",
NullableDateTimeField = new DateTime(1961, 2, 18)
},
},

#endregion

Expand Down Expand Up @@ -229,7 +240,7 @@ public void SetupFixtures()
Id = "211",
SByteField = -89
},

#endregion

#region NullableSByteField
Expand Down Expand Up @@ -314,7 +325,7 @@ public void SetupFixtures()
Id = "280",
NullableUInt16Field = 65000
},

#endregion

#region Int32Field
Expand Down Expand Up @@ -364,7 +375,7 @@ public void SetupFixtures()
Id = "320",
NullableUInt32Field = 345678901
},

#endregion

#region Int64Field
Expand Down Expand Up @@ -414,7 +425,7 @@ public void SetupFixtures()
Id = "360",
NullableUInt64Field = 345678901234
},

#endregion

#region SingleField
Expand Down Expand Up @@ -473,17 +484,65 @@ public void SetupFixtures()
{
Id = "1000",
UnknownTypeField = new SomeUnknownType()
},

#endregion

#region ToOneRelatedItem

new Dummy
{
Id = "1100",
ToOneRelatedItem = new RelatedItemWithId
{
Id = "1101",
Name = "Related sample 1"
}
},
new Dummy
{
Id = "1102",
ToOneRelatedItem = new RelatedItemWithId
{
Id = "1103",
Name = "Related sample 2"
}
},

#endregion

#region ToManyRelatedItems

new Dummy
{
Id = "1110",
ToManyRelatedItems = new List<RelatedItemWithId>
{
new RelatedItemWithId { Id = "1111", Name = "Related sample 3" },
new RelatedItemWithId { Id = "1112", Name = "Related sample 4" }
}
},

new Dummy
{
Id = "1120",
ToManyRelatedItems = new List<RelatedItemWithId>
{
new RelatedItemWithId { Id = "1121", Name = "Related sample 5" },
new RelatedItemWithId { Id = "1122", Name = "Related sample 6" }
}
}

#endregion
}.AsQueryable();
};
_fixturesQuery = _fixtures.AsQueryable();
}

private HttpActionExecutedContext CreateActionExecutedContext(string uri)
private HttpActionExecutedContext CreateActionExecutedContext(IModelManager modelManager, string uri)
{
var formatter = new JsonMediaTypeFormatter();
var formatter = new JsonApiFormatter(modelManager);

var httpContent = new ObjectContent(typeof(IQueryable<Dummy>), _fixtures, formatter);
var httpContent = new ObjectContent(typeof(IQueryable<Dummy>), _fixturesQuery, formatter);

return new HttpActionExecutedContext
{
Expand All @@ -503,9 +562,11 @@ private HttpActionExecutedContext CreateActionExecutedContext(string uri)

private T[] GetArray<T>(string uri)
{
var filter = new EnableFilteringAttribute();
var modelManager = new ModelManager(new PluralizationService());

var filter = new EnableFilteringAttribute(modelManager);

var context = CreateActionExecutedContext(uri);
var context = CreateActionExecutedContext(modelManager, uri);

filter.OnActionExecuted(context);

Expand Down Expand Up @@ -533,7 +594,8 @@ public void Filters_by_matching_string_property()
public void Filters_by_missing_string_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?stringField=");
returnedArray.Length.Should().Be(46);
returnedArray.Length.Should().Be(_fixtures.Count - 3);
returnedArray.Any(d => d.Id == "100" || d.Id == "101" || d.Id == "102").Should().BeFalse();
}

#endregion
Expand Down Expand Up @@ -567,7 +629,7 @@ public void Filters_by_matching_nullable_datetime_property()
public void Filters_by_missing_nullable_datetime_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableDateTimeField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "120").Should().BeFalse();
}

Expand Down Expand Up @@ -602,7 +664,7 @@ public void Filters_by_matching_nullable_datetimeoffset_property()
public void Filters_by_missing_nullable_datetimeoffset_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableDateTimeOffsetField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "140").Should().BeFalse();
}

Expand Down Expand Up @@ -637,7 +699,7 @@ public void Filters_by_matching_nullable_enum_property()
public void Filters_by_missing_nullable_enum_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableEnumField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "160").Should().BeFalse();
}

Expand Down Expand Up @@ -672,7 +734,7 @@ public void Filters_by_matching_nullable_decimal_property()
public void Filters_by_missing_nullable_decimal_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableDecimalField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "180").Should().BeFalse();
}

Expand Down Expand Up @@ -707,7 +769,7 @@ public void Filters_by_matching_nullable_boolean_property()
public void Filters_by_missing_nullable_boolean_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableBooleanField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "200").Should().BeFalse();
}

Expand Down Expand Up @@ -742,7 +804,7 @@ public void Filters_by_matching_nullable_sbyte_property()
public void Filters_by_missing_nullable_sbyte_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableSByteField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "220").Should().BeFalse();
}

Expand Down Expand Up @@ -777,7 +839,7 @@ public void Filters_by_matching_nullable_byte_property()
public void Filters_by_missing_nullable_byte_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableByteField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "240").Should().BeFalse();
}

Expand Down Expand Up @@ -812,7 +874,7 @@ public void Filters_by_matching_nullable_int16_property()
public void Filters_by_missing_nullable_int16_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableInt16Field=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "260").Should().BeFalse();
}

Expand Down Expand Up @@ -847,7 +909,7 @@ public void Filters_by_matching_nullable_uint16_property()
public void Filters_by_missing_nullable_uint16_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableUInt16Field=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "280").Should().BeFalse();
}

Expand Down Expand Up @@ -882,7 +944,7 @@ public void Filters_by_matching_nullable_int32_property()
public void Filters_by_missing_nullable_int32_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableInt32Field=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "300").Should().BeFalse();
}

Expand Down Expand Up @@ -917,7 +979,7 @@ public void Filters_by_matching_nullable_uint32_property()
public void Filters_by_missing_nullable_uint32_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableUInt32Field=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "320").Should().BeFalse();
}

Expand Down Expand Up @@ -952,7 +1014,7 @@ public void Filters_by_matching_nullable_int64_property()
public void Filters_by_missing_nullable_int64_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableInt64Field=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "340").Should().BeFalse();
}

Expand Down Expand Up @@ -987,7 +1049,7 @@ public void Filters_by_matching_nullable_uint64_property()
public void Filters_by_missing_nullable_uint64_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableUInt64Field=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "360").Should().BeFalse();
}

Expand Down Expand Up @@ -1022,7 +1084,7 @@ public void Filters_by_matching_nullable_single_property()
public void Filters_by_missing_nullable_single_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableSingleField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "380").Should().BeFalse();
}

Expand Down Expand Up @@ -1057,7 +1119,7 @@ public void Filters_by_matching_nullable_double_property()
public void Filters_by_missing_nullable_double_property()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?nullableDoubleField=");
returnedArray.Length.Should().Be(48);
returnedArray.Length.Should().Be(_fixtures.Count - 1);
returnedArray.Any(d => d.Id == "400").Should().BeFalse();
}

Expand All @@ -1069,7 +1131,47 @@ public void Filters_by_missing_nullable_double_property()
public void Does_not_filter_unknown_type()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?unknownTypeField=asdfasd");
returnedArray.Length.Should().Be(49);
returnedArray.Length.Should().Be(_fixtures.Count);
}

#endregion

#region To-one relationship

[TestMethod]
public void Filters_by_matching_to_one_relationship_id()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?toOneRelatedItem=1101");
returnedArray.Length.Should().Be(1);
returnedArray[0].Id.Should().Be("1100");
}

[TestMethod]
public void Filters_by_missing_to_one_relationship_id()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?toOneRelatedItem=");
returnedArray.Length.Should().Be(_fixtures.Count - 2);
returnedArray.Any(d => d.Id == "1100" || d.Id == "1102").Should().BeFalse();
}

#endregion

#region To-many relationship

[TestMethod]
public void Filters_by_matching_id_in_to_many_relationship()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?toManyRelatedItems=1111");
returnedArray.Length.Should().Be(1);
returnedArray[0].Id.Should().Be("1110");
}

[TestMethod]
public void Filters_by_missing_id_in_to_many_relationship()
{
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?toManyRelatedItems=");
returnedArray.Length.Should().Be(_fixtures.Count - 2);
returnedArray.Any(d => d.Id == "1110" || d.Id == "1120").Should().BeFalse();
}

#endregion
Expand Down
Loading