@@ -255,6 +255,69 @@ public async Task Can_Update_Many_To_Many()
255
255
Assert . Equal ( tag . Id , persistedArticleTag . TagId ) ;
256
256
}
257
257
258
+ [ Fact ]
259
+ public async Task Can_Update_Many_To_Many_With_Complete_Replacement ( )
260
+ {
261
+ // arrange
262
+ var context = _fixture . GetService < AppDbContext > ( ) ;
263
+ var firstTag = _tagFaker . Generate ( ) ;
264
+ var article = _articleFaker . Generate ( ) ;
265
+ var articleTag = new ArticleTag
266
+ {
267
+ Article = article ,
268
+ Tag = firstTag
269
+ } ;
270
+ context . ArticleTags . Add ( articleTag ) ;
271
+ await context . SaveChangesAsync ( ) ;
272
+
273
+ var secondTag = _tagFaker . Generate ( ) ;
274
+
275
+ var route = $ "/api/v1/articles/{ article . Id } ";
276
+ var request = new HttpRequestMessage ( new HttpMethod ( "PATCH" ) , route ) ;
277
+ var content = new
278
+ {
279
+ data = new
280
+ {
281
+ type = "articles" ,
282
+ id = article . StringId ,
283
+ relationships = new Dictionary < string , dynamic >
284
+ {
285
+ { "tags" , new {
286
+ data = new [ ] { new
287
+ {
288
+ type = "tags" ,
289
+ id = firstTag . StringId
290
+ } , new
291
+ {
292
+ type = "tags" ,
293
+ id = secondTag . StringId
294
+ } }
295
+ } }
296
+ }
297
+ }
298
+ } ;
299
+
300
+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
301
+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
302
+
303
+ // act
304
+ var response = await _fixture . Client . SendAsync ( request ) ;
305
+
306
+ // assert
307
+ var body = await response . Content . ReadAsStringAsync ( ) ;
308
+ Assert . True ( HttpStatusCode . OK == response . StatusCode , $ "{ route } returned { response . StatusCode } status code with payload: { body } ") ;
309
+
310
+ var articleResponse = _fixture . GetService < IJsonApiDeSerializer > ( ) . Deserialize < Article > ( body ) ;
311
+ Assert . NotNull ( articleResponse ) ;
312
+
313
+ _fixture . ReloadDbContext ( ) ;
314
+ var persistedArticle = await _fixture . Context . Articles
315
+ . Include ( a => a . ArticleTags )
316
+ . SingleOrDefaultAsync ( a => a . Id == article . Id ) ;
317
+ var tags = persistedArticle . ArticleTags . Select ( at => at . Tag ) . ToList ( ) ;
318
+ Assert . Equal ( 2 , tags . Count ) ;
319
+ }
320
+
258
321
[ Fact ]
259
322
public async Task Can_Update_Many_To_Many_Through_Relationship_Link ( )
260
323
{
0 commit comments