Ejemplo n.º 1
0
        public void DataTableChopTest()
        {
            string[]  array_first  = new string[] { "first", "two", "three", "four, five" };
            string[]  array_second = new string[] { "second", "two", "three", "four, five" };
            string[]  array_third  = new string[] { "third", "two", "three", "four, five" };
            DataTable dt           = new DataTable();

            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Columns.Add("col3");
            dt.Columns.Add("col4");
            dt.Columns.Add("col5");
            for (int i = 0; i < 5000; i++)
            {
                dt.Rows.Add(array_first);
            }
            for (int i = 0; i < 5000; i++)
            {
                dt.Rows.Add(array_second);
            }
            for (int i = 0; i < 5000; i++)
            {
                dt.Rows.Add(array_third);
            }

            // Save this string to a test file
            string sourcefile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csv");

            CSVDataTable.WriteToFile(dt, sourcefile);

            // Create an empty test folder
            string dirname = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Directory.CreateDirectory(dirname);

            // Chop this file into one-line chunks
            CSVReader.ChopFile(sourcefile, dirname, 5000);

            // Verify that we got three files
            string[] files = Directory.GetFiles(dirname);
            Assert.AreEqual(3, files.Length);

            // Read in each file and verify that each one has one line
            dt = CSVDataTable.FromFile(files[0]);
            Assert.AreEqual(5000, dt.Rows.Count);
            Assert.AreEqual("first", dt.Rows[0].ItemArray[0]);

            dt = CSVDataTable.FromFile(files[1]);
            Assert.AreEqual(5000, dt.Rows.Count);
            Assert.AreEqual("second", dt.Rows[0].ItemArray[0]);

            dt = CSVDataTable.FromFile(files[2]);
            Assert.AreEqual(5000, dt.Rows.Count);
            Assert.AreEqual("third", dt.Rows[0].ItemArray[0]);

            // Clean up
            Directory.Delete(dirname, true);
            File.Delete(sourcefile);
        }
        public void DataTableChoppingFiles()
        {
            const string sourceText = @"timestamp,TestString,SetComment,PropertyString,IntField,IntProperty
2012-05-01,test1,""Hi there, I said!"",Bob,57,0
2011-04-01,test2,""What's up, buttercup?"",Ralph,1,-999
1975-06-03,test3,""Bye and bye, dragonfly!"",Jimmy's The Bomb,12,13";
            var          dt         = CSVDataTable.FromString(sourceText);
            var          outfile    = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csv");
            var          dirname    = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            try
            {
                // Save this string to a test file
                CSVDataTable.WriteToFile(dt, outfile);

                // Create an empty test folder
                Directory.CreateDirectory(dirname);

                // Chop this file into one-line chunks
                CSVReader.ChopFile(outfile, dirname, 1);

                // Verify that we got three files
                var files = Directory.GetFiles(dirname).ToList();
                files.Sort();
                Assert.IsTrue(files[0].EndsWith("1.csv"));
                Assert.IsTrue(files[1].EndsWith("2.csv"));
                Assert.IsTrue(files[2].EndsWith("3.csv"));
                Assert.AreEqual(3, files.Count);

                // Read in each file and verify that each one has one line
                dt = CSVDataTable.FromFile(files[0]);
                Assert.AreEqual(1, dt.Rows.Count);
                Assert.AreEqual("2012-05-01", dt.Rows[0].ItemArray[0]);
                Assert.AreEqual("test1", dt.Rows[0].ItemArray[1]);

                dt = CSVDataTable.FromFile(files[1]);
                Assert.AreEqual(1, dt.Rows.Count);
                Assert.AreEqual("2011-04-01", dt.Rows[0].ItemArray[0]);
                Assert.AreEqual("test2", dt.Rows[0].ItemArray[1]);

                dt = CSVDataTable.FromFile(files[2]);
                Assert.AreEqual(1, dt.Rows.Count);
                Assert.AreEqual("1975-06-03", dt.Rows[0].ItemArray[0]);
                Assert.AreEqual("test3", dt.Rows[0].ItemArray[1]);
            }
            finally
            {
                if (Directory.Exists(dirname))
                {
                    Directory.Delete(dirname, true);
                }

                if (File.Exists(outfile))
                {
                    File.Delete(outfile);
                }
            }
        }
Ejemplo n.º 3
0
        public void DataTableChoppingFiles()
        {
            string    source = @"timestamp,TestString,SetComment,PropertyString,IntField,IntProperty
2012-05-01,test1,""Hi there, I said!"",Bob,57,0
2011-04-01,test2,""What's up, buttercup?"",Ralph,1,-999
1975-06-03,test3,""Bye and bye, dragonfly!"",Jimmy's The Bomb,12,13";
            DataTable dt     = CSVDataTable.FromString(source);

            // Save this string to a test file
            string outfile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csv");

            CSVDataTable.WriteToFile(dt, outfile);

            // Create an empty test folder
            string dirname = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            Directory.CreateDirectory(dirname);

            // Chop this file into one-line chunks
            CSVReader.ChopFile(outfile, dirname, 1);

            // Verify that we got three files
            string[] files = Directory.GetFiles(dirname);
            Assert.AreEqual(3, files.Length);

            // Read in each file and verify that each one has one line
            dt = CSVDataTable.FromFile(files[0]);
            Assert.AreEqual(1, dt.Rows.Count);
            Assert.AreEqual("2012-05-01", dt.Rows[0].ItemArray[0]);
            Assert.AreEqual("test1", dt.Rows[0].ItemArray[1]);

            dt = CSVDataTable.FromFile(files[1]);
            Assert.AreEqual(1, dt.Rows.Count);
            Assert.AreEqual("2011-04-01", dt.Rows[0].ItemArray[0]);
            Assert.AreEqual("test2", dt.Rows[0].ItemArray[1]);

            dt = CSVDataTable.FromFile(files[2]);
            Assert.AreEqual(1, dt.Rows.Count);
            Assert.AreEqual("1975-06-03", dt.Rows[0].ItemArray[0]);
            Assert.AreEqual("test3", dt.Rows[0].ItemArray[1]);

            // Clean up
            Directory.Delete(dirname, true);
            File.Delete(outfile);
        }
        public void DataTableChopTest()
        {
            object[] array_first  = { "first", "two", "three", "four, five" };
            object[] array_second = { "second", "two", "three", "four, five" };
            object[] array_third  = { "third", "two", "three", "four, five" };
            var      dt           = new DataTable();

            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Columns.Add("col3");
            dt.Columns.Add("col4");
            dt.Columns.Add("col5");
            for (var i = 0; i < 5000; i++)
            {
                dt.Rows.Add(array_first);
            }
            for (var i = 0; i < 5000; i++)
            {
                dt.Rows.Add(array_second);
            }
            for (var i = 0; i < 5000; i++)
            {
                dt.Rows.Add(array_third);
            }
            var sourceFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".csv");
            var dirname    = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());

            try
            {
                // Save this string to a test file
                CSVDataTable.WriteToFile(dt, sourceFile);

                // Chop this file into one-line chunks in a new folder
                Directory.CreateDirectory(dirname);
                CSVReader.ChopFile(sourceFile, dirname, 5000);

                // Verify that we got three files
                var files = Directory.GetFiles(dirname).ToList();
                files.Sort();
                Assert.IsTrue(files[0].EndsWith("1.csv"));
                Assert.IsTrue(files[1].EndsWith("2.csv"));
                Assert.IsTrue(files[2].EndsWith("3.csv"));
                Assert.AreEqual(3, files.Count);

                // Read in each file and verify that each one has one line
                dt = CSVDataTable.FromFile(files[0]);
                Assert.AreEqual(5000, dt.Rows.Count);
                Assert.AreEqual("first", dt.Rows[0].ItemArray[0]);

                dt = CSVDataTable.FromFile(files[1]);
                Assert.AreEqual(5000, dt.Rows.Count);
                Assert.AreEqual("second", dt.Rows[0].ItemArray[0]);

                dt = CSVDataTable.FromFile(files[2]);
                Assert.AreEqual(5000, dt.Rows.Count);
                Assert.AreEqual("third", dt.Rows[0].ItemArray[0]);
            }
            finally
            {
                if (Directory.Exists(dirname))
                {
                    Directory.Delete(dirname, true);
                }

                if (File.Exists(sourceFile))
                {
                    File.Delete(sourceFile);
                }
            }
        }