Skip to content

Commit 8ca7934

Browse files
committed
test: exposing bug as described in #492
1 parent 0158da3 commit 8ca7934

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingRelationshipsTests.cs

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,78 @@ public UpdatingRelationshipsTests(TestFixture<TestStartup> fixture)
3737
.RuleFor(t => t.Description, f => f.Lorem.Sentence())
3838
.RuleFor(t => t.Ordinal, f => f.Random.Number())
3939
.RuleFor(t => t.CreatedDate, f => f.Date.Past());
40+
41+
42+
}
43+
44+
45+
[Fact]
46+
public async Task Can_Update_ToMany_Relationship_By_Patching_Resource()
47+
{
48+
// arrange
49+
var todoCollection = new TodoItemCollection();
50+
todoCollection.TodoItems = new List<TodoItem>();
51+
var person = _personFaker.Generate();
52+
var todoItem = _todoItemFaker.Generate();
53+
todoCollection.Owner = person;
54+
todoCollection.TodoItems.Add(todoItem);
55+
_context.TodoItemCollections.Add(todoCollection);
56+
_context.SaveChanges();
57+
58+
var newTodoItem1 = _todoItemFaker.Generate();
59+
var newTodoItem2 = _todoItemFaker.Generate();
60+
_context.AddRange(new TodoItem[] { newTodoItem1, newTodoItem2 });
61+
_context.SaveChanges();
62+
63+
64+
65+
var builder = new WebHostBuilder()
66+
.UseStartup<Startup>();
67+
68+
var server = new TestServer(builder);
69+
var client = server.CreateClient();
70+
71+
72+
var content = new
73+
{
74+
data = new
75+
{
76+
type = "todo-collections",
77+
id = todoCollection.Id,
78+
relationships = new Dictionary<string, object>
79+
{
80+
{ "todo-items", new
81+
{
82+
data = new object[]
83+
{
84+
new { type = "todo-items", id = $"{newTodoItem1.Id}" },
85+
new { type = "todo-items", id = $"{newTodoItem2.Id}" }
86+
}
87+
88+
}
89+
},
90+
}
91+
}
92+
};
93+
94+
var httpMethod = new HttpMethod("PATCH");
95+
var route = $"/api/v1/todo-collections/{todoCollection.Id}";
96+
var request = new HttpRequestMessage(httpMethod, route);
97+
98+
string serializedContent = JsonConvert.SerializeObject(content);
99+
request.Content = new StringContent(serializedContent);
100+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
101+
102+
// Act
103+
var response = await client.SendAsync(request);
104+
_context = _fixture.GetService<AppDbContext>();
105+
var updatedTodoItems = _context.TodoItemCollections
106+
.Where(tic => tic.Id == todoCollection.Id)
107+
.Include(tdc => tdc.TodoItems).SingleOrDefault().TodoItems;
108+
109+
110+
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
111+
Assert.Equal(3, updatedTodoItems.Count);
40112
}
41113

42114
[Fact]

0 commit comments

Comments
 (0)