public void TestCtor_OptionsNull_Throws() { string text = ""; SeparatedValueParserOptions options = null; new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text)), options); }
public void TestReadFlatFile_ClearsSchemaAndData() { // fill the table with some existing columns, constraints and data DataTable table = new DataTable(); DataColumn column = table.Columns.Add("blah", typeof(int)); table.Columns.Add("bleh", typeof(string)); table.Constraints.Add("PK_blah", column, true); DataRow row = table.Rows.Add(new object[] { 123, "dopey" }); row.AcceptChanges(); const string text = @"id,name,created,avg 123,Bob,12/31/2012,3.14159"; SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true }; Stream stream = new MemoryStream(Encoding.Default.GetBytes(text)); IParser parser = new SeparatedValueParser(stream, options); table.ReadFlatFile(parser); Assert.AreEqual(4, table.Columns.Count, "The wrong number of columns were extracted."); Assert.IsTrue(table.Columns.Contains("id"), "The ID column was not extracted."); Assert.IsTrue(table.Columns.Contains("name"), "The name column was not extracted."); Assert.IsTrue(table.Columns.Contains("created"), "The created column was not extracted."); Assert.IsTrue(table.Columns.Contains("avg"), "The AVG column was not extracted."); Assert.AreEqual(1, table.Rows.Count, "Not all of the records were extracted."); row = table.Rows[0]; object[] expected = new object[] { "123", "Bob", "12/31/2012", "3.14159" }; object[] values = row.ItemArray; CollectionAssert.AreEqual(expected, values, "The wrong values were extracted"); }
public void TestCtor_Options_SchemaNull_Throws() { string text = String.Empty; Schema schema = null; SeparatedValueParserOptions options = new SeparatedValueParserOptions(); new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema, options); }
public void TestCtor_Options_Schema_TextNull_Throws() { string text = null; Schema schema = new Schema(); SeparatedValueParserOptions options = new SeparatedValueParserOptions(); new SeparatedValueParser(text, schema, options); }
public void TestReadFlatFile_ExtractsSchema_PopulatesTable() { const string text = @"id,name,created,avg 123,Bob,12/31/2012,3.14159"; SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true }; Stream stream = new MemoryStream(Encoding.Default.GetBytes(text)); DataTable table = new DataTable(); IParser parser = new SeparatedValueParser(stream, options); table.ReadFlatFile(parser); Assert.AreEqual(4, table.Columns.Count, "The wrong number of columns were extracted."); Assert.IsTrue(table.Columns.Contains("id"), "The ID column was not extracted."); Assert.IsTrue(table.Columns.Contains("name"), "The name column was not extracted."); Assert.IsTrue(table.Columns.Contains("created"), "The created column was not extracted."); Assert.IsTrue(table.Columns.Contains("avg"), "The AVG column was not extracted."); Assert.AreEqual(1, table.Rows.Count, "Not all of the records were extracted."); DataRow row = table.Rows[0]; object[] expected = new object[] { "123", "Bob", "12/31/2012", "3.14159" }; object[] values = row.ItemArray; CollectionAssert.AreEqual(expected, values, "The wrong values were extracted"); }
public void TestCtor_Options_TextNull_Throws() { string text = null; SeparatedValueParserOptions options = new SeparatedValueParserOptions(); new SeparatedValueParser(text, options); }
public void TestReadFlatFile_DataTableNull_Throws() { const string text = @"id,name,created,avg 123,Bob,12/31/2012,3.14159"; SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true }; Stream stream = new MemoryStream(Encoding.Default.GetBytes(text)); DataTable table = null; IParser parser = new SeparatedValueParser(stream, options); DataTableExtensions.ReadFlatFile(table, parser); }
public void TestGetSchema_NotExtracted_Throws() { string text = "a,b,c"; SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = false }; IParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text)), options); parser.GetSchema(); }
public void TestGetSchema_FirstRecordSchema_WrongNumberOfColumns_Throws() { const string text = @"id,name,created 123,Bob,1/19/2013,Hello"; SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true }; SeparatedValueParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text)), options); parser.Read(); }
public void TestGetSchema_Extracted_ReturnsColumnNames() { string text = "a,b,c"; SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true }; IParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text)), options); Schema schema = parser.GetSchema(); Assert.IsTrue(schema.ColumnDefinitions.All(d => d is StringColumn), "Not all of the columns were treated as strings."); string[] actual = schema.ColumnDefinitions.Select(d => d.ColumnName).ToArray(); string[] expected = new string[] { "a", "b", "c" }; CollectionAssert.AreEqual(expected, actual, "The schema was not extracted as expected."); }
public void TestGetSchema_SchemaProvided_FirstRecordSchema_SkipsFirstRecord() { const string text = @"id,name,created"; Schema schema = new Schema(); schema.AddColumn(new Int32Column("id")) .AddColumn(new StringColumn("name")) .AddColumn(new DateTimeColumn("created")); SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true }; IParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema, options); Schema actual = parser.GetSchema(); Assert.AreSame(schema, actual, "The schema was passed did not take priority."); Assert.IsFalse(parser.Read(), "The schema record was not skipped."); }
public void ShouldCreateCustomerFromCsv() { Schema schema = new Schema().AddColumn<string>("name").AddColumn<DateTime>("modified").AddColumn<int>("visits"); Mapper<Customer> mapper = new Mapper<Customer>().Map(c => c.Name).To("name").Map(c => c.LastModified).To("modified").Map(c => c.Visits).To("visits"); const string data = @"name,modified,visits bob,12/31/2012,108"; Stream stream = new MemoryStream(Encoding.Default.GetBytes(data)); SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true }; SeparatedValueParser parser = new SeparatedValueParser(stream, schema, options); FlatFileReader reader = new FlatFileReader(parser); IEnumerable<Customer> customers = mapper.Extract(reader); Assert.AreEqual(1, customers.Count(), "The wrong number of records were mapped."); Assert.AreEqual("bob", customers.First().Name, "The customer name was not parsed correctly."); Assert.AreEqual(new DateTime(2012, 12, 31), customers.First().LastModified, "The customer modified date was not parsed correctly."); Assert.AreEqual(108, customers.First().Visits, "The customer visits was not parsed correctly."); }
public void ShouldCreateCustomerFromCsv() { Schema schema = new Schema().AddColumn <string>("name").AddColumn <DateTime>("modified").AddColumn <int>("visits"); Mapper <Customer> mapper = new Mapper <Customer>().Map(c => c.Name).To("name").Map(c => c.LastModified).To("modified").Map(c => c.Visits).To("visits"); const string data = @"name,modified,visits bob,12/31/2012,108"; Stream stream = new MemoryStream(Encoding.Default.GetBytes(data)); SeparatedValueParserOptions options = new SeparatedValueParserOptions() { IsFirstRecordSchema = true }; SeparatedValueParser parser = new SeparatedValueParser(stream, schema, options); FlatFileReader reader = new FlatFileReader(parser); IEnumerable <Customer> customers = mapper.Extract(reader); Assert.AreEqual(1, customers.Count(), "The wrong number of records were mapped."); Assert.AreEqual("bob", customers.First().Name, "The customer name was not parsed correctly."); Assert.AreEqual(new DateTime(2012, 12, 31), customers.First().LastModified, "The customer modified date was not parsed correctly."); Assert.AreEqual(108, customers.First().Visits, "The customer visits was not parsed correctly."); }