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 ;
17
14
using Newtonsoft . Json ;
18
15
using Xunit ;
16
+ using Person = JsonApiDotNetCoreExample . Models . Person ;
19
17
20
18
namespace JsonApiDotNetCoreExampleTests . Acceptance . Spec
21
19
{
@@ -25,14 +23,18 @@ public class UpdatingDataTests
25
23
private DocsFixture < Startup , JsonDocWriter > _fixture ;
26
24
private AppDbContext _context ;
27
25
private Faker < TodoItem > _todoItemFaker ;
26
+ private Faker < Person > _personFaker ;
28
27
29
28
public UpdatingDataTests ( DocsFixture < Startup , JsonDocWriter > fixture )
30
29
{
31
30
_fixture = fixture ;
32
31
_context = fixture . GetService < AppDbContext > ( ) ;
33
- _todoItemFaker = new Faker < TodoItem > ( )
32
+ _todoItemFaker = new Faker < TodoItem > ( )
34
33
. RuleFor ( t => t . Description , f => f . Lorem . Sentence ( ) )
35
34
. 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 ( ) ) ;
36
38
}
37
39
38
40
[ Fact ]
@@ -73,5 +75,61 @@ public async Task Respond_404_If_EntityDoesNotExist()
73
75
// Assert
74
76
Assert . Equal ( HttpStatusCode . NotFound , response . StatusCode ) ;
75
77
}
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
+ }
76
134
}
77
135
}
0 commit comments