コード例 #1
0
        public void FileToLoadNotSet_Throws()
        {
            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();
            var ex = Assert.Throws <Exception>(() => source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken()));

            StringAssert.Contains("_fileToLoad was not set", ex.Message);
        }
コード例 #2
0
        public void Test_IgnoreQuotes(bool ignoreQuotes)
        {
            var f = Path.Combine(TestContext.CurrentContext.WorkDirectory, "talk.csv");

            File.WriteAllText(f, @"Field1,Field2
1,Watch out guys its Billie ""The Killer"" Cole
2,""The Killer""? I've heard of him hes a bad un");

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(new FileInfo(f)), new ThrowImmediatelyDataLoadEventListener());
            source.Separator    = ",";
            source.MaxBatchSize = DelimitedFlatFileDataFlowSource.MinimumStronglyTypeInputBatchSize;
            source.StronglyTypeInputBatchSize = DelimitedFlatFileDataFlowSource.MinimumStronglyTypeInputBatchSize;
            source.StronglyTypeInput          = true;
            source.IgnoreQuotes = ignoreQuotes;

            if (!ignoreQuotes)
            {
                var toMem = new ToMemoryDataLoadEventListener(true);
                var ex    = Assert.Throws <ParserException>(() => source.GetChunk(toMem, new GracefulCancellationToken()));
                Assert.AreEqual(2, ex.ReadingContext.RawRow);
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
            else
            {
                var dt = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
                Assert.AreEqual(2, dt.Rows.Count);
                Assert.AreEqual(@"Watch out guys its Billie ""The Killer"" Cole", dt.Rows[0]["Field2"]);
                Assert.AreEqual(@"""The Killer""? I've heard of him hes a bad un", dt.Rows[1]["Field2"]);
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
        }
コード例 #3
0
        public void OverrideDatatypes_ForcedFreakyTypesCorrect()
        {
            var testFile = new FileInfo(filename);
            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator         = ",";
            source.StronglyTypeInput = true;//makes the source interpret the file types properly

            source.ExplicitlyTypedColumns = new ExplicitTypingCollection();
            source.ExplicitlyTypedColumns.ExplicitTypesCSharp.Add("StudyID", typeof(string));

            //preview should be correct
            DataTable preview = source.TryGetPreview();

            Assert.AreEqual(typeof(string), preview.Columns["StudyID"].DataType);
            Assert.AreEqual("5", preview.Rows[0]["StudyID"]);

            //as should live run
            var chunk = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());

            Assert.AreEqual(typeof(string), chunk.Columns["StudyID"].DataType);
            Assert.AreEqual("5", chunk.Rows[0]["StudyID"]);

            source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
        }
コード例 #4
0
        public void LoadCSVWithCorrectDatatypes_ForceHeadersWhitespace()
        {
            var testFile = new FileInfo(filename);

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator    = ",";
            source.ForceHeaders = "chi  ,Study ID\t ,Date";
            source.ForceHeadersReplacesFirstLineInFile = true;
            source.StronglyTypeInput = true;//makes the source interpret the file types properly

            var chunk = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());

            Console.WriteLine("Resulting columns were:" + string.Join(",", chunk.Columns.Cast <DataColumn>().Select(c => c.ColumnName)));

            Assert.IsTrue(chunk.Columns.Contains("chi"));      //notice the lack of whitespace!
            Assert.IsTrue(chunk.Columns.Contains("study ID")); //whitespace is allowed in the middle though... because we like a challenge!

            Assert.AreEqual(3, chunk.Columns.Count);
            Assert.AreEqual(1, chunk.Rows.Count);
            Assert.AreEqual("0101010101", chunk.Rows[0][0]);
            Assert.AreEqual(5, chunk.Rows[0][1]);
            Assert.AreEqual(new DateTime(2001, 1, 5), chunk.Rows[0][2]);  //notice the strong typing (we are not looking for strings here)

            source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
        }
コード例 #5
0
        public void CommittingNewCohortFile_CallPipeline()
        {
            var listener = new ThrowImmediatelyDataLoadEventListener();

            var proj = new Project(DataExportRepository, projName);

            proj.ProjectNumber = 999;
            proj.SaveToDatabase();

            CohortCreationRequest request = new CohortCreationRequest(proj, new CohortDefinition(null, "CommittingNewCohorts", 1, 999, _externalCohortTable), (DataExportRepository)DataExportRepository, "fish");

            request.Check(new ThrowImmediatelyCheckNotifier());


            DelimitedFlatFileDataFlowSource source      = new DelimitedFlatFileDataFlowSource();
            BasicCohortDestination          destination = new BasicCohortDestination();

            source.Separator         = ",";
            source.StronglyTypeInput = true;

            DataFlowPipelineEngine <DataTable> pipeline = new DataFlowPipelineEngine <DataTable>((DataFlowPipelineContext <DataTable>)request.GetContext(), source, destination, listener);

            pipeline.Initialize(new FlatFileToLoad(new FileInfo(filename)), request);
            pipeline.ExecutePipeline(new GracefulCancellationToken());

            //there should be a new ExtractableCohort now
            Assert.NotNull(request.NewCohortDefinition.ID);

            var ec = DataExportRepository.GetAllObjects <ExtractableCohort>().Single(c => c.OriginID == request.NewCohortDefinition.ID);

            //with the data in it from the test file
            Assert.AreEqual(ec.Count, 3);
        }
コード例 #6
0
ファイル: ExcelTests.cs プロジェクト: HicServices/RDMP
        public void DontTryToOpenWithDelimited_ThrowsInvalidFileExtension()
        {
            DelimitedFlatFileDataFlowSource invalid = new DelimitedFlatFileDataFlowSource();

            invalid.Separator = ",";
            invalid.PreInitialize(new FlatFileToLoad(new FileInfo(TestFile)), new ThrowImmediatelyDataLoadEventListener());
            var ex = Assert.Throws <Exception>(() => invalid.Check(new ThrowImmediatelyCheckNotifier()));

            StringAssert.Contains("File Book1.xlsx has a prohibitted file extension .xlsx", ex.Message);
        }
コード例 #7
0
        public void SeparatorNotSet_Throws()
        {
            var testFile = new FileInfo(filename);
            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            var ex = Assert.Throws <Exception>(() => source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken()));

            StringAssert.Contains("Separator has not been set", ex.Message);
        }
コード例 #8
0
        public void DelimitedFlatFileDataFlowSource_LoadDataWithQuotesInMiddle_WithMultiLineRecords(bool ignoreQuotes)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CHI,Name,SomeInterestingFacts,Date");
            sb.AppendLine("0101010101,Dave,Dave is over 1000 years old,2001-01-05");
            sb.AppendLine("0101010101,Dave,Dave is \"over\" 1000 years old,2001-01-05");
            sb.AppendLine(@"0101010101,Dave,""Dave is
""over"" 1000 years 

old"",2001-01-05");

            File.WriteAllText(filename, sb.ToString());

            var testFile = new FileInfo(filename);

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator    = ",";
            source.IgnoreQuotes = ignoreQuotes;
            source.MaxBatchSize = 10000;
            source.AttemptToResolveNewLinesInRecords = true;

            source.StronglyTypeInput       = true; //makes the source interpret the file types properly
            source.BadDataHandlingStrategy = BadDataHandlingStrategy.ThrowException;
            try
            {
                if (!ignoreQuotes)
                {
                    Assert.Throws <ParserException>(() => source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken()));
                }
                else
                {
                    var chunk = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
                    Assert.AreEqual(3, chunk.Rows.Count);
                    Assert.AreEqual("Dave is \"over\" 1000 years old", chunk.Rows[1][2]);
                    Assert.AreEqual(@"""Dave is
""over"" 1000 years 

old""", chunk.Rows[2][2]);
                }
            }
            finally
            {
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
        }
コード例 #9
0
        public void DelimitedFlatFileDataFlowSource_LoadDataWithQuotesInMiddle_IgnoreBadReads()
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CHI,Name,SomeInterestingFacts,Date");
            sb.AppendLine("0101010101,Dave,Dave is over 1000 years old,2001-01-05");
            sb.AppendLine("0101010101,Dave,Dave is \"over\" 1000 years old,2001-01-05");
            sb.AppendLine($"0101010101,Dave,\"Dave is {Environment.NewLine}over 1000 years old\",2001-01-05");
            sb.AppendLine("0101010101,Dave,\"Dave is \"over\" 1000 years old\",2001-01-05");
            sb.AppendLine("0101010101,Dave,Dave is over 1000 years old,2001-01-05");

            File.WriteAllText(filename, sb.ToString());

            var testFile = new FileInfo(filename);

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator    = ",";
            source.IgnoreQuotes = false;
            source.MaxBatchSize = 10000;

            source.StronglyTypeInput       = true; //makes the source interpret the file types properly
            source.BadDataHandlingStrategy = BadDataHandlingStrategy.ThrowException;
            source.IgnoreBadReads          = true;

            try
            {
                var chunk = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
                Assert.AreEqual(5, chunk.Rows.Count);
                Assert.AreEqual("Dave is \"over\" 1000 years old", chunk.Rows[1][2]);
                Assert.AreEqual($"Dave is {Environment.NewLine}over 1000 years old", chunk.Rows[2][2]);
                Assert.AreEqual("Dave is over\" 1000 years old\"", chunk.Rows[3][2]); //notice this line drops some of the quotes, we just have to live with that
            }
            finally
            {
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
        }
コード例 #10
0
        public void LoadCSVWithCorrectDatatypes_DatatypesAreCorrect()
        {
            var testFile = new FileInfo(filename);
            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator         = ",";
            source.StronglyTypeInput = true;//makes the source interpret the file types properly

            var chunk = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());

            Assert.AreEqual(3, chunk.Columns.Count);
            Assert.AreEqual(1, chunk.Rows.Count);
            Assert.AreEqual("0101010101", chunk.Rows[0][0]);
            Assert.AreEqual(5, chunk.Rows[0][1]);
            Assert.AreEqual(new DateTime(2001, 1, 5), chunk.Rows[0][2]);//notice the strong typing (we are not looking for strings here)

            source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
        }
コード例 #11
0
        public void Test_ScientificNotation_StronglyTyped()
        {
            var f = Path.Combine(TestContext.CurrentContext.WorkDirectory, "meee.csv");

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("test");

            //1 scientific notation on first row (test is the header)
            sb.AppendLine("-4.10235746055587E-05");

            //500 lines of random stuff to force 2 batches
            for (int i = 0; i < DelimitedFlatFileDataFlowSource.MinimumStronglyTypeInputBatchSize; i++)
            {
                sb.AppendLine("5");
            }

            //a scientific notation in batch 2
            sb.AppendLine("-4.10235746055587E-05");

            File.WriteAllText(f, sb.ToString());

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(new FileInfo(f)), new ThrowImmediatelyDataLoadEventListener());
            source.Separator    = ",";
            source.MaxBatchSize = DelimitedFlatFileDataFlowSource.MinimumStronglyTypeInputBatchSize;
            source.StronglyTypeInputBatchSize = DelimitedFlatFileDataFlowSource.MinimumStronglyTypeInputBatchSize;
            source.StronglyTypeInput          = true;

            var dt = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());

            Assert.AreEqual(typeof(Decimal), dt.Columns.Cast <DataColumn>().Single().DataType);
            Assert.AreEqual(DelimitedFlatFileDataFlowSource.MinimumStronglyTypeInputBatchSize, dt.Rows.Count);

            dt = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
            Assert.AreEqual(typeof(Decimal), dt.Columns.Cast <DataColumn>().Single().DataType);
            Assert.AreEqual(2, dt.Rows.Count);


            dt = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
            Assert.IsNull(dt);
        }
コード例 #12
0
        public void NewLinesInConstantString_EscapedCorrectly()
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CHI,StudyID,Date");
            sb.AppendLine(@"0101010101,""5
    The first"",2001-01-05");
            sb.AppendLine("0101010101,5,2001-01-05");
            sb.AppendLine("0101010101,5,2001-01-05");
            sb.AppendLine("0101010101,5,2001-01-05");
            sb.AppendLine("0101010101,5,2001-01-05");

            File.WriteAllText(filename, sb.ToString());

            var testFile = new FileInfo(filename);

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator = ",";

            source.MaxBatchSize      = 10000;
            source.StronglyTypeInput = true;//makes the source interpret the file types properly

            try
            {
                var dt = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
                Assert.IsNotNull(dt);
                Assert.AreEqual(5, dt.Rows.Count);
                Assert.AreEqual(@"5
    The first", dt.Rows[0][1]);
            }
            finally
            {
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
        }
コード例 #13
0
        public void DelimitedFlatFileDataFlowSource_TrashFile_IgnoreBadReads()
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CHI,Name,SomeInterestingFacts,Date");
            sb.AppendLine("0101010101,Dave,Dave is over 1000 years old,2001-01-05");
            sb.AppendLine("0101010101,Dave,Da,,ve is \"over\" 1000 years old,2001-01-05");
            sb.AppendLine("0101010101\"Dave is \"over\" 1000 years old\",2001-01-05");
            sb.AppendLine("0101010101,Dave,Dave is over 1000 years old,2001-01-05");

            File.WriteAllText(filename, sb.ToString());

            var testFile = new FileInfo(filename);

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator    = ",";
            source.IgnoreQuotes = false;
            source.MaxBatchSize = 10000;

            source.StronglyTypeInput       = true; //makes the source interpret the file types properly
            source.BadDataHandlingStrategy = BadDataHandlingStrategy.ThrowException;
            source.IgnoreBadReads          = true;

            try
            {
                var ex = Assert.Throws <FlatFileLoadException>(() => source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken()));
                Assert.AreEqual("Bad data found on line 3", ex.Message);
            }
            finally
            {
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
        }
コード例 #14
0
        public void OverrideHeadersAndTab()
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("0101010101\t5\t2001-01-05");
            sb.AppendLine("0101010101\t5\t2001-01-05");
            File.WriteAllText(filename, sb.ToString());


            var testFile = new FileInfo(filename);

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator    = "\\t"; //<-- Important this is the string value SLASH T not an actual escaped tab as C# understands it.  This reflects the user pressing slash and t on his keyboard for the Separator argument in the UI
            source.ForceHeaders = "CHI\tStudyID\tDate";
            source.MaxBatchSize = 10000;

            var dt = source.GetChunk(new ThrowImmediatelyDataLoadJob(), new GracefulCancellationToken());

            Assert.NotNull(dt);

            Assert.AreEqual(3, dt.Columns.Count);

            Assert.AreEqual("CHI", dt.Columns[0].ColumnName);
            Assert.AreEqual("StudyID", dt.Columns[1].ColumnName);
            Assert.AreEqual("Date", dt.Columns[2].ColumnName);

            Assert.AreEqual(2, dt.Rows.Count);

            source.Dispose(new ThrowImmediatelyDataLoadJob(), null);

            File.Delete(filename);
        }
コード例 #15
0
        public void DelimitedFlatFileDataFlowSource_ProperQuoteEscaping()
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CHI,Name,SomeInterestingFacts,Date");
            sb.AppendLine("0101010101,Dave,Dave is over 1000 years old,2001-01-05");
            sb.AppendLine("0101010101,Dave,\"Dave is \"\"over\"\" 1000 years old\",2001-01-05"); //https://tools.ietf.org/html/rfc4180 (to properly include quotes in escaped text you need to use "")

            File.WriteAllText(filename, sb.ToString());

            var testFile = new FileInfo(filename);

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator    = ",";
            source.MaxBatchSize = 10000;

            source.StronglyTypeInput       = true; //makes the source interpret the file types properly
            source.BadDataHandlingStrategy = BadDataHandlingStrategy.ThrowException;
            source.IgnoreQuotes            = false;
            source.IgnoreBadReads          = false;

            try
            {
                var chunk = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
                Assert.AreEqual(2, chunk.Rows.Count);
                Assert.AreEqual("Dave is \"over\" 1000 years old", chunk.Rows[1][2]);
            }
            finally
            {
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
        }
コード例 #16
0
        protected DataTable RunGetChunk(FlatFileToLoad file, Action <DelimitedFlatFileDataFlowSource> adjust = null)
        {
            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(file, new ThrowImmediatelyDataLoadEventListener());
            source.Separator                         = ",";
            source.StronglyTypeInput                 = true; //makes the source interpret the file types properly
            source.StronglyTypeInputBatchSize        = 100;
            source.AttemptToResolveNewLinesInRecords = true; //maximise potential for conflicts
            if (adjust != null)
            {
                adjust(source);
            }

            try
            {
                return(source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken()));
            }
            finally
            {
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
        }
コード例 #17
0
        public void NewLinesInConstantString_NotEscaped(BadDataHandlingStrategy strategy)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("CHI,StudyID,Date");
            sb.AppendLine(@"0101010101,5
    The first,2001-01-05");
            sb.AppendLine("0101010101,5,2001-01-05");
            sb.AppendLine("0101010101,5,2001-01-05");
            sb.AppendLine("0101010101,5,2001-01-05");
            sb.AppendLine("0101010101,5,2001-01-05");

            File.WriteAllText(filename, sb.ToString());

            var testFile = new FileInfo(filename);

            DelimitedFlatFileDataFlowSource source = new DelimitedFlatFileDataFlowSource();

            source.PreInitialize(new FlatFileToLoad(testFile), new ThrowImmediatelyDataLoadEventListener());
            source.Separator = ",";

            source.MaxBatchSize            = 10000;
            source.StronglyTypeInput       = true;//makes the source interpret the file types properly
            source.BadDataHandlingStrategy = strategy;
            try
            {
                switch (strategy)
                {
                case BadDataHandlingStrategy.ThrowException:
                    var ex = Assert.Throws <FlatFileLoadException>(() => source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken()));
                    StringAssert.Contains("line 2", ex.Message);
                    break;

                case BadDataHandlingStrategy.IgnoreRows:
                    var dt = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
                    Assert.IsNotNull(dt);

                    Assert.AreEqual(4, dt.Rows.Count);
                    break;

                case BadDataHandlingStrategy.DivertRows:
                    var dt2 = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());
                    Assert.AreEqual(4, dt2.Rows.Count);

                    Assert.IsNotNull(source.EventHandlers.DivertErrorsFile);

                    Assert.AreEqual(@"0101010101,5
    The first,2001-01-05
", File.ReadAllText(source.EventHandlers.DivertErrorsFile.FullName));

                    break;

                default:
                    throw new ArgumentOutOfRangeException("strategy");
                }
            }
            finally
            {
                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);
            }
        }
コード例 #18
0
ファイル: ImportFileTests.cs プロジェクト: HicServices/RDMP
        public void ImportFile()
        {
            string file         = Path.GetTempFileName();
            string databaseName = TestDatabaseNames.GetConsistentName(GetType().Name);

            try
            {
                using (var sw = new StreamWriter(file))
                {
                    sw.WriteLine("Name,Surname,Age,Healthiness,DateOfImagining");
                    sw.WriteLine("Frank,\"Mortus,M\",41,0.00,2005-12-01");
                    sw.WriteLine("Bob,Balie,12,1,2013-06-11");
                    sw.WriteLine("Munchen,'Smith',43,0.3,2002-01-01");
                    sw.WriteLine("Carnage,Here there is,29,0.91,2005-01-01");
                    sw.WriteLine("Nathan,Crumble,51,0.78,2005-01-01");
                    sw.Close();
                }

                var source = new DelimitedFlatFileDataFlowSource
                {
                    Separator                  = ",",
                    IgnoreBlankLines           = true,
                    MakeHeaderNamesSane        = true,
                    StronglyTypeInputBatchSize = -1,
                    StronglyTypeInput          = true
                };

                source.PreInitialize(new FlatFileToLoad(new FileInfo(file)), new ThrowImmediatelyDataLoadEventListener());//this is the file we want to load
                source.Check(new ThrowImmediatelyCheckNotifier());

                var server   = DiscoveredServerICanCreateRandomDatabasesAndTablesOn;
                var database = server.ExpectDatabase(databaseName);

                //recreate it
                database.Create(true);

                server.ChangeDatabase(databaseName);

                var dt = source.GetChunk(new ThrowImmediatelyDataLoadEventListener(), new GracefulCancellationToken());

                var    tbl       = database.CreateTable(dt.TableName, dt);
                string tableName = tbl.GetRuntimeName();

                source.Dispose(new ThrowImmediatelyDataLoadEventListener(), null);

                var tablesInDatabase = server.ExpectDatabase(databaseName).DiscoverTables(false);

                //there should be 1 table in this database
                Assert.AreEqual(1, tablesInDatabase.Length);

                //it should be called the same as the file loaded
                Assert.AreEqual(Path.GetFileNameWithoutExtension(file), tablesInDatabase[0].GetRuntimeName());

                Assert.AreEqual("varchar(7)", GetColumnType(database, tableName, "Name"));
                Assert.AreEqual("varchar(13)", GetColumnType(database, tableName, "Surname"));
                Assert.AreEqual("int", GetColumnType(database, tableName, "Age"));
                Assert.AreEqual("decimal(3,2)", GetColumnType(database, tableName, "Healthiness"));
                Assert.AreEqual("datetime2", GetColumnType(database, tableName, "DateOfImagining"));

                using (var con = (SqlConnection)server.GetConnection())
                {
                    con.Open();

                    SqlCommand cmdReadData =
                        new SqlCommand(
                            "Select * from " + tablesInDatabase[0].GetRuntimeName() + " WHERE Name='Frank'", con);
                    SqlDataReader r = cmdReadData.ExecuteReader();

                    //expected 1 record only
                    Assert.IsTrue(r.Read());

                    Assert.AreEqual("Frank", r["Name"]);
                    Assert.AreEqual("Mortus,M", r["Surname"]);
                    Assert.AreEqual(41, r["Age"]);
                    Assert.AreEqual(0.0f, r["Healthiness"]);
                    Assert.AreEqual(new DateTime(2005, 12, 1), r["DateOfImagining"]);

                    //and no more records
                    Assert.IsFalse(r.Read());

                    con.Close();
                }

                server.ExpectDatabase(databaseName).Drop();
                Assert.IsFalse(server.ExpectDatabase(databaseName).Exists());
            }
            finally
            {
                try
                {
                    File.Delete(file);
                }
                catch (IOException)
                {
                    //Couldn't delete temporary file... oh well
                }
            }
        }