2
2
using System . Net . Http ;
3
3
using System . Threading . Tasks ;
4
4
using DotNetCoreDocs ;
5
- using DotNetCoreDocs . Models ;
6
5
using DotNetCoreDocs . Writers ;
7
6
using JsonApiDotNetCoreExample ;
8
7
using Microsoft . AspNetCore . Hosting ;
9
8
using Microsoft . AspNetCore . TestHost ;
10
- using Newtonsoft . Json ;
11
9
using Xunit ;
12
- using JsonApiDotNetCore . Internal ;
13
10
using JsonApiDotNetCoreExample . Data ;
14
11
using Bogus ;
15
12
using JsonApiDotNetCoreExample . Models ;
16
13
using JsonApiDotNetCore . Serialization ;
17
14
using System . Linq ;
15
+ using Person = JsonApiDotNetCoreExample . Models . Person ;
18
16
19
17
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
20
18
{
@@ -23,13 +21,18 @@ public class AttributeFilterTests
23
21
{
24
22
private DocsFixture < Startup , JsonDocWriter > _fixture ;
25
23
private Faker < TodoItem > _todoItemFaker ;
26
-
24
+ private readonly Faker < Person > _personFaker ;
25
+
27
26
public AttributeFilterTests ( DocsFixture < Startup , JsonDocWriter > fixture )
28
27
{
29
28
_fixture = fixture ;
30
29
_todoItemFaker = new Faker < TodoItem > ( )
31
30
. RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
32
31
. RuleFor ( t => t . Ordinal , f => f . Random . Number ( ) ) ;
32
+
33
+ _personFaker = new Faker < Person > ( )
34
+ . RuleFor ( p => p . FirstName , f => f . Name . FirstName ( ) )
35
+ . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
33
36
}
34
37
35
38
[ Fact ]
@@ -63,5 +66,39 @@ public async Task Can_Filter_On_Guid_Properties()
63
66
Assert . Equal ( todoItem . Id , todoItemResponse . Id ) ;
64
67
Assert . Equal ( todoItem . GuidProperty , todoItemResponse . GuidProperty ) ;
65
68
}
69
+
70
+
71
+ [ Fact ]
72
+ public async Task Can_Filter_On_Related_Attrs ( )
73
+ {
74
+ // arrange
75
+ var context = _fixture . GetService < AppDbContext > ( ) ;
76
+ var person = _personFaker . Generate ( ) ;
77
+ var todoItem = _todoItemFaker . Generate ( ) ;
78
+ todoItem . Owner = person ;
79
+ context . TodoItems . Add ( todoItem ) ;
80
+ await context . SaveChangesAsync ( ) ;
81
+
82
+ var builder = new WebHostBuilder ( )
83
+ . UseStartup < Startup > ( ) ;
84
+ var httpMethod = new HttpMethod ( "GET" ) ;
85
+ var route = $ "/api/v1/todo-items?include[owner]&filter[owner.first-name]={ person . FirstName } ";
86
+ var server = new TestServer ( builder ) ;
87
+ var client = server . CreateClient ( ) ;
88
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
89
+
90
+ // act
91
+ var response = await client . SendAsync ( request ) ;
92
+ var body = await response . Content . ReadAsStringAsync ( ) ;
93
+ var deserializedBody = _fixture
94
+ . GetService < IJsonApiDeSerializer > ( )
95
+ . DeserializeList < TodoItem > ( body ) ;
96
+
97
+ // assert
98
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
99
+ Assert . NotEmpty ( deserializedBody ) ;
100
+ foreach ( var item in deserializedBody )
101
+ Assert . Equal ( person . FirstName , item . Owner . FirstName ) ;
102
+ }
66
103
}
67
104
}
0 commit comments