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 ;
14+ using Microsoft . EntityFrameworkCore ;
1715using Newtonsoft . Json ;
1816using Xunit ;
17+ using Person = JsonApiDotNetCoreExample . Models . Person ;
1918
2019namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
2120{
@@ -25,14 +24,18 @@ public class UpdatingDataTests
2524 private DocsFixture < Startup , JsonDocWriter > _fixture ;
2625 private AppDbContext _context ;
2726 private Faker < TodoItem > _todoItemFaker ;
27+ private Faker < Person > _personFaker ;
2828
2929 public UpdatingDataTests ( DocsFixture < Startup , JsonDocWriter > fixture )
3030 {
3131 _fixture = fixture ;
3232 _context = fixture . GetService < AppDbContext > ( ) ;
33- _todoItemFaker = new Faker < TodoItem > ( )
33+ _todoItemFaker = new Faker < TodoItem > ( )
3434 . RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
3535 . RuleFor ( t => t . Ordinal , f => f . Random . Number ( ) ) ;
36+ _personFaker = new Faker < Person > ( )
37+ . RuleFor ( p => p . FirstName , f => f . Name . FirstName ( ) )
38+ . RuleFor ( p => p . LastName , f => f . Name . LastName ( ) ) ;
3639 }
3740
3841 [ Fact ]
@@ -73,5 +76,62 @@ public async Task Respond_404_If_EntityDoesNotExist()
7376 // Assert
7477 Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
7578 }
79+
80+ [ Fact ]
81+ public async Task Can_Patch_Entity_And_HasOne_Relationships ( )
82+ {
83+ // arrange
84+ var todoItem = _todoItemFaker . Generate ( ) ;
85+ var person = _personFaker . Generate ( ) ;
86+ _context . TodoItems . Add ( todoItem ) ;
87+ _context . People . Add ( person ) ;
88+ _context . SaveChanges ( ) ;
89+
90+ var builder = new WebHostBuilder ( )
91+ . UseStartup < Startup > ( ) ;
92+ var server = new TestServer ( builder ) ;
93+ var client = server . CreateClient ( ) ;
94+
95+ var content = new
96+ {
97+ data = new
98+ {
99+ type = "todo-items" ,
100+ attributes = new
101+ {
102+ description = todoItem . Description ,
103+ ordinal = todoItem . Ordinal
104+ } ,
105+ relationships = new
106+ {
107+ owner = new
108+ {
109+ data = new
110+ {
111+ type = "people" ,
112+ id = person . Id . ToString ( )
113+ }
114+ }
115+ }
116+ }
117+ } ;
118+
119+ var httpMethod = new HttpMethod ( "PATCH" ) ;
120+ var route = $ "/api/v1/todo-items/{ todoItem . Id } ";
121+ var request = new HttpRequestMessage ( httpMethod , route ) ;
122+
123+ request . Content = new StringContent ( JsonConvert . SerializeObject ( content ) ) ;
124+ request . Content . Headers . ContentType = new MediaTypeHeaderValue ( "application/vnd.api+json" ) ;
125+
126+ // Act
127+ var response = await client . SendAsync ( request ) ;
128+ var updatedTodoItem = _context . TodoItems . AsNoTracking ( )
129+ . Include ( t => t . Owner )
130+ . SingleOrDefault ( t => t . Id == todoItem . Id ) ;
131+
132+ // Assert
133+ Assert . Equal ( HttpStatusCode . OK , response . StatusCode ) ;
134+ Assert . Equal ( person . Id , updatedTodoItem . OwnerId ) ;
135+ }
76136 }
77137}
0 commit comments