|
14 | 14 | using Microsoft.AspNetCore.TestHost;
|
15 | 15 | using Newtonsoft.Json;
|
16 | 16 | using Xunit;
|
| 17 | +using System.Collections.Generic; |
| 18 | +using System.Linq; |
| 19 | +using Microsoft.EntityFrameworkCore; |
17 | 20 |
|
18 | 21 | namespace JsonApiDotNetCoreExampleTests.Acceptance.Spec
|
19 | 22 | {
|
@@ -55,16 +58,16 @@ public async Task Can_Create_Guid_Identifiable_Entities()
|
55 | 58 | {
|
56 | 59 | data = new
|
57 | 60 | {
|
58 |
| - type = "todo-item-collections" |
59 |
| - }, |
60 |
| - relationships = new |
61 |
| - { |
62 |
| - owner = new |
| 61 | + type = "todo-item-collections", |
| 62 | + relationships = new |
63 | 63 | {
|
64 |
| - data = new |
| 64 | + owner = new |
65 | 65 | {
|
66 |
| - type = "people", |
67 |
| - id = owner.Id.ToString() |
| 66 | + data = new |
| 67 | + { |
| 68 | + type = "people", |
| 69 | + id = owner.Id.ToString() |
| 70 | + } |
68 | 71 | }
|
69 | 72 | }
|
70 | 73 | }
|
@@ -114,6 +117,73 @@ public async Task Request_With_ClientGeneratedId_Returns_403()
|
114 | 117 | Assert.Equal(HttpStatusCode.Forbidden, response.StatusCode);
|
115 | 118 | }
|
116 | 119 |
|
| 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 | + |
117 | 187 | [Fact]
|
118 | 188 | public async Task ShouldReceiveLocationHeader_InResponse()
|
119 | 189 | {
|
@@ -144,7 +214,7 @@ public async Task ShouldReceiveLocationHeader_InResponse()
|
144 | 214 | // act
|
145 | 215 | var response = await client.SendAsync(request);
|
146 | 216 | 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>()); |
148 | 218 |
|
149 | 219 | // assert
|
150 | 220 | Assert.Equal(HttpStatusCode.Created, response.StatusCode);
|
|
0 commit comments