2
2
using System . Net ;
3
3
using System . Net . Http ;
4
4
using System . Threading . Tasks ;
5
+ using Bogus ;
5
6
using DotNetCoreDocs ;
6
7
using DotNetCoreDocs . Writers ;
8
+ using JsonApiDotNetCore . Models ;
7
9
using JsonApiDotNetCore . Serialization ;
8
10
using JsonApiDotNetCore . Services ;
9
11
using JsonApiDotNetCoreExample ;
13
15
using Microsoft . AspNetCore . TestHost ;
14
16
using Newtonsoft . Json ;
15
17
using Xunit ;
18
+ using Person = JsonApiDotNetCoreExample . Models . Person ;
16
19
17
20
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
18
21
{
@@ -21,11 +24,19 @@ public class FetchingDataTests
21
24
{
22
25
private DocsFixture < Startup , JsonDocWriter > _fixture ;
23
26
private IJsonApiContext _jsonApiContext ;
27
+ private Faker < TodoItem > _todoItemFaker ;
28
+ private Faker < Person > _personFaker ;
24
29
25
30
public FetchingDataTests ( DocsFixture < Startup , JsonDocWriter > fixture )
26
31
{
27
32
_fixture = fixture ;
28
33
_jsonApiContext = fixture . GetService < IJsonApiContext > ( ) ;
34
+ _todoItemFaker = new Faker < TodoItem > ( )
35
+ . RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
36
+ . RuleFor ( t => t . Ordinal , f => f . Random . Number ( ) ) ;
37
+ _personFaker = new Faker < Person > ( )
38
+ . RuleFor ( p => p . FirstName , f => f . Name . FirstName ( ) )
39
+ . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
29
40
}
30
41
31
42
[ Fact ]
@@ -60,5 +71,38 @@ public async Task Request_ForEmptyCollection_Returns_EmptyDataCollection()
60
71
61
72
context . Dispose ( ) ;
62
73
}
74
+
75
+ [ Fact ]
76
+ public async Task Included_Records_Contain_Relationship_Links ( )
77
+ {
78
+ // arrange
79
+ var context = _fixture . GetService < AppDbContext > ( ) ;
80
+ var todoItem = _todoItemFaker . Generate ( ) ;
81
+ var person = _personFaker . Generate ( ) ;
82
+ todoItem . Owner = person ;
83
+ context . TodoItems . Add ( todoItem ) ;
84
+ await context . SaveChangesAsync ( ) ;
85
+
86
+ var builder = new WebHostBuilder ( )
87
+ . UseStartup < Startup > ( ) ;
88
+ var httpMethod = new HttpMethod ( "GET" ) ;
89
+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ?include=owner";
90
+ var server = new TestServer ( builder ) ;
91
+ var client = server . CreateClient ( ) ;
92
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
93
+
94
+ // act
95
+ var response = await client . SendAsync ( request ) ;
96
+ var body = await response . Content . ReadAsStringAsync ( ) ;
97
+ var deserializedBody = JsonConvert . DeserializeObject < Document > ( body ) ;
98
+
99
+ // assert
100
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
101
+ Assert . Equal ( person . StringId , deserializedBody . Included [ 0 ] . Id ) ;
102
+ Assert . NotNull ( deserializedBody . Included [ 0 ] . Relationships ) ;
103
+ Assert . Equal ( $ "http://localhost/api/v1/people/{ person . Id } /todo-items", deserializedBody . Included [ 0 ] . Relationships [ "todo-items" ] . Links . Related ) ;
104
+ Assert . Equal ( $ "http://localhost/api/v1/people/{ person . Id } /relationships/todo-items", deserializedBody . Included [ 0 ] . Relationships [ "todo-items" ] . Links . Self ) ;
105
+ context . Dispose ( ) ;
106
+ }
63
107
}
64
108
}
0 commit comments