-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathGetRelationshipTests.cs
87 lines (77 loc) · 2.83 KB
/
GetRelationshipTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using Bogus;
using JsonApiDotNetCore.Models.Operations;
using JsonApiDotNetCoreExample.Data;
using OperationsExampleTests.Factories;
using Xunit;
namespace OperationsExampleTests
{
public class GetRelationshipTests : Fixture, IDisposable
{
private readonly Faker _faker = new Faker();
[Fact]
public async Task Can_Get_HasOne_Relationship()
{
// arrange
var context = GetService<AppDbContext>();
var author = AuthorFactory.Get();
var article = ArticleFactory.Get();
article.Author = author;
context.Articles.Add(article);
context.SaveChanges();
var content = new
{
operations = new[] {
new Dictionary<string, object> {
{ "op", "get"},
{ "ref", new { type = "articles", id = article.StringId, relationship = "author" } }
}
}
};
// act
var (response, data) = await PatchAsync<OperationsDocument>("api/bulk", content);
// assert
Assert.NotNull(response);
Assert.NotNull(data);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Single(data.Operations);
var resourceObject = data.Operations.Single().DataObject;
Assert.Equal(author.Id.ToString(), resourceObject.Id);
Assert.Equal("authors", resourceObject.Type);
}
[Fact]
public async Task Can_Get_HasMany_Relationship()
{
// arrange
var context = GetService<AppDbContext>();
var author = AuthorFactory.Get();
var article = ArticleFactory.Get();
article.Author = author;
context.Articles.Add(article);
context.SaveChanges();
var content = new
{
operations = new[] {
new Dictionary<string, object> {
{ "op", "get"},
{ "ref", new { type = "authors", id = author.StringId, relationship = "articles" } }
}
}
};
// act
var (response, data) = await PatchAsync<OperationsDocument>("api/bulk", content);
// assert
Assert.NotNull(response);
Assert.NotNull(data);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Single(data.Operations);
var resourceObject = data.Operations.Single().DataList.Single();
Assert.Equal(article.Id.ToString(), resourceObject.Id);
Assert.Equal("articles", resourceObject.Type);
}
}
}