Skip to content

Commit cedea74

Browse files
committed
test(creating-data): can create new entities with hasMany relationships
1 parent cc53963 commit cedea74

File tree

1 file changed

+79
-9
lines changed

1 file changed

+79
-9
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs

Lines changed: 79 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
using Microsoft.AspNetCore.TestHost;
1515
using Newtonsoft.Json;
1616
using Xunit;
17+
using System.Collections.Generic;
18+
using System.Linq;
19+
using Microsoft.EntityFrameworkCore;
1720

1821
namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
1922
{
@@ -55,16 +58,16 @@ public async Task Can_Create_Guid_Identifiable_Entities()
5558
{
5659
data = new
5760
{
58-
type = "todo-item-collections"
59-
},
60-
relationships = new
61-
{
62-
owner = new
61+
type = "todo-item-collections",
62+
relationships = new
6363
{
64-
data = new
64+
owner = new
6565
{
66-
type = "people",
67-
id = owner.Id.ToString()
66+
data = new
67+
{
68+
type = "people",
69+
id = owner.Id.ToString()
70+
}
6871
}
6972
}
7073
}
@@ -114,6 +117,73 @@ public async Task Request_With_ClientGeneratedId_Returns_403()
114117
Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
115118
}
116119

120+
[Fact]
121+
public async Task Can_Create_And_Set_HasMany_Relationships()
122+
{
123+
// arrange
124+
var builder = new WebHostBuilder()
125+
.UseStartup<Startup>();
126+
var httpMethod = new HttpMethod("POST");
127+
var server = new TestServer(builder);
128+
var client = server.CreateClient();
129+
130+
var context = _fixture.GetService<AppDbContext>();
131+
132+
var owner = new JsonApiDotNetCoreExample.Models.Person();
133+
var todoItem = new TodoItem();
134+
todoItem.Owner = owner;
135+
context.People.Add(owner);
136+
context.TodoItems.Add(todoItem);
137+
await context.SaveChangesAsync();
138+
139+
var route = "/api/v1/todo-item-collections";
140+
var request = new HttpRequestMessage(httpMethod, route);
141+
var content = new
142+
{
143+
data = new
144+
{
145+
type = "todo-item-collections",
146+
relationships = new Dictionary<string, dynamic>
147+
{
148+
{ "owner", new {
149+
data = new
150+
{
151+
type = "people",
152+
id = owner.Id.ToString()
153+
}
154+
} },
155+
{ "todo-items", new {
156+
data = new dynamic[]
157+
{
158+
new {
159+
type = "todo-items",
160+
id = todoItem.Id.ToString()
161+
}
162+
}
163+
} }
164+
}
165+
}
166+
};
167+
168+
request.Content = new StringContent(JsonConvert.SerializeObject(content));
169+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
170+
171+
// act
172+
var response = await client.SendAsync(request);
173+
var body = await response.Content.ReadAsStringAsync();
174+
var deserializedBody = (TodoItemCollection)JsonApiDeSerializer.Deserialize(body, _jsonApiContext, context);
175+
var newId = deserializedBody.Id;
176+
var contextCollection = context.TodoItemCollections
177+
.Include(c=> c.Owner)
178+
.Include(c => c.TodoItems)
179+
.SingleOrDefault(c => c.Id == newId);
180+
181+
// assert
182+
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
183+
Assert.Equal(owner.Id, contextCollection.OwnerId);
184+
Assert.NotEmpty(contextCollection.TodoItems);
185+
}
186+
117187
[Fact]
118188
public async Task ShouldReceiveLocationHeader_InResponse()
119189
{
@@ -144,7 +214,7 @@ public async Task ShouldReceiveLocationHeader_InResponse()
144214
// act
145215
var response = await client.SendAsync(request);
146216
var body = await response.Content.ReadAsStringAsync();
147-
var deserializedBody = (TodoItem)JsonApiDeSerializer.Deserialize(body, _jsonApiContext);
217+
var deserializedBody = (TodoItem)JsonApiDeSerializer.Deserialize(body, _jsonApiContext, _fixture.GetService<AppDbContext>());
148218

149219
// assert
150220
Assert.Equal(HttpStatusCode.Created, response.StatusCode);

0 commit comments

Comments
 (0)