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); } } }
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 TestDataTableWithEmbeddedNewlines() { DataTable dt = CSVDataTable.FromString(source_embedded_newlines); Assert.AreEqual(dt.Columns.Count, 3); Assert.AreEqual(dt.Rows.Count, 4); Assert.AreEqual(dt.Rows[0].ItemArray[0], "JD"); Assert.AreEqual(dt.Rows[1].ItemArray[0], "Janitor"); Assert.AreEqual(dt.Rows[2].ItemArray[0], "Dr. Reed, " + Environment.NewLine + "Eliot"); Assert.AreEqual(dt.Rows[3].ItemArray[0], "Dr. Kelso"); Assert.AreEqual(dt.Rows[0].ItemArray[1], "Doctor"); Assert.AreEqual(dt.Rows[1].ItemArray[1], "Janitor"); Assert.AreEqual(dt.Rows[2].ItemArray[1], "Private \"Practice\""); Assert.AreEqual(dt.Rows[3].ItemArray[1], "Chief of Medicine"); Assert.AreEqual(dt.Rows[0].ItemArray[2], "x234"); Assert.AreEqual(dt.Rows[1].ItemArray[2], "x235"); Assert.AreEqual(dt.Rows[2].ItemArray[2], "x236"); Assert.AreEqual(dt.Rows[3].ItemArray[2], "x100"); }
public void TestBasicDataTable() { DataTable dt = CSVDataTable.FromString(source); Assert.AreEqual(dt.Columns.Count, 3); Assert.AreEqual(dt.Rows.Count, 4); Assert.AreEqual(dt.Rows[0].ItemArray[0], "JD"); Assert.AreEqual(dt.Rows[1].ItemArray[0], "Janitor"); Assert.AreEqual(dt.Rows[2].ItemArray[0], "Dr. Reed, Eliot"); Assert.AreEqual(dt.Rows[3].ItemArray[0], "Dr. Kelso"); Assert.AreEqual(dt.Rows[0].ItemArray[1], "Doctor"); Assert.AreEqual(dt.Rows[1].ItemArray[1], "Janitor"); Assert.AreEqual(dt.Rows[2].ItemArray[1], "Private Practice"); Assert.AreEqual(dt.Rows[3].ItemArray[1], "Chief of Medicine"); Assert.AreEqual(dt.Rows[0].ItemArray[2], "x234"); Assert.AreEqual(dt.Rows[1].ItemArray[2], "x235"); Assert.AreEqual(dt.Rows[2].ItemArray[2], "x236"); Assert.AreEqual(dt.Rows[3].ItemArray[2], "x100"); }
public void TestDataTableWithEmbeddedNewlines() { var dt = CSVDataTable.FromString(source_embedded_newlines); Assert.AreEqual(3, dt.Columns.Count); Assert.AreEqual(4, dt.Rows.Count); Assert.AreEqual("JD", dt.Rows[0].ItemArray[0]); Assert.AreEqual("Janitor", dt.Rows[1].ItemArray[0]); Assert.AreEqual("Dr. Reed, " + Environment.NewLine + "Eliot", dt.Rows[2].ItemArray[0]); Assert.AreEqual("Dr. Kelso", dt.Rows[3].ItemArray[0]); Assert.AreEqual("Doctor", dt.Rows[0].ItemArray[1]); Assert.AreEqual("Janitor", dt.Rows[1].ItemArray[1]); Assert.AreEqual("Private \"Practice\"", dt.Rows[2].ItemArray[1]); Assert.AreEqual("Chief of Medicine", dt.Rows[3].ItemArray[1]); Assert.AreEqual("x234", dt.Rows[0].ItemArray[2]); Assert.AreEqual("x235", dt.Rows[1].ItemArray[2]); Assert.AreEqual("x236", dt.Rows[2].ItemArray[2]); Assert.AreEqual("x100", dt.Rows[3].ItemArray[2]); }
public void TestBasicDataTable() { var dt = CSVDataTable.FromString(source); Assert.AreEqual(3, dt.Columns.Count); Assert.AreEqual(4, dt.Rows.Count); Assert.AreEqual("JD", dt.Rows[0].ItemArray[0]); Assert.AreEqual("Janitor", dt.Rows[1].ItemArray[0]); Assert.AreEqual("Dr. Reed, Eliot", dt.Rows[2].ItemArray[0]); Assert.AreEqual("Dr. Kelso", dt.Rows[3].ItemArray[0]); Assert.AreEqual("Doctor", dt.Rows[0].ItemArray[1]); Assert.AreEqual("Janitor", dt.Rows[1].ItemArray[1]); Assert.AreEqual("Private Practice", dt.Rows[2].ItemArray[1]); Assert.AreEqual("Chief of Medicine", dt.Rows[3].ItemArray[1]); Assert.AreEqual("x234", dt.Rows[0].ItemArray[2]); Assert.AreEqual("x235", dt.Rows[1].ItemArray[2]); Assert.AreEqual("x236", dt.Rows[2].ItemArray[2]); Assert.AreEqual("x100", dt.Rows[3].ItemArray[2]); }