-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathGetOrganizationReportSummaryDataByDateRangeQueryTests.cs
More file actions
243 lines (209 loc) · 11.1 KB
/
GetOrganizationReportSummaryDataByDateRangeQueryTests.cs
File metadata and controls
243 lines (209 loc) · 11.1 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
using AutoFixture;
using Bit.Core.Dirt.Models.Data;
using Bit.Core.Dirt.Reports.ReportFeatures;
using Bit.Core.Dirt.Repositories;
using Bit.Core.Exceptions;
using Bit.Test.Common.AutoFixture;
using Bit.Test.Common.AutoFixture.Attributes;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;
using ZiggyCreatures.Caching.Fusion;
namespace Bit.Core.Test.Dirt.ReportFeatures;
[SutProviderCustomize]
public class GetOrganizationReportSummaryDataByDateRangeQueryTests
{
[Theory]
[BitAutoData]
public async Task GetOrganizationReportSummaryDataByDateRangeAsync_WithValidParams_ShouldReturnSummaryData(
SutProvider<GetOrganizationReportSummaryDataByDateRangeQuery> sutProvider)
{
// Arrange
var fixture = new Fixture();
var organizationId = fixture.Create<Guid>();
var reportId = fixture.Create<Guid>();
var startDate = DateTime.UtcNow.AddDays(-30);
var endDate = DateTime.UtcNow;
var summaryDataList = fixture.Build<OrganizationReportSummaryDataResponse>()
.CreateMany(3).ToList();
summaryDataList[0].RevisionDate = DateTime.UtcNow; // most recent
summaryDataList[1].RevisionDate = DateTime.UtcNow.AddDays(-1);
summaryDataList[2].RevisionDate = DateTime.UtcNow.AddDays(-2);
sutProvider.GetDependency<IOrganizationReportRepository>()
.GetSummaryDataByDateRangeAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<DateTime>())
.Returns(summaryDataList);
sutProvider
.GetDependency<IFusionCache>()
.GetOrSetAsync(
key: Arg.Any<string?>(),
factory: Arg.Any<Func<object, CancellationToken, Task<IEnumerable<OrganizationReportSummaryDataResponse>>>>(),
options: Arg.Any<FusionCacheEntryOptions>(),
tags: Arg.Any<IEnumerable<string>>())
.Returns(callInfo =>
{
var factory = callInfo.ArgAt<Func<FusionCacheFactoryExecutionContext<IEnumerable<OrganizationReportSummaryDataResponse>>, CancellationToken, Task<IEnumerable<OrganizationReportSummaryDataResponse>>>>(1);
return new ValueTask<IEnumerable<OrganizationReportSummaryDataResponse>>(factory.Invoke(null, CancellationToken.None));
});
// Act
var result = await sutProvider.Sut.GetOrganizationReportSummaryDataByDateRangeAsync(organizationId, startDate, endDate);
// Assert
Assert.NotNull(result);
Assert.Equal(3, result.Count());
await sutProvider.GetDependency<IOrganizationReportRepository>()
.Received(1).GetSummaryDataByDateRangeAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<DateTime>());
}
[Theory]
[BitAutoData]
public async Task GetOrganizationReportSummaryDataByDateRangeAsync_ShouldReturnAllResults(
SutProvider<GetOrganizationReportSummaryDataByDateRangeQuery> sutProvider)
{
// Arrange
var fixture = new Fixture();
var organizationId = fixture.Create<Guid>();
var reportId = fixture.Create<Guid>();
var startDate = DateTime.UtcNow.AddDays(-30);
var endDate = DateTime.UtcNow;
var summaryDataList = fixture.Build<OrganizationReportSummaryDataResponse>()
.CreateMany(12)
.ToList();
summaryDataList[0].RevisionDate = DateTime.UtcNow; // most recent
summaryDataList[1].RevisionDate = DateTime.UtcNow.AddDays(-1);
summaryDataList[2].RevisionDate = DateTime.UtcNow.AddDays(-2);
summaryDataList[3].RevisionDate = DateTime.UtcNow.AddDays(-3);
summaryDataList[4].RevisionDate = DateTime.UtcNow.AddDays(-4);
summaryDataList[5].RevisionDate = DateTime.UtcNow.AddDays(-5);
summaryDataList[6].RevisionDate = DateTime.UtcNow.AddDays(-6);
summaryDataList[7].RevisionDate = DateTime.UtcNow.AddDays(-7);
summaryDataList[8].RevisionDate = DateTime.UtcNow.AddDays(-8);
summaryDataList[9].RevisionDate = DateTime.UtcNow.AddDays(-9);
summaryDataList[10].RevisionDate = DateTime.UtcNow.AddDays(-10);
summaryDataList[11].RevisionDate = DateTime.UtcNow.AddDays(-11);
sutProvider.GetDependency<IOrganizationReportRepository>()
.GetSummaryDataByDateRangeAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<DateTime>())
.Returns(summaryDataList);
sutProvider
.GetDependency<IFusionCache>()
.GetOrSetAsync(
key: Arg.Any<string?>(),
factory: Arg.Any<Func<object, CancellationToken, Task<IEnumerable<OrganizationReportSummaryDataResponse>>>>(),
options: Arg.Any<FusionCacheEntryOptions>(),
tags: Arg.Any<IEnumerable<string>>())
.Returns(callInfo =>
{
var factory = callInfo.ArgAt<Func<FusionCacheFactoryExecutionContext<IEnumerable<OrganizationReportSummaryDataResponse>>, CancellationToken, Task<IEnumerable<OrganizationReportSummaryDataResponse>>>>(1);
return new ValueTask<IEnumerable<OrganizationReportSummaryDataResponse>>(factory.Invoke(null, CancellationToken.None));
});
// Act
var result = await sutProvider.Sut.GetOrganizationReportSummaryDataByDateRangeAsync(organizationId, startDate, endDate);
// Assert
Assert.NotNull(result);
Assert.Equal(12, result.Count());
await sutProvider.GetDependency<IOrganizationReportRepository>()
.Received(1).GetSummaryDataByDateRangeAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<DateTime>());
}
[Theory]
[BitAutoData]
public async Task GetOrganizationReportSummaryDataByDateRangeAsync_WithEmptyOrganizationId_ShouldThrowBadRequestException(
SutProvider<GetOrganizationReportSummaryDataByDateRangeQuery> sutProvider)
{
// Arrange
var reportId = Guid.NewGuid();
var startDate = DateTime.UtcNow.AddDays(-30);
var endDate = DateTime.UtcNow;
// Act & Assert
var exception = await Assert.ThrowsAsync<BadRequestException>(async () =>
await sutProvider.Sut.GetOrganizationReportSummaryDataByDateRangeAsync(Guid.Empty, startDate, endDate));
Assert.Equal("OrganizationId is required", exception.Message);
await sutProvider.GetDependency<IOrganizationReportRepository>()
.DidNotReceive()
.GetSummaryDataByDateRangeAsync(
Arg.Any<Guid>(),
Arg.Any<DateTime>(),
Arg.Any<DateTime>());
}
[Theory]
[BitAutoData]
public async Task GetOrganizationReportSummaryDataByDateRangeAsync_WithStartDateAfterEndDate_ShouldThrowBadRequestException(
SutProvider<GetOrganizationReportSummaryDataByDateRangeQuery> sutProvider)
{
// Arrange
var organizationId = Guid.NewGuid();
var reportId = Guid.NewGuid();
var startDate = DateTime.UtcNow;
var endDate = DateTime.UtcNow.AddDays(-30);
// Act & Assert
var exception = await Assert.ThrowsAsync<BadRequestException>(async () =>
await sutProvider.Sut.GetOrganizationReportSummaryDataByDateRangeAsync(organizationId, startDate, endDate));
Assert.Equal("StartDate must be earlier than or equal to EndDate", exception.Message);
await sutProvider.GetDependency<IOrganizationReportRepository>()
.DidNotReceive().GetSummaryDataByDateRangeAsync(Arg.Any<Guid>(), Arg.Any<DateTime>(), Arg.Any<DateTime>());
}
[Theory]
[BitAutoData]
public async Task GetOrganizationReportSummaryDataByDateRangeAsync_WithEmptyResult_ShouldReturnEmptyList(
SutProvider<GetOrganizationReportSummaryDataByDateRangeQuery> sutProvider)
{
// Arrange
var organizationId = Guid.NewGuid();
var reportId = Guid.NewGuid();
var startDate = DateTime.UtcNow.AddDays(-30);
var endDate = DateTime.UtcNow;
sutProvider.GetDependency<IOrganizationReportRepository>()
.GetSummaryDataByDateRangeAsync(organizationId, startDate, endDate)
.Returns(new List<OrganizationReportSummaryDataResponse>());
sutProvider
.GetDependency<IFusionCache>()
.GetOrSetAsync(
key: Arg.Any<string?>(),
factory: Arg.Any<Func<object, CancellationToken, Task<IEnumerable<OrganizationReportSummaryDataResponse>>>>(),
options: Arg.Any<FusionCacheEntryOptions>(),
tags: Arg.Any<IEnumerable<string>>())
.Returns(callInfo =>
{
var factory = callInfo.ArgAt<Func<FusionCacheFactoryExecutionContext<IEnumerable<OrganizationReportSummaryDataResponse>>, CancellationToken, Task<IEnumerable<OrganizationReportSummaryDataResponse>>>>(1);
return new ValueTask<IEnumerable<OrganizationReportSummaryDataResponse>>(factory.Invoke(null, CancellationToken.None));
});
// Act
var result = await sutProvider.Sut.GetOrganizationReportSummaryDataByDateRangeAsync(organizationId, startDate, endDate);
// Assert
Assert.NotNull(result);
Assert.Empty(result);
}
[Theory]
[BitAutoData]
public async Task GetOrganizationReportSummaryDataByDateRangeAsync_WhenRepositoryThrowsException_ShouldPropagateException(
SutProvider<GetOrganizationReportSummaryDataByDateRangeQuery> sutProvider)
{
// Arrange
var organizationId = Guid.NewGuid();
var reportId = Guid.NewGuid();
var startDate = DateTime.UtcNow.AddDays(-30);
var endDate = DateTime.UtcNow;
var expectedMessage = "Database connection failed";
var repo = sutProvider.GetDependency<IOrganizationReportRepository>();
repo
.GetSummaryDataByDateRangeAsync(organizationId, startDate, endDate)
.Throws(new InvalidOperationException(expectedMessage));
sutProvider
.GetDependency<IFusionCache>()
.GetOrSetAsync(
key: Arg.Any<string?>(),
factory: Arg.Any<Func<object, CancellationToken, Task<IEnumerable<OrganizationReportSummaryDataResponse>>>>(),
options: Arg.Any<FusionCacheEntryOptions>(),
tags: Arg.Any<IEnumerable<string>>())
.Returns(callInfo =>
{
var factory = callInfo.ArgAt<Func<FusionCacheFactoryExecutionContext<IEnumerable<OrganizationReportSummaryDataResponse>>, CancellationToken, Task<IEnumerable<OrganizationReportSummaryDataResponse>>>>(1);
return new ValueTask<IEnumerable<OrganizationReportSummaryDataResponse>>(factory.Invoke(null, CancellationToken.None));
});
// Act & Assert
// var exception = await Assert.ThrowsAsync<InvalidOperationException>(async () =>
// await sutProvider.Sut.GetOrganizationReportSummaryDataByDateRangeAsync(organizationId, startDate, endDate));
var results = await sutProvider.Sut.GetOrganizationReportSummaryDataByDateRangeAsync(organizationId, startDate, endDate);
// Assert
// since the IFusionCache has a failsafe,
// the exception from the repository should be caught and logged, and an empty list should be returned
Assert.NotNull(results);
Assert.Empty(results);
}
}