-
-
Notifications
You must be signed in to change notification settings - Fork 157
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
3 changed files
with
148 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"version": "0.2.9", | ||
"version": "0.2.10", | ||
|
||
"dependencies": { | ||
"Microsoft.NETCore.App": { | ||
|
141 changes: 141 additions & 0 deletions
141
test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/Relationships.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using DotNetCoreDocs; | ||
using DotNetCoreDocs.Writers; | ||
using JsonApiDotNetCoreExample; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.TestHost; | ||
using Newtonsoft.Json; | ||
using Xunit; | ||
using JsonApiDotNetCore.Models; | ||
using JsonApiDotNetCoreExample.Data; | ||
using System.Linq; | ||
using System; | ||
|
||
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec | ||
{ | ||
[Collection("WebHostCollection")] | ||
public class Relationships | ||
{ | ||
private DocsFixture<Startup, JsonDocWriter> _fixture; | ||
private AppDbContext _context; | ||
public Relationships(DocsFixture<Startup, JsonDocWriter> fixture) | ||
{ | ||
_fixture = fixture; | ||
_context = fixture.GetService<AppDbContext>(); | ||
} | ||
|
||
[Fact] | ||
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships() | ||
{ | ||
// arrange | ||
var builder = new WebHostBuilder() | ||
.UseStartup<Startup>(); | ||
|
||
var httpMethod = new HttpMethod("GET"); | ||
var route = $"/api/v1/todo-items"; | ||
|
||
var server = new TestServer(builder); | ||
var client = server.CreateClient(); | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await client.SendAsync(request); | ||
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); | ||
var data = documents.Data[0]; | ||
var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{data.Id}/relationships/owner"; | ||
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{data.Id}/owner"; | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links.Self); | ||
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related); | ||
} | ||
|
||
[Fact] | ||
public async Task Correct_RelationshipObjects_For_ManyToOne_Relationships_ById() | ||
{ | ||
// arrange | ||
var todoItemId = _context.TodoItems.Last().Id; | ||
|
||
var builder = new WebHostBuilder() | ||
.UseStartup<Startup>(); | ||
|
||
var httpMethod = new HttpMethod("GET"); | ||
var route = $"/api/v1/todo-items/{todoItemId}"; | ||
|
||
var server = new TestServer(builder); | ||
var client = server.CreateClient(); | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await client.SendAsync(request); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
var data = JsonConvert.DeserializeObject<Document>(responseString).Data; | ||
var expectedOwnerSelfLink = $"http://localhost/api/v1/todo-items/{todoItemId}/relationships/owner"; | ||
var expectedOwnerRelatedLink = $"http://localhost/api/v1/todo-items/{todoItemId}/owner"; | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal(expectedOwnerSelfLink, data.Relationships["owner"].Links?.Self); | ||
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["owner"].Links.Related); | ||
} | ||
|
||
[Fact] | ||
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships() | ||
{ | ||
// arrange | ||
var builder = new WebHostBuilder() | ||
.UseStartup<Startup>(); | ||
|
||
var httpMethod = new HttpMethod("GET"); | ||
var route = $"/api/v1/people"; | ||
|
||
var server = new TestServer(builder); | ||
var client = server.CreateClient(); | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await client.SendAsync(request); | ||
var documents = JsonConvert.DeserializeObject<Documents>(await response.Content.ReadAsStringAsync()); | ||
var data = documents.Data[0]; | ||
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{data.Id}/relationships/todo-items"; | ||
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{data.Id}/todo-items"; | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links.Self); | ||
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related); | ||
} | ||
|
||
[Fact] | ||
public async Task Correct_RelationshipObjects_For_OneToMany_Relationships_ById() | ||
{ | ||
// arrange | ||
var personId = _context.People.Last().Id; | ||
|
||
var builder = new WebHostBuilder() | ||
.UseStartup<Startup>(); | ||
|
||
var httpMethod = new HttpMethod("GET"); | ||
var route = $"/api/v1/people/{personId}"; | ||
|
||
var server = new TestServer(builder); | ||
var client = server.CreateClient(); | ||
var request = new HttpRequestMessage(httpMethod, route); | ||
|
||
// act | ||
var response = await client.SendAsync(request); | ||
var responseString = await response.Content.ReadAsStringAsync(); | ||
var data = JsonConvert.DeserializeObject<Document>(responseString).Data; | ||
var expectedOwnerSelfLink = $"http://localhost/api/v1/people/{personId}/relationships/todo-items"; | ||
var expectedOwnerRelatedLink = $"http://localhost/api/v1/people/{personId}/todo-items"; | ||
|
||
// assert | ||
Assert.Equal(HttpStatusCode.OK, response.StatusCode); | ||
Assert.Equal(expectedOwnerSelfLink, data.Relationships["todo-items"].Links?.Self); | ||
Assert.Equal(expectedOwnerRelatedLink, data.Relationships["todo-items"].Links.Related); | ||
} | ||
} | ||
} |