-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathCommitCohortExample.cs
More file actions
106 lines (87 loc) · 4.82 KB
/
Copy pathCommitCohortExample.cs
File metadata and controls
106 lines (87 loc) · 4.82 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
// 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.DataExport.Data;
using Rdmp.Core.DataFlowPipeline;
using Rdmp.Core.ReusableLibraryCode.Checks;
using Rdmp.Core.ReusableLibraryCode.Progress;
using System.Data;
using System.Linq;
using Tests.Common;
namespace Rdmp.Core.Tests.CohortCommitting;
internal class CommitCohortExample : DatabaseTests
{
[TestCase(DatabaseType.MicrosoftSQLServer, "varchar(10)")]
[TestCase(DatabaseType.MySql, "varchar(10)")]
[TestCase(DatabaseType.Oracle, "varchar2(10)")]
public void CommitCohortExampleTest(DatabaseType dbType, string privateDataType)
{
RunBlitzDatabases(RepositoryLocator);
//find the test server (where we will create the store schema)
var db = GetCleanedServer(dbType);
//create the cohort store table
var wizard = new CreateNewCohortDatabaseWizard(db, CatalogueRepository, DataExportRepository, false);
var privateColumn = new PrivateIdentifierPrototype("chi", privateDataType);
var externalCohortTable = wizard.CreateDatabase(privateColumn, ThrowImmediatelyCheckNotifier.Quiet);
Assert.That(externalCohortTable.DatabaseType, Is.EqualTo(dbType));
//create a project into which we want to import a cohort
var project = new Project(DataExportRepository, "MyProject")
{
ProjectNumber = 500
};
project.SaveToDatabase();
//create a description of the cohort we are importing
var definition = new CohortDefinition(null, "MyCohort", 1, 500, externalCohortTable);
//create our cohort (normally this would be read from a file or be the results of cohort identification query)
var dt = new DataTable();
dt.Columns.Add("chi");
dt.Rows.Add("0101010101");
dt.Rows.Add("0202020202");
//Create a pipeline (we only need the destination)
var pipelineDestination = new BasicCohortDestination
{
//choose how to allocate the anonymous release identifiers
ReleaseIdentifierAllocator = typeof(ProjectConsistentGuidReleaseIdentifierAllocator)
};
//initialize the destination
pipelineDestination.PreInitialize(new ThrowImmediatelyActivator(RepositoryLocator, null),
new CohortCreationRequest(project, definition, DataExportRepository,
"A cohort created in an example unit test"),
ThrowImmediatelyDataLoadEventListener.Quiet);
//process the cohort data table
pipelineDestination.ProcessPipelineData(dt, ThrowImmediatelyDataLoadEventListener.Quiet,
new GracefulCancellationToken());
//there should be no cohorts yet
Assert.That(DataExportRepository.GetAllObjects<ExtractableCohort>(), Is.Empty);
//dispose of the pipeline
pipelineDestination.Dispose(ThrowImmediatelyDataLoadEventListener.Quiet, null);
//now there should be one
var cohort = DataExportRepository.GetAllObjects<ExtractableCohort>().Single();
Assert.Multiple(() =>
{
//make sure we are all on the same page about what the DBMS type is (nothing cached etc)
Assert.That(cohort.ExternalCohortTable.DatabaseType, Is.EqualTo(dbType));
Assert.That(cohort.GetQuerySyntaxHelper().DatabaseType, Is.EqualTo(dbType));
Assert.That(cohort.ExternalProjectNumber, Is.EqualTo(500));
Assert.That(cohort.CountDistinct, Is.EqualTo(2));
});
var tbl = externalCohortTable.DiscoverCohortTable();
Assert.That(tbl.GetRowCount(), Is.EqualTo(2));
var dtInDatabase = tbl.GetDataTable();
Assert.Multiple(() =>
{
//guid will be something like "6fb23de5-e8eb-46eb-84b5-dd368da21073"
Assert.That(dtInDatabase.Rows[0]["ReleaseId"].ToString(), Has.Length.EqualTo(36));
Assert.That(dtInDatabase.Rows[0]["chi"], Is.EqualTo("0101010101"));
});
}
}