33using System . Net ;
44using System . Threading . Tasks ;
55using Bogus ;
6- using JsonApiDotNetCore . Extensions ;
76using JsonApiDotNetCore . Models . Operations ;
87using Microsoft . EntityFrameworkCore ;
98using OperationsExample . Data ;
@@ -24,20 +23,20 @@ public AddTests(Fixture fixture)
2423 }
2524
2625 [ Fact ]
27- public async Task Can_Create_Article ( )
26+ public async Task Can_Create_Author ( )
2827 {
2928 // arrange
3029 var context = _fixture . GetService < AppDbContext > ( ) ;
31- var article = ArticleFactory . Get ( ) ;
30+ var author = AuthorFactory . Get ( ) ;
3231 var content = new
3332 {
3433 operations = new [ ] {
3534 new {
3635 op = "add" ,
3736 data = new {
38- type = "articles " ,
37+ type = "authors " ,
3938 attributes = new {
40- name = article . Name
39+ name = author . Name
4140 }
4241 }
4342 }
@@ -48,21 +47,21 @@ public async Task Can_Create_Article()
4847 var result = await _fixture . PatchAsync < OperationsDocument > ( "api/bulk" , content ) ;
4948
5049 // assert
51- Assert . NotNull ( result ) ;
50+ Assert . NotNull ( result . response ) ;
5251 Assert . Equal ( HttpStatusCode . OK , result . response . StatusCode ) ;
5352
54- var id = ( string ) result . data . Operations . Single ( ) . DataObject . Id ;
55- var lastArticle = await context . Articles . SingleAsync ( a => a . StringId == id ) ;
56- Assert . Equal ( article . Name , lastArticle . Name ) ;
53+ var id = result . data . Operations . Single ( ) . DataObject . Id ;
54+ var lastAuthor = await context . Authors . SingleAsync ( a => a . StringId == id ) ;
55+ Assert . Equal ( author . Name , lastAuthor . Name ) ;
5756 }
5857
5958 [ Fact ]
60- public async Task Can_Create_Articles ( )
59+ public async Task Can_Create_Authors ( )
6160 {
6261 // arrange
6362 var expectedCount = _faker . Random . Int ( 1 , 10 ) ;
6463 var context = _fixture . GetService < AppDbContext > ( ) ;
65- var articles = ArticleFactory . Get ( expectedCount ) ;
64+ var authors = AuthorFactory . Get ( expectedCount ) ;
6665 var content = new
6766 {
6867 operations = new List < object > ( )
@@ -76,30 +75,97 @@ public async Task Can_Create_Articles()
7675 op = "add" ,
7776 data = new
7877 {
79- type = "articles " ,
78+ type = "authors " ,
8079 attributes = new
8180 {
82- name = articles [ i ] . Name
81+ name = authors [ i ] . Name
8382 }
8483 }
8584 }
8685 ) ;
8786 }
8887
8988 // act
90- var result = await _fixture . PatchAsync < OperationsDocument > ( "api/bulk" , content ) ;
89+ var ( response , data ) = await _fixture . PatchAsync < OperationsDocument > ( "api/bulk" , content ) ;
9190
9291 // assert
93- Assert . NotNull ( result ) ;
94- Assert . Equal ( HttpStatusCode . OK , result . response . StatusCode ) ;
95- Assert . Equal ( expectedCount , result . data . Operations . Count ) ;
92+ Assert . NotNull ( response ) ;
93+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
94+ Assert . Equal ( expectedCount , data . Operations . Count ) ;
9695
9796 for ( int i = 0 ; i < expectedCount ; i ++ )
9897 {
99- var data = result . data . Operations [ i ] . DataObject ;
100- var article = context . Articles . Single ( a => a . StringId == data . Id . ToString ( ) ) ;
101- Assert . Equal ( articles [ i ] . Name , article . Name ) ;
98+ var dataObject = data . Operations [ i ] . DataObject ;
99+ var author = context . Authors . Single ( a => a . StringId == dataObject . ToString ( ) ) ;
100+ Assert . Equal ( authors [ i ] . Name , author . Name ) ;
102101 }
102+ }
103+
104+ [ Fact ]
105+ public async Task Can_Create_Author_With_Article ( )
106+ {
107+ // arrange
108+ var context = _fixture . GetService < AppDbContext > ( ) ;
109+ var author = AuthorFactory . Get ( ) ;
110+ var article = ArticleFactory . Get ( ) ;
111+ const string authorLocalId = "author-1" ;
112+
113+ var content = new
114+ {
115+ operations = new object [ ] {
116+ new {
117+ op = "add" ,
118+ data = new {
119+ lid = authorLocalId ,
120+ type = "authors" ,
121+ attributes = new {
122+ name = author . Name
123+ } ,
124+ }
125+ } ,
126+ new {
127+ op = "add" ,
128+ data = new {
129+ type = "articles" ,
130+ attributes = new {
131+ name = article . Name
132+ } ,
133+ relationships = new {
134+ author = new {
135+ data = new {
136+ type = "authors" ,
137+ lid = authorLocalId
138+ }
139+ }
140+ }
141+ }
142+ }
143+ }
144+ } ;
145+
146+ // act
147+ var ( response , data ) = await _fixture . PatchAsync < OperationsDocument > ( "api/bulk" , content ) ;
148+
149+ // assert
150+ Assert . NotNull ( response ) ;
151+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
152+ Assert . Equal ( 2 , data . Operations . Count ) ;
153+
154+ var authorOperationResult = data . Operations [ 0 ] ;
155+ var id = authorOperationResult . DataObject . Id ;
156+ var lastAuthor = await context . Authors
157+ . Include ( a => a . Articles )
158+ . SingleAsync ( a => a . StringId == id ) ;
159+ var articleOperationResult = data . Operations [ 1 ] ;
160+
161+ // author validation
162+ Assert . Equal ( authorLocalId , authorOperationResult . DataObject . LocalId ) ;
163+ Assert . Equal ( author . Name , lastAuthor . Name ) ;
164+
165+ // article validation
166+ Assert . Equal ( 1 , lastAuthor . Articles . Count ) ;
167+ Assert . Equal ( article . Name , lastAuthor . Articles [ 0 ] . Name ) ;
168+ Assert . Equal ( articleOperationResult . DataObject . Id , lastAuthor . Articles [ 0 ] . StringId ) ;
103169 }
104170 }
105171}
0 commit comments