-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAddCohortDistributionTests.cs
More file actions
374 lines (333 loc) · 14.4 KB
/
AddCohortDistributionTests.cs
File metadata and controls
374 lines (333 loc) · 14.4 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
namespace NHS.CohortManager.Tests.CohortDistributionServiceTests;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Common.Interfaces;
using Data.Database;
using DataServices.Client;
using Model;
using Moq;
using System.Linq.Expressions;
[TestClass]
public class AddCohortDistributionTests
{
private Mock<IDataServiceClient<CohortDistribution>> _cohortDistributionDataServiceClient;
private Mock<IDataServiceClient<BsSelectRequestAudit>> _bsSelectRequestAuditDataServiceClient;
private IExtractCohortDistributionRecordsStrategy _extractionStrategy;
private CreateCohortDistributionData _service;
private List<CohortDistribution> _participantList;
[TestInitialize]
public void Setup()
{
_cohortDistributionDataServiceClient = new Mock<IDataServiceClient<CohortDistribution>>();
_bsSelectRequestAuditDataServiceClient = new Mock<IDataServiceClient<BsSelectRequestAudit>>();
_participantList = new List<CohortDistribution>();
_cohortDistributionDataServiceClient
.Setup(x => x.GetByFilter(It.IsAny<Expression<Func<CohortDistribution, bool>>>()))
.ReturnsAsync((Expression<Func<CohortDistribution, bool>> filter) =>
_participantList.Where(filter.Compile()).ToList());
_cohortDistributionDataServiceClient
.Setup(x => x.GetByFilter(It.IsAny<Expression<Func<CohortDistribution, bool>>>(), It.IsAny<Expression<Func<CohortDistribution, object>>>(), It.IsAny<int>()))
.ReturnsAsync((Expression<Func<CohortDistribution, bool>> filter, Expression<Func<CohortDistribution, object>> orderBy, int take) =>
_participantList.Where(filter.Compile()).OrderBy(orderBy.Compile()).Take(take).ToList());
_cohortDistributionDataServiceClient
.Setup(x => x.GetSingle(It.IsAny<string>()))
.ReturnsAsync((string id) =>
_participantList.FirstOrDefault(p => p.CohortDistributionId.ToString() == id));
_cohortDistributionDataServiceClient
.Setup(x => x.Update(It.IsAny<CohortDistribution>()))
.ReturnsAsync((CohortDistribution cohort) =>
{
var existing = _participantList.FirstOrDefault(c => c.CohortDistributionId == cohort.CohortDistributionId);
if (existing != null)
{
existing.IsExtracted = cohort.IsExtracted;
existing.RequestId = cohort.RequestId;
return true;
}
return false;
});
_extractionStrategy = new ExtractCohortDistributionRecords(_cohortDistributionDataServiceClient.Object);
_service = new CreateCohortDistributionData(
_cohortDistributionDataServiceClient.Object,
_bsSelectRequestAuditDataServiceClient.Object,
_extractionStrategy
);
}
// Tests for old logic when retrieveSupersededRecordsLast is false
[TestMethod]
public async Task GetUnextractedCohortDistributionParticipants_ReturnsOneRecordWhenRetrieveSupersededRecordsLastIsFalse()
{
// Arrange
_participantList.AddRange(new List<CohortDistribution>
{
new CohortDistribution
{
ParticipantId = 1,
CohortDistributionId = 1,
NHSNumber = 99900000000,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = null,
RecordInsertDateTime = DateTime.UtcNow.AddDays(-1)
},
new CohortDistribution
{
ParticipantId = 2,
CohortDistributionId = 2,
NHSNumber = 99900000001,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = 99988877777,
RecordInsertDateTime = DateTime.UtcNow.AddDays(-1)
}
});
// Act
var result = await _service.GetUnextractedCohortDistributionParticipants(10, false);
// Assert
Assert.IsTrue(result.Count == 2, "Should return two records when retrieveSupersededRecordsLast is false.");
}
[TestMethod]
public async Task GetUnextractedCohortDistributionParticipants_ReturnsUpToRowCount()
{
// Arrange - Add 5 participants
for (int i = 1; i <= 5; i++)
{
_participantList.Add(new CohortDistribution
{
ParticipantId = i,
CohortDistributionId = i,
NHSNumber = 9990000000 + i,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = null,
RecordInsertDateTime = DateTime.UtcNow.AddMinutes(-i)
});
}
// Act
var result = await _service.GetUnextractedCohortDistributionParticipants(3, false);
// Assert
Assert.AreEqual(3, result.Count());
}
[TestMethod]
public async Task GetUnextractedCohortDistributionParticipants_ReturnsExpectedDto()
{
// Arrange
_participantList.Add(new CohortDistribution
{
ParticipantId = 1,
CohortDistributionId = 1,
NHSNumber = 99900000000,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = 99900000001,
RecordInsertDateTime = DateTime.UtcNow.AddDays(-1)
});
// Act - Tests real filtering and DTO mapping logic
var result = await _service.GetUnextractedCohortDistributionParticipants(10, false);
// Assert
Assert.AreEqual(1, result.Count);
Assert.AreEqual("99900000000", result[0].NhsNumber);
Assert.AreEqual("99900000001", result[0].SupersededByNhsNumber);
Assert.AreEqual("1", result[0].ParticipantId);
Assert.AreEqual("0", result[0].IsExtracted); // This has been updated in the DB but DTO reflects original value
Assert.IsTrue(Guid.TryParse(result[0].RequestId, out var guid) && guid != Guid.Empty, "RequestId should be a non-empty GUID.");
}
[TestMethod]
public async Task GetCohortDistributionParticipantsByRequestId_ReturnsEmptyForUnknownRequestId()
{
// Arrange
var knownRequestId = Guid.NewGuid();
var unknownRequestId = Guid.NewGuid();
_participantList.Add(new CohortDistribution
{
ParticipantId = 1,
CohortDistributionId = 1,
NHSNumber = 99900000000,
IsExtracted = 1,
RequestId = knownRequestId,
SupersededNHSNumber = 99900000001,
RecordInsertDateTime = DateTime.UtcNow.AddDays(-1)
});
// Act - Tests filtering by RequestId
var result = await _service.GetCohortDistributionParticipantsByRequestId(unknownRequestId);
// Assert
Assert.AreEqual(0, result.Count, "Should return no participants for an unknown requestId.");
}
[TestMethod]
public async Task GetUnextractedCohortDistributionParticipants_OnlyReturnsSupersededWithMatchingNhsNumber()
{
// Arrange
var supersededNhsNumberWithMatch = 99988888888;
_participantList.Add(new CohortDistribution
{
ParticipantId = 1,
CohortDistributionId = 1,
NHSNumber = 99900000001,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = supersededNhsNumberWithMatch,
RecordInsertDateTime = DateTime.UtcNow.AddDays(-1)
});
// Act
var result = await _service.GetUnextractedCohortDistributionParticipants(10, false);
// Assert
Assert.AreEqual(1, result.Count, "Only one superseded participant should be extracted (the one with a matching NHSNumber).");
Assert.AreEqual("99900000001", result[0].NhsNumber);
Assert.AreEqual(supersededNhsNumberWithMatch.ToString(), result[0].SupersededByNhsNumber);
}
// Tests for new logic when retrieveSupersededRecordsLast is true
[TestMethod]
public async Task GetUnextractedCohortDistributionParticipants_ReturnsEmptyWhenNoSupersededMatch()
{
// Arrange
_participantList.Add(new CohortDistribution
{
ParticipantId = 1,
CohortDistributionId = 1,
NHSNumber = 99900000001,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = 99988888888, // No matching extracted record with this NHS number
RecordInsertDateTime = DateTime.UtcNow.AddDays(-1)
});
// Act
var result = await _service.GetUnextractedCohortDistributionParticipants(10, true);
// Assert
Assert.AreEqual(0, result.Count, "Should return no participants when there is no matching extracted NHSNumber.");
}
[TestMethod]
public async Task GetUnextractedCohortDistributionParticipants_UsesExtractionStrategyWhenRetrieveSupersededRecordsLastIsTrue()
{
// Arrange
_participantList.Add(new CohortDistribution
{
ParticipantId = 1,
CohortDistributionId = 1,
NHSNumber = 99900000000,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = null,
RecordInsertDateTime = DateTime.UtcNow.AddDays(-1)
});
// Act - This now tests the real ExtractCohortDistributionRecords strategy
var result = await _service.GetUnextractedCohortDistributionParticipants(10, true);
// Assert - Verify the strategy correctly extracted the participant
Assert.AreEqual(1, result.Count);
Assert.AreEqual("99900000000", result[0].NhsNumber);
}
[TestMethod]
public async Task GetUnextractedCohortDistributionParticipants_ReturnsAllWhenAllSupersededMatch()
{
// Arrange
var supersededNhsNumbers = new[] { 99911111111, 99922222222};
// Add the extracted records that the superseded records will match against
_participantList.AddRange(new List<CohortDistribution>
{
new CohortDistribution
{
ParticipantId = 10,
CohortDistributionId = 10,
NHSNumber = supersededNhsNumbers[0],
IsExtracted = 1,
RequestId = Guid.NewGuid(),
SupersededNHSNumber = null,
RecordInsertDateTime = DateTime.UtcNow.AddDays(-10)
},
new CohortDistribution
{
ParticipantId = 11,
CohortDistributionId = 11,
NHSNumber = supersededNhsNumbers[1],
IsExtracted = 1,
RequestId = Guid.NewGuid(),
SupersededNHSNumber = null,
RecordInsertDateTime = DateTime.UtcNow.AddDays(-11)
}
});
// Add the unextracted superseded participants
_participantList.AddRange(new List<CohortDistribution>
{
new CohortDistribution
{
ParticipantId = 1,
CohortDistributionId = 1,
NHSNumber = 99900000001,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = supersededNhsNumbers[0],
RecordInsertDateTime = DateTime.UtcNow.AddDays(-1)
},
new CohortDistribution
{
ParticipantId = 2,
CohortDistributionId = 2,
NHSNumber = 99900000002,
IsExtracted = 0,
RequestId = Guid.Empty,
SupersededNHSNumber = supersededNhsNumbers[1],
RecordInsertDateTime = DateTime.UtcNow.AddDays(-2)
}
});
// Act
var result = await _service.GetUnextractedCohortDistributionParticipants(10, true);
// Assert
Assert.AreEqual(2, result.Count, "Should return all superseded participants when all have a matching extracted NHSNumber.");
}
[TestMethod]
public async Task GetCohortDistributionParticipantsByRequestId_ReturnsExpectedDto()
{
// Arrange
var requestId = Guid.NewGuid();
var otherRequestId = Guid.NewGuid();
_participantList.AddRange(new List<CohortDistribution>
{
new CohortDistribution
{
ParticipantId = 1,
CohortDistributionId = 1,
NHSNumber = 99900000000,
IsExtracted = 1,
RequestId = requestId,
SupersededNHSNumber = 99900000001,
RecordInsertDateTime = DateTime.UtcNow
},
new CohortDistribution
{
ParticipantId = 2,
CohortDistributionId = 2,
NHSNumber = 99900000002,
IsExtracted = 1,
RequestId = otherRequestId,
SupersededNHSNumber = null,
RecordInsertDateTime = DateTime.UtcNow
},
new CohortDistribution
{
ParticipantId = 3,
CohortDistributionId = 3,
NHSNumber = 99900000003,
IsExtracted = 1,
RequestId = requestId,
SupersededNHSNumber = null,
RecordInsertDateTime = DateTime.UtcNow
}
});
// Act
var result = await _service.GetCohortDistributionParticipantsByRequestId(requestId);
// Assert correct participants are returned. This should return all records with the requestId, regardless of superseded by nhs number status
Assert.AreEqual(2, result.Count, "Should return only participants with matching RequestId");
Assert.IsTrue(result.All(r => r.RequestId == requestId.ToString()), "All returned DTOs should have the correct RequestId.");
// Assert returned DTOs match expected data
var expected = _participantList.Where(c => c.RequestId == requestId).ToList();
for (int i = 0; i < 2; i++)
{
Assert.AreEqual(expected[i].NHSNumber.ToString(), result[i].NhsNumber);
Assert.AreEqual(expected[i].SupersededNHSNumber?.ToString() ?? "", result[i].SupersededByNhsNumber);
Assert.AreEqual(expected[i].ParticipantId.ToString(), result[i].ParticipantId);
Assert.AreEqual(expected[i].IsExtracted.ToString(), result[i].IsExtracted);
Assert.AreEqual(expected[i].RequestId.ToString(), result[i].RequestId);
}
}
}