-
-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathTopLevelCountTests.cs
152 lines (121 loc) · 4.73 KB
/
TopLevelCountTests.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.Net;
using System.Text.Json;
using FluentAssertions;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Serialization.Objects;
using Microsoft.Extensions.DependencyInjection;
using TestBuildingBlocks;
using Xunit;
namespace JsonApiDotNetCoreTests.IntegrationTests.Meta;
public sealed class TopLevelCountTests : IClassFixture<IntegrationTestContext<TestableStartup<MetaDbContext>, MetaDbContext>>
{
private readonly IntegrationTestContext<TestableStartup<MetaDbContext>, MetaDbContext> _testContext;
private readonly MetaFakers _fakers = new();
public TopLevelCountTests(IntegrationTestContext<TestableStartup<MetaDbContext>, MetaDbContext> testContext)
{
_testContext = testContext;
testContext.UseController<ProductFamiliesController>();
testContext.UseController<SupportTicketsController>();
testContext.ConfigureServicesAfterStartup(services =>
{
services.AddScoped(typeof(IResourceChangeTracker<>), typeof(NeverSameResourceChangeTracker<>));
});
var options = (JsonApiOptions)testContext.Factory.Services.GetRequiredService<IJsonApiOptions>();
options.IncludeTotalResourceCount = true;
}
[Fact]
public async Task Renders_resource_count_for_collection()
{
// Arrange
SupportTicket ticket = _fakers.SupportTicket.Generate();
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
await dbContext.ClearTableAsync<SupportTicket>();
dbContext.SupportTickets.Add(ticket);
await dbContext.SaveChangesAsync();
});
const string route = "/supportTickets";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
responseDocument.Meta.ShouldContainKey("total").With(value =>
{
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
element.GetInt32().Should().Be(1);
});
}
[Fact]
public async Task Renders_resource_count_for_empty_collection()
{
// Arrange
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
await dbContext.ClearTableAsync<SupportTicket>();
});
const string route = "/supportTickets";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecuteGetAsync<Document>(route);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
responseDocument.Meta.ShouldContainKey("total").With(value =>
{
JsonElement element = value.Should().BeOfType<JsonElement>().Subject;
element.GetInt32().Should().Be(0);
});
}
[Fact]
public async Task Hides_resource_count_in_create_resource_response()
{
// Arrange
string newDescription = _fakers.SupportTicket.Generate().Description;
var requestBody = new
{
data = new
{
type = "supportTickets",
attributes = new
{
description = newDescription
}
}
};
const string route = "/supportTickets";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePostAsync<Document>(route, requestBody);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.Created);
responseDocument.Meta.Should().BeNull();
}
[Fact]
public async Task Hides_resource_count_in_update_resource_response()
{
// Arrange
SupportTicket existingTicket = _fakers.SupportTicket.Generate();
string newDescription = _fakers.SupportTicket.Generate().Description;
await _testContext.RunOnDatabaseAsync(async dbContext =>
{
dbContext.SupportTickets.Add(existingTicket);
await dbContext.SaveChangesAsync();
});
var requestBody = new
{
data = new
{
type = "supportTickets",
id = existingTicket.StringId,
attributes = new
{
description = newDescription
}
}
};
string route = $"/supportTickets/{existingTicket.StringId}";
// Act
(HttpResponseMessage httpResponse, Document responseDocument) = await _testContext.ExecutePatchAsync<Document>(route, requestBody);
// Assert
httpResponse.ShouldHaveStatusCode(HttpStatusCode.OK);
responseDocument.Meta.Should().BeNull();
}
}