Skip to content

Commit 4c5acb9

Browse files
authored
Merge pull request #296 from json-api-dotnet/test/creating-has-one
test(spec): can create hasOne relationship with resource
2 parents f4854c4 + 22e742d commit 4c5acb9

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/CreatingDataTests.cs

+59-2
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public async Task Cannot_Create_Entity_With_Client_Generate_Id()
106106
attributes = new
107107
{
108108
description = todoItem.Description,
109-
ordinal = todoItem.Ordinal,
109+
ordinal = todoItem.Ordinal,
110110
createdDate = DateTime.Now
111111
}
112112
}
@@ -174,7 +174,7 @@ public async Task Can_Create_Guid_Identifiable_Entity_With_Client_Defined_Id_If_
174174
var httpMethod = new HttpMethod("POST");
175175
var server = new TestServer(builder);
176176
var client = server.CreateClient();
177-
177+
178178
var context = _fixture.GetService<AppDbContext>();
179179

180180
var owner = new JsonApiDotNetCoreExample.Models.Person();
@@ -285,6 +285,63 @@ public async Task Can_Create_And_Set_HasMany_Relationships()
285285
Assert.NotEmpty(contextCollection.TodoItems);
286286
}
287287

288+
[Fact]
289+
public async Task Can_Create_And_Set_HasOne_Relationships()
290+
{
291+
// arrange
292+
var builder = new WebHostBuilder()
293+
.UseStartup<Startup>();
294+
var httpMethod = new HttpMethod("POST");
295+
var server = new TestServer(builder);
296+
var client = server.CreateClient();
297+
298+
var context = _fixture.GetService<AppDbContext>();
299+
300+
var todoItem = new TodoItem();
301+
var owner = new JsonApiDotNetCoreExample.Models.Person();
302+
context.People.Add(owner);
303+
await context.SaveChangesAsync();
304+
305+
var route = "/api/v1/todo-items";
306+
var request = new HttpRequestMessage(httpMethod, route);
307+
var content = new
308+
{
309+
data = new
310+
{
311+
type = "todo-items",
312+
relationships = new Dictionary<string, dynamic>
313+
{
314+
{ "owner", new {
315+
data = new
316+
{
317+
type = "people",
318+
id = owner.Id.ToString()
319+
}
320+
} }
321+
}
322+
}
323+
};
324+
325+
request.Content = new StringContent(JsonConvert.SerializeObject(content));
326+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
327+
328+
// act
329+
var response = await client.SendAsync(request);
330+
var body = await response.Content.ReadAsStringAsync();
331+
332+
// assert
333+
Assert.Equal(HttpStatusCode.Created, response.StatusCode);
334+
var deserializedBody = (TodoItem)_fixture.GetService<IJsonApiDeSerializer>().Deserialize(body);
335+
var newId = deserializedBody.Id;
336+
337+
context = _fixture.GetService<AppDbContext>();
338+
var todoItemResult = context.TodoItems
339+
.Include(c => c.Owner)
340+
.SingleOrDefault(c => c.Id == newId);
341+
342+
Assert.Equal(owner.Id, todoItemResult.OwnerId);
343+
}
344+
288345
[Fact]
289346
public async Task ShouldReceiveLocationHeader_InResponse()
290347
{

0 commit comments

Comments
 (0)