Skip to content

Commit 7f84dd2

Browse files
committed
test(relationships): add failing tests
1 parent 3f93a8e commit 7f84dd2

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System.Net;
2+
using System.Net.Http;
3+
using System.Threading.Tasks;
4+
using DotNetCoreDocs;
5+
using DotNetCoreDocs.Models;
6+
using DotNetCoreDocs.Writers;
7+
using JsonApiDotNetCoreExample;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.TestHost;
10+
using Newtonsoft.Json;
11+
using Xunit;
12+
using JsonApiDotNetCore.Internal;
13+
using JsonApiDotNetCore.Models;
14+
15+
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
16+
{
17+
[Collection("WebHostCollection")]
18+
public class Relationships
19+
{
20+
private DocsFixture<Startup, JsonDocWriter> _fixture;
21+
public Relationships(DocsFixture<Startup, JsonDocWriter> fixture)
22+
{
23+
_fixture = fixture;
24+
}
25+
26+
[Fact]
27+
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships()
28+
{
29+
// arrange
30+
var builder = new WebHostBuilder()
31+
.UseStartup<Startup>();
32+
33+
var httpMethod = new HttpMethod("GET");
34+
var route = $"/api/v1/todo-items";
35+
36+
var server = new TestServer(builder);
37+
var client = server.CreateClient();
38+
var request = new HttpRequestMessage(httpMethod, route);
39+
40+
// act
41+
var response = await client.SendAsync(request);
42+
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
43+
var data = documents.Data[0];
44+
var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{data.Id}/relationships/owner";
45+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{data.Id}/owner";
46+
47+
// assert
48+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
49+
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links.Self);
50+
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related);
51+
}
52+
53+
[Fact]
54+
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships()
55+
{
56+
// arrange
57+
var builder = new WebHostBuilder()
58+
.UseStartup<Startup>();
59+
60+
var httpMethod = new HttpMethod("GET");
61+
var route = $"/api/v1/people";
62+
63+
var server = new TestServer(builder);
64+
var client = server.CreateClient();
65+
var request = new HttpRequestMessage(httpMethod, route);
66+
67+
// act
68+
var response = await client.SendAsync(request);
69+
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync());
70+
var data = documents.Data[0];
71+
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{data.Id}/relationships/todo-items";
72+
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{data.Id}/todo-items";
73+
74+
// assert
75+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
76+
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links.Self);
77+
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related);
78+
}
79+
}
80+
}

0 commit comments

Comments
 (0)