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
9
using Newtonsoft . Json ;
11
10
using Xunit ;
12
- using JsonApiDotNetCore . Internal ;
13
11
using JsonApiDotNetCore . Models ;
12
+ using JsonApiDotNetCoreExample . Data ;
13
+ using System . Linq ;
14
+ using System ;
14
15
15
16
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
16
17
{
17
18
[ Collection ( "WebHostCollection" ) ]
18
19
public class Relationships
19
20
{
20
21
private DocsFixture < Startup , JsonDocWriter > _fixture ;
22
+ private AppDbContext _context ;
21
23
public Relationships ( DocsFixture < Startup , JsonDocWriter > fixture )
22
24
{
23
25
_fixture = fixture ;
26
+ _context = fixture . GetService < AppDbContext > ( ) ;
24
27
}
25
28
26
29
[ Fact ]
@@ -50,6 +53,35 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
50
53
Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "owner" ] . Links . Related ) ;
51
54
}
52
55
56
+ [ Fact ]
57
+ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById ( )
58
+ {
59
+ // arrange
60
+ var todoItemId = _context . TodoItems . Last ( ) . Id ;
61
+
62
+ var builder = new WebHostBuilder ( )
63
+ . UseStartup < Startup > ( ) ;
64
+
65
+ var httpMethod = new HttpMethod ( "GET" ) ;
66
+ var route = $ "/api/v1/todo-items/{ todoItemId } ";
67
+
68
+ var server = new TestServer ( builder ) ;
69
+ var client = server . CreateClient ( ) ;
70
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
71
+
72
+ // act
73
+ var response = await client . SendAsync ( request ) ;
74
+ var responseString = await response . Content . ReadAsStringAsync ( ) ;
75
+ var data = JsonConvert . DeserializeObject < Document > ( responseString ) . Data ;
76
+ var expectedOwnerSelfLink = $ "http://localhost/api/v1/todo-items/{ todoItemId } /relationships/owner";
77
+ var expectedOwnerRelatedLink = $ "http://localhost/api/v1/todo-items/{ todoItemId } /owner";
78
+
79
+ // assert
80
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
81
+ Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "owner" ] . Links ? . Self ) ;
82
+ Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "owner" ] . Links . Related ) ;
83
+ }
84
+
53
85
[ Fact ]
54
86
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships ( )
55
87
{
@@ -76,5 +108,34 @@ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships()
76
108
Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "todo-items" ] . Links . Self ) ;
77
109
Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "todo-items" ] . Links . Related ) ;
78
110
}
111
+
112
+ [ Fact ]
113
+ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById ( )
114
+ {
115
+ // arrange
116
+ var personId = _context . People . Last ( ) . Id ;
117
+
118
+ var builder = new WebHostBuilder ( )
119
+ . UseStartup < Startup > ( ) ;
120
+
121
+ var httpMethod = new HttpMethod ( "GET" ) ;
122
+ var route = $ "/api/v1/people/{ personId } ";
123
+
124
+ var server = new TestServer ( builder ) ;
125
+ var client = server . CreateClient ( ) ;
126
+ var request = new HttpRequestMessage ( httpMethod , route ) ;
127
+
128
+ // act
129
+ var response = await client . SendAsync ( request ) ;
130
+ var responseString = await response . Content . ReadAsStringAsync ( ) ;
131
+ var data = JsonConvert . DeserializeObject < Document > ( responseString ) . Data ;
132
+ var expectedOwnerSelfLink = $ "http://localhost/api/v1/people/{ personId } /relationships/todo-items";
133
+ var expectedOwnerRelatedLink = $ "http://localhost/api/v1/people/{ personId } /todo-items";
134
+
135
+ // assert
136
+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
137
+ Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "todo-items" ] . Links ? . Self ) ;
138
+ Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "todo-items" ] . Links . Related ) ;
139
+ }
79
140
}
80
141
}
0 commit comments