Skip to content

Commit d67ed88

Browse files
committed
Merge pull request #40 from csantero/filter-model-manager
allow filtering by the id property on related resources Awesome feature add! Yeah we gotta get this dependency resolution thing figured out though.
2 parents 824f8a9 + 0aba12b commit d67ed88

File tree

2 files changed

+244
-38
lines changed

2 files changed

+244
-38
lines changed

JSONAPI.Tests/ActionFilters/EnableFilteringAttributeTests.cs

Lines changed: 133 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using System.Web.Http.Filters;
99
using FluentAssertions;
1010
using JSONAPI.ActionFilters;
11+
using JSONAPI.Core;
12+
using JSONAPI.Json;
1113
using Microsoft.VisualStudio.TestTools.UnitTesting;
1214

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

2527
private class SomeUnknownType
2628
{
27-
29+
30+
}
31+
32+
private class RelatedItemWithId
33+
{
34+
public string Id { get; set; }
35+
public string Name { get; set; }
2836
}
2937

3038
private class Dummy
@@ -62,10 +70,13 @@ private class Dummy
6270
public Single SingleField { get; set; }
6371
public Single? NullableSingleField { get; set; }
6472
public SomeUnknownType UnknownTypeField { get; set; }
73+
public RelatedItemWithId ToOneRelatedItem { get; set; }
74+
public ICollection<RelatedItemWithId> ToManyRelatedItems { get; set; }
6575
}
6676

67-
private IQueryable<Dummy> _fixtures;
68-
77+
private IList<Dummy> _fixtures;
78+
private IQueryable<Dummy> _fixturesQuery;
79+
6980
[TestInitialize]
7081
public void SetupFixtures()
7182
{
@@ -113,7 +124,7 @@ public void SetupFixtures()
113124
{
114125
Id = "120",
115126
NullableDateTimeField = new DateTime(1961, 2, 18)
116-
},
127+
},
117128

118129
#endregion
119130

@@ -229,7 +240,7 @@ public void SetupFixtures()
229240
Id = "211",
230241
SByteField = -89
231242
},
232-
243+
233244
#endregion
234245

235246
#region NullableSByteField
@@ -314,7 +325,7 @@ public void SetupFixtures()
314325
Id = "280",
315326
NullableUInt16Field = 65000
316327
},
317-
328+
318329
#endregion
319330

320331
#region Int32Field
@@ -364,7 +375,7 @@ public void SetupFixtures()
364375
Id = "320",
365376
NullableUInt32Field = 345678901
366377
},
367-
378+
368379
#endregion
369380

370381
#region Int64Field
@@ -414,7 +425,7 @@ public void SetupFixtures()
414425
Id = "360",
415426
NullableUInt64Field = 345678901234
416427
},
417-
428+
418429
#endregion
419430

420431
#region SingleField
@@ -473,17 +484,65 @@ public void SetupFixtures()
473484
{
474485
Id = "1000",
475486
UnknownTypeField = new SomeUnknownType()
487+
},
488+
489+
#endregion
490+
491+
#region ToOneRelatedItem
492+
493+
new Dummy
494+
{
495+
Id = "1100",
496+
ToOneRelatedItem = new RelatedItemWithId
497+
{
498+
Id = "1101",
499+
Name = "Related sample 1"
500+
}
501+
},
502+
new Dummy
503+
{
504+
Id = "1102",
505+
ToOneRelatedItem = new RelatedItemWithId
506+
{
507+
Id = "1103",
508+
Name = "Related sample 2"
509+
}
510+
},
511+
512+
#endregion
513+
514+
#region ToManyRelatedItems
515+
516+
new Dummy
517+
{
518+
Id = "1110",
519+
ToManyRelatedItems = new List<RelatedItemWithId>
520+
{
521+
new RelatedItemWithId { Id = "1111", Name = "Related sample 3" },
522+
new RelatedItemWithId { Id = "1112", Name = "Related sample 4" }
523+
}
524+
},
525+
526+
new Dummy
527+
{
528+
Id = "1120",
529+
ToManyRelatedItems = new List<RelatedItemWithId>
530+
{
531+
new RelatedItemWithId { Id = "1121", Name = "Related sample 5" },
532+
new RelatedItemWithId { Id = "1122", Name = "Related sample 6" }
533+
}
476534
}
477535

478536
#endregion
479-
}.AsQueryable();
537+
};
538+
_fixturesQuery = _fixtures.AsQueryable();
480539
}
481540

482-
private HttpActionExecutedContext CreateActionExecutedContext(string uri)
541+
private HttpActionExecutedContext CreateActionExecutedContext(IModelManager modelManager, string uri)
483542
{
484-
var formatter = new JsonMediaTypeFormatter();
543+
var formatter = new JsonApiFormatter(modelManager);
485544

486-
var httpContent = new ObjectContent(typeof(IQueryable<Dummy>), _fixtures, formatter);
545+
var httpContent = new ObjectContent(typeof(IQueryable<Dummy>), _fixturesQuery, formatter);
487546

488547
return new HttpActionExecutedContext
489548
{
@@ -503,9 +562,11 @@ private HttpActionExecutedContext CreateActionExecutedContext(string uri)
503562

504563
private T[] GetArray<T>(string uri)
505564
{
506-
var filter = new EnableFilteringAttribute();
565+
var modelManager = new ModelManager(new PluralizationService());
566+
567+
var filter = new EnableFilteringAttribute(modelManager);
507568

508-
var context = CreateActionExecutedContext(uri);
569+
var context = CreateActionExecutedContext(modelManager, uri);
509570

510571
filter.OnActionExecuted(context);
511572

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -1069,7 +1131,47 @@ public void Filters_by_missing_nullable_double_property()
10691131
public void Does_not_filter_unknown_type()
10701132
{
10711133
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?unknownTypeField=asdfasd");
1072-
returnedArray.Length.Should().Be(49);
1134+
returnedArray.Length.Should().Be(_fixtures.Count);
1135+
}
1136+
1137+
#endregion
1138+
1139+
#region To-one relationship
1140+
1141+
[TestMethod]
1142+
public void Filters_by_matching_to_one_relationship_id()
1143+
{
1144+
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?toOneRelatedItem=1101");
1145+
returnedArray.Length.Should().Be(1);
1146+
returnedArray[0].Id.Should().Be("1100");
1147+
}
1148+
1149+
[TestMethod]
1150+
public void Filters_by_missing_to_one_relationship_id()
1151+
{
1152+
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?toOneRelatedItem=");
1153+
returnedArray.Length.Should().Be(_fixtures.Count - 2);
1154+
returnedArray.Any(d => d.Id == "1100" || d.Id == "1102").Should().BeFalse();
1155+
}
1156+
1157+
#endregion
1158+
1159+
#region To-many relationship
1160+
1161+
[TestMethod]
1162+
public void Filters_by_matching_id_in_to_many_relationship()
1163+
{
1164+
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?toManyRelatedItems=1111");
1165+
returnedArray.Length.Should().Be(1);
1166+
returnedArray[0].Id.Should().Be("1110");
1167+
}
1168+
1169+
[TestMethod]
1170+
public void Filters_by_missing_id_in_to_many_relationship()
1171+
{
1172+
var returnedArray = GetArray<Dummy>("http://api.example.com/dummies?toManyRelatedItems=");
1173+
returnedArray.Length.Should().Be(_fixtures.Count - 2);
1174+
returnedArray.Any(d => d.Id == "1110" || d.Id == "1120").Should().BeFalse();
10731175
}
10741176

10751177
#endregion

0 commit comments

Comments
 (0)