Example #1
0
        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, new ThrowImmediatelyCheckNotifier());

            Assert.AreEqual(dbType, externalCohortTable.DatabaseType);

            //create a project into which we want to import a cohort
            var project = new Project(DataExportRepository, "MyProject");

            project.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
            pipelineDestination.ReleaseIdentifierAllocator = typeof(ProjectConsistentGuidReleaseIdentifierAllocator);

            //initialize the destination
            pipelineDestination.PreInitialize(
                new CohortCreationRequest(project, definition, DataExportRepository, "A cohort created in an example unit test"),
                new ThrowImmediatelyDataLoadEventListener());

            //process the cohort data table
            pipelineDestination.ProcessPipelineData(dt, new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());

            //there should be no cohorts yet
            Assert.IsEmpty(DataExportRepository.GetAllObjects <ExtractableCohort>());

            //dispose of the pipeline
            pipelineDestination.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);

            //now there should be one
            ExtractableCohort cohort = DataExportRepository.GetAllObjects <ExtractableCohort>().Single();

            //make sure we are all on the same page about what the DBMS type is (nothing cached etc)
            Assert.AreEqual(dbType, cohort.ExternalCohortTable.DatabaseType);
            Assert.AreEqual(dbType, cohort.GetQuerySyntaxHelper().DatabaseType);

            Assert.AreEqual(500, cohort.ExternalProjectNumber);
            Assert.AreEqual(2, cohort.CountDistinct);

            var tbl = externalCohortTable.DiscoverCohortTable();

            Assert.AreEqual(2, tbl.GetRowCount());
            var dtInDatabase = tbl.GetDataTable();

            //guid will be something like "6fb23de5-e8eb-46eb-84b5-dd368da21073"
            Assert.AreEqual(36, dtInDatabase.Rows[0]["ReleaseId"].ToString().Length);
            Assert.AreEqual("0101010101", dtInDatabase.Rows[0]["chi"]);
        }
        public void TestActuallyCreatingIt(DatabaseType type)
        {
            var db = GetCleanedServer(type);

            //drop it
            db.Drop();

            CreateNewCohortDatabaseWizard 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,
                new ThrowImmediatelyCheckNotifier());

            //database should exist
            DiscoveredServerICanCreateRandomDatabasesAndTablesOn.ExpectDatabase(cohortDatabaseName);
            Assert.IsTrue(db.Exists());

            //did it create the correct type?
            Assert.AreEqual(type, ect.DatabaseType);

            //the ExternalCohortTable should pass tests
            ect.Check(new ThrowImmediatelyCheckNotifier());

            //now try putting someone in it
            //the project it will go under
            var project = new Project(DataExportRepository, "MyProject");

            project.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
            DataTable 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(request, new ThrowImmediatelyDataLoadEventListener());

            //tell it to use the guid allocator
            dest.ReleaseIdentifierAllocator = typeof(GuidReleaseIdentifierAllocator);

            dest.ProcessPipelineData(dt, new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
            dest.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);

            var cohort = request.CohortCreatedIfAny;

            Assert.IsNotNull(cohort);

            var externalData = cohort.GetExternalData();

            Assert.AreEqual(10, externalData.ExternalProjectNumber);
            Assert.IsFalse(string.IsNullOrEmpty(externalData.ExternalDescription));


            Assert.AreEqual(DateTime.Now.Year, externalData.ExternalCohortCreationDate.Value.Year);
            Assert.AreEqual(DateTime.Now.Month, externalData.ExternalCohortCreationDate.Value.Month);
            Assert.AreEqual(DateTime.Now.Day, externalData.ExternalCohortCreationDate.Value.Day);
            Assert.AreEqual(DateTime.Now.Hour, externalData.ExternalCohortCreationDate.Value.Hour);

            cohort.AppendToAuditLog("Test");

            Assert.IsTrue(cohort.AuditLog.Contains("Test"));

            Assert.AreEqual(1, cohort.Count);
            Assert.AreEqual(1, cohort.CountDistinct);

            var cohortTable = cohort.FetchEntireCohort();

            Assert.AreEqual(1, cohortTable.Rows.Count);

            var helper = ect.GetQuerySyntaxHelper();

            Assert.AreEqual(101243, cohortTable.Rows[0][helper.GetRuntimeName(ect.PrivateIdentifierField)]);
            var aguid = cohortTable.Rows[0][helper.GetRuntimeName(ect.ReleaseIdentifierField)].ToString();

            Assert.IsFalse(string.IsNullOrWhiteSpace(aguid)); //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, new ThrowImmediatelyDataLoadEventListener(), true);

            Assert.AreEqual(2, dtAno.Columns.Count);
            Assert.IsTrue(dtAno.Columns.Contains(cohort.GetPrivateIdentifier(true)));

            Assert.AreEqual("101243", dtAno.Rows[0][cohort.GetPrivateIdentifier(true)]);
            Assert.AreEqual("101243", dtAno.Rows[1][cohort.GetPrivateIdentifier(true)]);
        }