11using System ;
2+ using System . Collections . Generic ;
23using System . Linq ;
34using System . Net ;
45using System . Net . Http ;
56using System . Net . Http . Headers ;
67using System . Threading . Tasks ;
78using Bogus ;
9+ using JsonApiDotNetCore . Models ;
810using JsonApiDotNetCoreExample ;
911using JsonApiDotNetCoreExample . Data ;
1012using JsonApiDotNetCoreExample . Models ;
@@ -49,7 +51,7 @@ public async Task Respond_404_If_EntityDoesNotExist()
4951
5052 var server = new TestServer ( builder ) ;
5153 var client = server . CreateClient ( ) ;
52-
54+
5355 var content = new
5456 {
5557 data = new
@@ -78,6 +80,124 @@ public async Task Respond_404_If_EntityDoesNotExist()
7880 Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
7981 }
8082
83+
84+ [ Fact ]
85+ public async Task Can_Patch_Entity ( )
86+ {
87+ // arrange
88+ var todoItem = _todoItemFaker . Generate ( ) ;
89+ var person = _personFaker . Generate ( ) ;
90+ todoItem . Owner = person ;
91+ _context . TodoItems . Add ( todoItem ) ;
92+ _context . SaveChanges ( ) ;
93+
94+ var newTodoItem = _todoItemFaker . Generate ( ) ;
95+
96+ var builder = new WebHostBuilder ( ) . UseStartup < Startup > ( ) ;
97+ var server = new TestServer ( builder ) ;
98+ var client = server . CreateClient ( ) ;
99+
100+ var content = new
101+ {
102+ data = new
103+ {
104+ type = "todo-items" ,
105+ attributes = new
106+ {
107+ description = newTodoItem . Description ,
108+ ordinal = newTodoItem . Ordinal
109+ }
110+ }
111+ } ;
112+
113+ var httpMethod = new HttpMethod ( "PATCH" ) ;
114+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
115+ var request = new HttpRequestMessage ( httpMethod , route ) ;
116+
117+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
118+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
119+
120+ // Act
121+ var response = await client . SendAsync ( request ) ;
122+
123+ // Assert -- response
124+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
125+ var body = await response . Content . ReadAsStringAsync ( ) ;
126+ var document = JsonConvert . DeserializeObject < Document > ( body ) ;
127+ Assert . NotNull ( document ) ;
128+ Assert . NotNull ( document . Data ) ;
129+ Assert . NotNull ( document . Data . Attributes ) ;
130+ Assert . Equal ( newTodoItem . Description , document . Data . Attributes [ "description" ] ) ;
131+ Assert . Equal ( newTodoItem . Ordinal , ( long ) document . Data . Attributes [ "ordinal" ] ) ;
132+ Assert . True ( document . Data . Relationships . ContainsKey ( "owner" ) ) ;
133+ Assert . NotNull ( document . Data . Relationships [ "owner" ] . SingleData ) ;
134+ Assert . Equal ( person . Id . ToString ( ) , document . Data . Relationships [ "owner" ] . SingleData . Id ) ;
135+ Assert . Equal ( "people" , document . Data . Relationships [ "owner" ] . SingleData . Type ) ;
136+
137+ // Assert -- database
138+ var updatedTodoItem = _context . TodoItems . AsNoTracking ( )
139+ . Include ( t => t . Owner )
140+ . SingleOrDefault ( t => t . Id == todoItem . Id ) ;
141+
142+ Assert . Equal ( person . Id , updatedTodoItem . OwnerId ) ;
143+ Assert . Equal ( newTodoItem . Description , updatedTodoItem . Description ) ;
144+ Assert . Equal ( newTodoItem . Ordinal , updatedTodoItem . Ordinal ) ;
145+ }
146+
147+ [ Fact ]
148+ public async Task Patch_Entity_With_HasMany_Does_Not_Included_Relationships ( )
149+ {
150+ // arrange
151+ var todoItem = _todoItemFaker . Generate ( ) ;
152+ var person = _personFaker . Generate ( ) ;
153+ todoItem . Owner = person ;
154+ _context . TodoItems . Add ( todoItem ) ;
155+ _context . SaveChanges ( ) ;
156+
157+ var newPerson = _personFaker . Generate ( ) ;
158+
159+ var builder = new WebHostBuilder ( ) . UseStartup < Startup > ( ) ;
160+ var server = new TestServer ( builder ) ;
161+ var client = server . CreateClient ( ) ;
162+
163+ var content = new
164+ {
165+ data = new
166+ {
167+ type = "people" ,
168+ attributes = new Dictionary < string , object >
169+ {
170+ { "last-name" , newPerson . LastName } ,
171+ { "first-name" , newPerson . FirstName } ,
172+ }
173+ }
174+ } ;
175+
176+ var httpMethod = new HttpMethod ( "PATCH" ) ;
177+ var route = $ "/api/v1/people/{ person . Id } ";
178+ var request = new HttpRequestMessage ( httpMethod , route ) ;
179+
180+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
181+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
182+
183+ // Act
184+ var response = await client . SendAsync ( request ) ;
185+
186+ // Assert -- response
187+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
188+ var body = await response . Content . ReadAsStringAsync ( ) ;
189+ var document = JsonConvert . DeserializeObject < Document > ( body ) ;
190+ Console . WriteLine ( body ) ;
191+ Assert . NotNull ( document ) ;
192+ Assert . NotNull ( document . Data ) ;
193+ Assert . NotNull ( document . Data . Attributes ) ;
194+ Assert . Equal ( newPerson . LastName , document . Data . Attributes [ "last-name" ] ) ;
195+ Assert . Equal ( newPerson . FirstName , document . Data . Attributes [ "first-name" ] ) ;
196+ Assert . True ( document . Data . Relationships . ContainsKey ( "todo-items" ) ) ;
197+ Assert . Null ( document . Data . Relationships [ "todo-items" ] . ManyData ) ;
198+ Assert . Null ( document . Data . Relationships [ "todo-items" ] . SingleData ) ;
199+ }
200+
81201 [ Fact ]
82202 public async Task Can_Patch_Entity_And_HasOne_Relationships ( )
83203 {
@@ -92,7 +212,7 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
92212 . UseStartup < Startup > ( ) ;
93213 var server = new TestServer ( builder ) ;
94214 var client = server . CreateClient ( ) ;
95-
215+
96216 var content = new
97217 {
98218 data = new
0 commit comments