11
11
using System . Linq ;
12
12
using Bogus ;
13
13
using JsonApiDotNetCoreExample . Models ;
14
+ using Person = JsonApiDotNetCoreExample . Models . Person ;
14
15
15
16
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec . DocumentTests
16
17
{
@@ -19,6 +20,7 @@ public sealed class Relationships
19
20
{
20
21
private readonly AppDbContext _context ;
21
22
private readonly Faker < TodoItem > _todoItemFaker ;
23
+ private readonly Faker < Person > _personFaker ;
22
24
23
25
public Relationships ( TestFixture < Startup > fixture )
24
26
{
@@ -27,32 +29,36 @@ public Relationships(TestFixture<Startup> fixture)
27
29
. RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
28
30
. RuleFor ( t => t . Ordinal , f => f . Random . Number ( ) )
29
31
. RuleFor ( t => t . CreatedDate , f => f . Date . Past ( ) ) ;
32
+ _personFaker = new Faker < Person > ( )
33
+ . RuleFor ( p => p . FirstName , f => f . Name . FirstName ( ) )
34
+ . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
30
35
}
31
36
32
37
[ Fact ]
33
38
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships ( )
34
39
{
35
40
// Arrange
36
- var builder = new WebHostBuilder ( )
37
- . UseStartup < Startup > ( ) ;
38
-
41
+ _context . TodoItems . RemoveRange ( _context . TodoItems ) ;
42
+ await _context . SaveChangesAsync ( ) ;
43
+
39
44
var todoItem = _todoItemFaker . Generate ( ) ;
40
45
_context . TodoItems . Add ( todoItem ) ;
41
46
await _context . SaveChangesAsync ( ) ;
42
47
43
48
var httpMethod = new HttpMethod ( "GET" ) ;
44
- var route = $ "/api/v1/todoItems/ { todoItem . Id } ";
49
+ var route = "/api/v1/todoItems" ;
45
50
51
+ var builder = new WebHostBuilder ( ) . UseStartup < Startup > ( ) ;
46
52
var server = new TestServer ( builder ) ;
47
53
var client = server . CreateClient ( ) ;
48
54
var request = new HttpRequestMessage ( httpMethod , route ) ;
49
55
50
56
// Act
51
57
var response = await client . SendAsync ( request ) ;
52
- var document = JsonConvert . DeserializeObject < Document > ( await response . Content . ReadAsStringAsync ( ) ) ;
53
- var data = document . SingleData ;
54
- var expectedOwnerSelfLink = $ "http://localhost/api/v1/todoItems/{ data . Id } /relationships/owner";
55
- var expectedOwnerRelatedLink = $ "http://localhost/api/v1/todoItems/{ data . Id } /owner";
58
+ var responseString = await response . Content . ReadAsStringAsync ( ) ;
59
+ var data = JsonConvert . DeserializeObject < Document > ( responseString ) . ManyData [ 0 ] ;
60
+ var expectedOwnerSelfLink = $ "http://localhost/api/v1/todoItems/{ todoItem . Id } /relationships/owner";
61
+ var expectedOwnerRelatedLink = $ "http://localhost/api/v1/todoItems/{ todoItem . Id } /owner";
56
62
57
63
// Assert
58
64
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
@@ -64,16 +70,14 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
64
70
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById ( )
65
71
{
66
72
// Arrange
67
- var builder = new WebHostBuilder ( )
68
- . UseStartup < Startup > ( ) ;
69
-
70
73
var todoItem = _todoItemFaker . Generate ( ) ;
71
74
_context . TodoItems . Add ( todoItem ) ;
72
75
await _context . SaveChangesAsync ( ) ;
73
76
74
77
var httpMethod = new HttpMethod ( "GET" ) ;
75
78
var route = $ "/api/v1/todoItems/{ todoItem . Id } ";
76
79
80
+ var builder = new WebHostBuilder ( ) . UseStartup < Startup > ( ) ;
77
81
var server = new TestServer ( builder ) ;
78
82
var client = server . CreateClient ( ) ;
79
83
var request = new HttpRequestMessage ( httpMethod , route ) ;
@@ -87,30 +91,35 @@ public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById()
87
91
88
92
// Assert
89
93
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
90
- Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "owner" ] . Links ? . Self ) ;
94
+ Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "owner" ] . Links . Self ) ;
91
95
Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "owner" ] . Links . Related ) ;
92
96
}
93
97
94
98
[ Fact ]
95
99
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships ( )
96
100
{
97
101
// Arrange
98
- var builder = new WebHostBuilder ( )
99
- . UseStartup < Startup > ( ) ;
102
+ _context . People . RemoveRange ( _context . People ) ;
103
+ await _context . SaveChangesAsync ( ) ;
104
+
105
+ var person = _personFaker . Generate ( ) ;
106
+ _context . People . Add ( person ) ;
107
+ await _context . SaveChangesAsync ( ) ;
100
108
101
109
var httpMethod = new HttpMethod ( "GET" ) ;
102
110
var route = "/api/v1/people" ;
103
111
112
+ var builder = new WebHostBuilder ( ) . UseStartup < Startup > ( ) ;
104
113
var server = new TestServer ( builder ) ;
105
114
var client = server . CreateClient ( ) ;
106
115
var request = new HttpRequestMessage ( httpMethod , route ) ;
107
116
108
117
// Act
109
118
var response = await client . SendAsync ( request ) ;
110
- var documents = JsonConvert . DeserializeObject < Document > ( await response . Content . ReadAsStringAsync ( ) ) ;
111
- var data = documents . ManyData . First ( ) ;
112
- var expectedOwnerSelfLink = $ "http://localhost/api/v1/people/{ data . Id } /relationships/todoItems";
113
- var expectedOwnerRelatedLink = $ "http://localhost/api/v1/people/{ data . Id } /todoItems";
119
+ var responseString = await response . Content . ReadAsStringAsync ( ) ;
120
+ var data = JsonConvert . DeserializeObject < Document > ( responseString ) . ManyData [ 0 ] ;
121
+ var expectedOwnerSelfLink = $ "http://localhost/api/v1/people/{ person . Id } /relationships/todoItems";
122
+ var expectedOwnerRelatedLink = $ "http://localhost/api/v1/people/{ person . Id } /todoItems";
114
123
115
124
// Assert
116
125
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
@@ -122,14 +131,14 @@ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships()
122
131
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById ( )
123
132
{
124
133
// Arrange
125
- var personId = _context . People . AsEnumerable ( ) . Last ( ) . Id ;
126
-
127
- var builder = new WebHostBuilder ( )
128
- . UseStartup < Startup > ( ) ;
134
+ var person = _personFaker . Generate ( ) ;
135
+ _context . People . Add ( person ) ;
136
+ await _context . SaveChangesAsync ( ) ;
129
137
130
138
var httpMethod = new HttpMethod ( "GET" ) ;
131
- var route = $ "/api/v1/people/{ personId } ";
139
+ var route = $ "/api/v1/people/{ person . Id } ";
132
140
141
+ var builder = new WebHostBuilder ( ) . UseStartup < Startup > ( ) ;
133
142
var server = new TestServer ( builder ) ;
134
143
var client = server . CreateClient ( ) ;
135
144
var request = new HttpRequestMessage ( httpMethod , route ) ;
@@ -138,12 +147,12 @@ public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById()
138
147
var response = await client . SendAsync ( request ) ;
139
148
var responseString = await response . Content . ReadAsStringAsync ( ) ;
140
149
var data = JsonConvert . DeserializeObject < Document > ( responseString ) . SingleData ;
141
- var expectedOwnerSelfLink = $ "http://localhost/api/v1/people/{ personId } /relationships/todoItems";
142
- var expectedOwnerRelatedLink = $ "http://localhost/api/v1/people/{ personId } /todoItems";
150
+ var expectedOwnerSelfLink = $ "http://localhost/api/v1/people/{ person . Id } /relationships/todoItems";
151
+ var expectedOwnerRelatedLink = $ "http://localhost/api/v1/people/{ person . Id } /todoItems";
143
152
144
153
// Assert
145
154
Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
146
- Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "todoItems" ] . Links ? . Self ) ;
155
+ Assert . Equal ( expectedOwnerSelfLink , data . Relationships [ "todoItems" ] . Links . Self ) ;
147
156
Assert . Equal ( expectedOwnerRelatedLink , data . Relationships [ "todoItems" ] . Links . Related ) ;
148
157
}
149
158
}
0 commit comments