1
- using System . Collections . Generic ;
2
1
using System . Linq ;
3
2
using System . Net ;
4
3
using System . Net . Http ;
7
6
using Bogus ;
8
7
using DotNetCoreDocs ;
9
8
using DotNetCoreDocs . Writers ;
10
- using JsonApiDotNetCore . Serialization ;
11
- using JsonApiDotNetCore . Services ;
12
9
using JsonApiDotNetCoreExample ;
13
10
using JsonApiDotNetCoreExample . Data ;
14
11
using JsonApiDotNetCoreExample . Models ;
15
12
using Microsoft . AspNetCore . Hosting ;
16
13
using Microsoft . AspNetCore . TestHost ;
14
+ using Microsoft . EntityFrameworkCore ;
17
15
using Newtonsoft . Json ;
18
16
using Xunit ;
17
+ using Person = JsonApiDotNetCoreExample . Models . Person ;
19
18
20
19
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
21
20
{
@@ -25,14 +24,18 @@ public class UpdatingDataTests
25
24
private DocsFixture < Startup , JsonDocWriter > _fixture ;
26
25
private AppDbContext _context ;
27
26
private Faker < TodoItem > _todoItemFaker ;
27
+ private Faker < Person > _personFaker ;
28
28
29
29
public UpdatingDataTests ( DocsFixture < Startup , JsonDocWriter > fixture )
30
30
{
31
31
_fixture = fixture ;
32
32
_context = fixture . GetService < AppDbContext > ( ) ;
33
- _todoItemFaker = new Faker < TodoItem > ( )
33
+ _todoItemFaker = new Faker < TodoItem > ( )
34
34
. RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
35
35
. 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 ( ) ) ;
36
39
}
37
40
38
41
[ Fact ]
@@ -73,5 +76,62 @@ public async Task Respond_404_If_EntityDoesNotExist()
73
76
// Assert
74
77
Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
75
78
}
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
+ }
76
136
}
77
137
}
0 commit comments