1- using System . Collections . Generic ;
21using System . Linq ;
32using System . Net ;
43using System . Net . Http ;
76using Bogus ;
87using DotNetCoreDocs ;
98using DotNetCoreDocs . Writers ;
10- using JsonApiDotNetCore . Serialization ;
11- using JsonApiDotNetCore . Services ;
129using JsonApiDotNetCoreExample ;
1310using JsonApiDotNetCoreExample . Data ;
1411using JsonApiDotNetCoreExample . Models ;
1512using Microsoft . AspNetCore . Hosting ;
1613using Microsoft . AspNetCore . TestHost ;
1714using Newtonsoft . Json ;
1815using Xunit ;
16+ using Person = JsonApiDotNetCoreExample . Models . Person ;
1917
2018namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
2119{
@@ -25,14 +23,18 @@ public class UpdatingDataTests
2523 private DocsFixture < Startup , JsonDocWriter > _fixture ;
2624 private AppDbContext _context ;
2725 private Faker < TodoItem > _todoItemFaker ;
26+ private Faker < Person > _personFaker ;
2827
2928 public UpdatingDataTests ( DocsFixture < Startup , JsonDocWriter > fixture )
3029 {
3130 _fixture = fixture ;
3231 _context = fixture . GetService < AppDbContext > ( ) ;
33- _todoItemFaker = new Faker < TodoItem > ( )
32+ _todoItemFaker = new Faker < TodoItem > ( )
3433 . RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
3534 . RuleFor ( t => t . Ordinal , f => f . Random . Number ( ) ) ;
35+ _personFaker = new Faker < Person > ( )
36+ . RuleFor ( p => p . FirstName , f => f . Name . FirstName ( ) )
37+ . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
3638 }
3739
3840 [ Fact ]
@@ -73,5 +75,61 @@ public async Task Respond_404_If_EntityDoesNotExist()
7375 // Assert
7476 Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
7577 }
78+
79+ [ Fact ]
80+ public async Task Can_Patch_Entity_And_HasOne_Relationships ( )
81+ {
82+ // arrange
83+ var todoItem = _todoItemFaker . Generate ( ) ;
84+ var person = _personFaker . Generate ( ) ;
85+ _context . TodoItems . Add ( todoItem ) ;
86+ _context . People . Add ( person ) ;
87+ _context . SaveChanges ( ) ;
88+
89+ var builder = new WebHostBuilder ( )
90+ . UseStartup < Startup > ( ) ;
91+ var server = new TestServer ( builder ) ;
92+ var client = server . CreateClient ( ) ;
93+
94+ var content = new
95+ {
96+ data = new
97+ {
98+ type = "todo-items" ,
99+ attributes = new
100+ {
101+ description = todoItem . Description ,
102+ ordinal = todoItem . Ordinal
103+ } ,
104+ relationships = new
105+ {
106+ owner = new
107+ {
108+ data = new
109+ {
110+ type = "people" ,
111+ id = person . Id . ToString ( )
112+ }
113+ }
114+ }
115+ }
116+ } ;
117+
118+ var httpMethod = new HttpMethod ( "PATCH" ) ;
119+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
120+ var request = new HttpRequestMessage ( httpMethod , route ) ;
121+
122+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
123+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
124+
125+ // Act
126+ var response = await client . SendAsync ( request ) ;
127+ var updatedTodoItem = _context . TodoItems
128+ . SingleOrDefault ( t => t . Id == todoItem . Id ) ;
129+
130+ // Assert
131+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
132+ Assert . Equal ( person . Id , updatedTodoItem . OwnerId ) ;
133+ }
76134 }
77135}
0 commit comments