public void TestReadFlatFile_SchemaProvided_TypesUsed() { const string text = @"123,Bob,12/31/2012,3.14159"; Stream stream = new MemoryStream(Encoding.Default.GetBytes(text)); DataTable table = new DataTable(); Schema schema = new Schema(); schema.AddColumn(new Int32Column("id")) .AddColumn(new StringColumn("name")) .AddColumn(new DateTimeColumn("created")) .AddColumn(new DoubleColumn("avg")); IParser parser = new SeparatedValueParser(stream, schema); 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", new DateTime(2012, 12, 31), 3.14159 }; object[] values = row.ItemArray; CollectionAssert.AreEqual(expected, values, "The wrong values were extracted"); }
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 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 TestRead_GetValuesWithoutReading_Throws() { string text = "a,b,c"; SeparatedValueParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text))); parser.GetValues(); }
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 TestRead_ValuesAfterEndOfFile_Throws() { string text = "a,b,c"; SeparatedValueParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text))); bool canRead = parser.Read(); Assert.IsTrue(canRead, "Could not read the record."); canRead = parser.Read(); Assert.IsFalse(canRead, "We should have reached the end of the file."); parser.GetValues(); }
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_SchemaProvided_WrongNumberOfColumns_Throws() { const string text = @"123,Bob"; Schema schema = new Schema(); schema.AddColumn(new Int32Column("id")) .AddColumn(new StringColumn("name")) .AddColumn(new DateTimeColumn("created")); SeparatedValueParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema); parser.Read(); }
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 TestRead_MultipleCallsToValues_ReturnsSameValues() { string text = "a,b,c"; SeparatedValueParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text))); bool canRead = parser.Read(); Assert.IsTrue(canRead, "Could not read the record."); string[] expected = new string[] { "a", "b", "c" }; object[] actual = parser.GetValues(); CollectionAssert.AreEqual(expected, actual, "The wrong values were parsed."); actual = parser.GetValues(); CollectionAssert.AreEqual(expected, actual, "The same values were not returned multiple times."); }
public void TestRead_SingleRecord_ReturnsTrueOnce() { const string text = "a,b,c"; SeparatedValueParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text))); bool canRead = parser.Read(); Assert.IsTrue(canRead, "Could not read the record."); string[] expected = new string[] { "a", "b", "c" }; object[] actual = parser.GetValues(); CollectionAssert.AreEqual(expected, actual, "The wrong values were parsed."); canRead = parser.Read(); Assert.IsFalse(canRead, "No more records should have been 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_ParsesValues() { const string text = @"123,Bob,1/19/2013"; Schema schema = new Schema(); schema.AddColumn(new Int32Column("id")) .AddColumn(new StringColumn("name")) .AddColumn(new DateTimeColumn("created")); SeparatedValueParser parser = new SeparatedValueParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema); Assert.IsTrue(parser.Read(), "The first record was skipped."); object[] actual = parser.GetValues(); object[] expected = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; CollectionAssert.AreEqual(expected, actual, "The values were not parsed 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."); }