-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCreateNewCohortDatabaseWizardTests.cs
More file actions
285 lines (215 loc) · 11.6 KB
/
CreateNewCohortDatabaseWizardTests.cs
File metadata and controls
285 lines (215 loc) · 11.6 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
// Copyright (c) The University of Dundee 2018-2019
// This file is part of the Research Data Management Platform (RDMP).
// RDMP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
// RDMP is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with RDMP. If not, see <https://www.gnu.org/licenses/>.
using FAnsi;
using NUnit.Framework;
using Rdmp.Core.CohortCommitting;
using Rdmp.Core.CohortCommitting.Pipeline;
using Rdmp.Core.CohortCommitting.Pipeline.Destinations;
using Rdmp.Core.CohortCommitting.Pipeline.Destinations.IdentifierAllocation;
using Rdmp.Core.CommandExecution;
using Rdmp.Core.Curation.Data;
using Rdmp.Core.DataExport.Data;
using Rdmp.Core.DataFlowPipeline;
using Rdmp.Core.Providers;
using Rdmp.Core.ReusableLibraryCode.Checks;
using Rdmp.Core.ReusableLibraryCode.Progress;
using Rdmp.Core.ReusableLibraryCode.Settings;
using System;
using System.Data;
using System.Linq;
using Tests.Common;
namespace Rdmp.Core.Tests.CohortCommitting;
public class CreateNewCohortDatabaseWizardTests : DatabaseTests
{
private Catalogue _cata1;
private Catalogue _cata2;
private TableInfo _t1;
private TableInfo _t2;
private ColumnInfo _c1;
private ColumnInfo _c2;
private CatalogueItem _ci1;
private CatalogueItem _ci2;
private ExtractionInformation _extractionInfo1;
private ExtractionInformation _extractionInfo2;
[SetUp]
protected override void SetUp()
{
base.SetUp();
RunBlitzDatabases(RepositoryLocator);
_cata1 = new Catalogue(CatalogueRepository, "Dataset1");
_cata2 = new Catalogue(CatalogueRepository, "Dataset2");
_t1 = new TableInfo(CatalogueRepository, "T1");
_t2 = new TableInfo(CatalogueRepository, "T2");
_c1 = new ColumnInfo(CatalogueRepository, "PrivateIdentifierA", "varchar(10)", _t1);
_c2 = new ColumnInfo(CatalogueRepository, "PrivateIdentifierB", "int", _t2);
_ci1 = new CatalogueItem(CatalogueRepository, _cata1, "PrivateIdentifierA");
_ci2 = new CatalogueItem(CatalogueRepository, _cata2, "PrivateIdentifierB");
_extractionInfo1 = new ExtractionInformation(CatalogueRepository, _ci1, _c1, _c1.ToString());
_extractionInfo2 = new ExtractionInformation(CatalogueRepository, _ci2, _c2, _c2.ToString());
cohortDatabaseName = TestDatabaseNames.GetConsistentName("Tests_CreateCohortDatabaseWizard");
}
private string cohortDatabaseName;
[Test]
public void TestMissingColumnInfos()
{
_extractionInfo1.IsExtractionIdentifier = true;
_extractionInfo1.SaveToDatabase();
var wizard = new CreateNewCohortDatabaseWizard(null, CatalogueRepository, DataExportRepository, false);
//it finds it!
Assert.That(wizard.GetPrivateIdentifierCandidates()
.Any(prototype => prototype.RuntimeName.Equals("PrivateIdentifierA")));
//delete the column info to make it a missing reference
_c1.DeleteInDatabase();
//now it should gracefully skip over it
Assert.That(wizard.GetPrivateIdentifierCandidates()
.Any(prototype => prototype.RuntimeName.Equals("PrivateIdentifierA")), Is.False);
}
[Test]
public void ProposePrivateIdentifierDatatypes()
{
var wizard = new CreateNewCohortDatabaseWizard(null, CatalogueRepository, DataExportRepository, false);
var candidates = wizard.GetPrivateIdentifierCandidates();
Assert.That(candidates.Any(c =>
c.RuntimeName.Equals("PrivateIdentifierA") || c.RuntimeName.Equals("PrivateIdentifierB")), Is.False);
_extractionInfo1.IsExtractionIdentifier = true;
_extractionInfo1.SaveToDatabase();
candidates = wizard.GetPrivateIdentifierCandidates();
var candidate = candidates.Single(c => c.RuntimeName.Equals("PrivateIdentifierA"));
Assert.Multiple(() =>
{
Assert.That(candidate.DataType, Is.EqualTo("varchar(10)"));
Assert.That(candidate.MatchingExtractionInformations.Single().ID, Is.EqualTo(_extractionInfo1.ID));
});
}
[TestCase("text")]
//[TestCase("ntext")] // TODO: FAnsiSql doesn't know that this is max width
[TestCase("varchar(max)")]
[TestCase("nvarchar(max)")]
public void TestVarcharMaxNotAllowed(string term)
{
var db = GetCleanedServer(DatabaseType.MicrosoftSQLServer);
var wizard = new CreateNewCohortDatabaseWizard(db, CatalogueRepository, DataExportRepository, false);
_extractionInfo2.IsExtractionIdentifier = true;
_c2.Data_type = term;
_c2.SaveToDatabase();
_extractionInfo2.SaveToDatabase();
var candidate = wizard.GetPrivateIdentifierCandidates().Single(c => c.RuntimeName.Equals("PrivateIdentifierB"));
var ex = Assert.Throws<Exception>(() => wizard.CreateDatabase(
candidate,
ThrowImmediatelyCheckNotifier.Quiet));
Assert.That(
ex.Message, Is.EqualTo("Private identifier datatype cannot be varchar(max) style as this prevents Primary Key creation on the table"));
}
[TestCaseSource(typeof(All), nameof(All.DatabaseTypes))]
public void TestActuallyCreatingIt(DatabaseType type)
{
var db = GetCleanedServer(type);
var wizard = new CreateNewCohortDatabaseWizard(db, CatalogueRepository, DataExportRepository, false);
_extractionInfo2.IsExtractionIdentifier = true;
_extractionInfo2.SaveToDatabase();
var candidate = wizard.GetPrivateIdentifierCandidates().Single(c => c.RuntimeName.Equals("PrivateIdentifierB"));
var ect = wizard.CreateDatabase(
candidate,
ThrowImmediatelyCheckNotifier.Quiet);
Assert.That(ect.DatabaseType, Is.EqualTo(type));
//database should exist
DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase(cohortDatabaseName);
Assert.Multiple(() =>
{
Assert.That(db.Exists());
//did it create the correct type?
Assert.That(ect.DatabaseType, Is.EqualTo(type));
});
//the ExternalCohortTable should pass tests
ect.Check(ThrowImmediatelyCheckNotifier.Quiet);
//now try putting someone in it
//the project it will go under
var project = new Project(DataExportRepository, "MyProject")
{
ProjectNumber = 10
};
project.SaveToDatabase();
//the request to put it under there
var request = new CohortCreationRequest(project, new CohortDefinition(null, "My cohort", 1, 10, ect),
DataExportRepository, "Blah");
//the actual cohort data
var dt = new DataTable();
dt.Columns.Add(_extractionInfo2.GetRuntimeName());
dt.Rows.Add(101243); //_extractionInfo2 is of type int
//the destination component that will put it there
var dest = new BasicCohortDestination();
dest.PreInitialize(new ThrowImmediatelyActivator(RepositoryLocator, null), request, ThrowImmediatelyDataLoadEventListener.Quiet);
//tell it to use the guid allocator
dest.ReleaseIdentifierAllocator = typeof(GuidReleaseIdentifierAllocator);
dest.ProcessPipelineData(dt, ThrowImmediatelyDataLoadEventListener.Quiet, new GracefulCancellationToken());
dest.Dispose(ThrowImmediatelyDataLoadEventListener.Quiet, null);
var cohort = request.CohortCreatedIfAny;
Assert.That(cohort, Is.Not.Null);
var externalData = cohort.GetExternalData();
Assert.Multiple(() =>
{
Assert.That(externalData.ExternalProjectNumber, Is.EqualTo(10));
Assert.That(string.IsNullOrEmpty(externalData.ExternalDescription), Is.False);
Assert.That(externalData.ExternalCohortCreationDate.Value.Year, Is.EqualTo(DateTime.Now.Year));
Assert.That(externalData.ExternalCohortCreationDate.Value.Month, Is.EqualTo(DateTime.Now.Month));
Assert.That(externalData.ExternalCohortCreationDate.Value.Day, Is.EqualTo(DateTime.Now.Day));
Assert.That(externalData.ExternalCohortCreationDate.Value.Hour, Is.EqualTo(DateTime.Now.Hour));
});
cohort.AppendToAuditLog("Test");
Assert.Multiple(() =>
{
Assert.That(cohort.AuditLog, Does.Contain("Test"));
Assert.That(cohort.Count, Is.EqualTo(1));
});
Assert.That(cohort.CountDistinct, Is.EqualTo(1));
var cohortTable = cohort.FetchEntireCohort();
Assert.That(cohortTable.Rows, Has.Count.EqualTo(1));
var helper = ect.GetQuerySyntaxHelper();
Assert.That(cohortTable.Rows[0][helper.GetRuntimeName(ect.PrivateIdentifierField)], Is.EqualTo(101243));
var aguid = cohortTable.Rows[0][helper.GetRuntimeName(ect.ReleaseIdentifierField)].ToString();
Assert.That(string.IsNullOrWhiteSpace(aguid), Is.False); //should be a guid
//test reversing the anonymisation of something
var dtAno = new DataTable();
dtAno.Columns.Add(cohort.GetReleaseIdentifier(true));
dtAno.Columns.Add("Age");
dtAno.Rows.Add(aguid, 23);
dtAno.Rows.Add(aguid, 99);
cohort.ReverseAnonymiseDataTable(dtAno, ThrowImmediatelyDataLoadEventListener.Quiet, true);
Assert.That(dtAno.Columns, Has.Count.EqualTo(2));
Assert.Multiple(() =>
{
Assert.That(dtAno.Columns.Contains(cohort.GetPrivateIdentifier(true)));
Assert.That(dtAno.Rows[0][cohort.GetPrivateIdentifier(true)], Is.EqualTo("101243"));
Assert.That(dtAno.Rows[1][cohort.GetPrivateIdentifier(true)], Is.EqualTo("101243"));
});
//make sure that it shows up in the child provider (provides fast object access in CLI and builds tree model for UI)
var repo = new DataExportChildProvider(RepositoryLocator, null, ThrowImmediatelyCheckNotifier.Quiet, null);
var descendancy = repo.GetDescendancyListIfAnyFor(cohort);
Assert.That(descendancy, Is.Not.Null);
}
[Test]
public void Test_IdentifiableExtractions()
{
var db = GetCleanedServer(DatabaseType.MicrosoftSQLServer);
var wizard = new CreateNewCohortDatabaseWizard(db, CatalogueRepository, DataExportRepository, false);
_extractionInfo2.IsExtractionIdentifier = true;
_extractionInfo2.SaveToDatabase();
var candidate = wizard.GetPrivateIdentifierCandidates().Single(c => c.RuntimeName.Equals("PrivateIdentifierB"));
var ect = wizard.CreateDatabase(
candidate,
ThrowImmediatelyCheckNotifier.Quiet);
ect.Check(ThrowImmediatelyCheckNotifier.Quiet);
ect.ReleaseIdentifierField = ect.PrivateIdentifierField;
ect.SaveToDatabase();
UserSettings.SetErrorReportingLevelFor(ErrorCodes.ExtractionIsIdentifiable, CheckResult.Fail);
var ex = Assert.Throws<Exception>(() => ect.Check(ThrowImmediatelyCheckNotifier.Quiet));
Assert.That(
ex.Message, Is.EqualTo("R004 PrivateIdentifierField and ReleaseIdentifierField are the same, this means your cohort will extract identifiable data (no cohort identifier substitution takes place)"));
UserSettings.SetErrorReportingLevelFor(ErrorCodes.ExtractionIsIdentifiable, CheckResult.Warning);
ect.Check(ThrowImmediatelyCheckNotifier.Quiet);
UserSettings.SetErrorReportingLevelFor(ErrorCodes.ExtractionIsIdentifiable, CheckResult.Fail);
}
}