public void TestRead_GetValuesWithoutReading_Throws() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10).AddColumn(new StringColumn("name"), 25).AddColumn(new DateTimeColumn("created"), 10); FixedLengthParser parser = new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema); parser.GetValues(); }
public void TestGetSchema_SchemaProvided_ParsesValues() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10).AddColumn(new StringColumn("name"), 25).AddColumn(new DateTimeColumn("created"), 10); IParser parser = new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema); Schema actual = parser.GetSchema(); Assert.AreSame(schema.Schema, actual, "The underlying schema was not returned."); }
public void TestGetSchema_SchemaProvided_WrongNumberOfColumns_Throws() { const string text = @" 123 Bob"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10) .AddColumn(new StringColumn("name"), 25) .AddColumn(new DateTimeColumn("created"), 10); FixedLengthParser parser = new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema); parser.Read(); }
public void TestRead_ValuesAfterEndOfFile_Throws() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10).AddColumn(new StringColumn("name"), 25).AddColumn(new DateTimeColumn("created"), 10); FixedLengthParser parser = new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema); Assert.IsTrue(parser.Read(), "Could not read the record."); Assert.IsFalse(parser.Read(), "We should have reached the end of the file."); parser.GetValues(); }
public void TestRead_SingleRecord_ReturnsTrueOnce() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10).AddColumn(new StringColumn("name"), 25).AddColumn(new DateTimeColumn("created"), 10); FixedLengthParser parser = new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema); Assert.IsTrue(parser.Read(), "Could not read the record."); object[] expected = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; object[] actual = parser.GetValues(); CollectionAssert.AreEqual(expected, actual, "The wrong values were parsed."); Assert.IsFalse(parser.Read(), "No more records should have been read."); }
public void TestRead_MultipleCallsToValues_ReturnsSameValues() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10).AddColumn(new StringColumn("name"), 25).AddColumn(new DateTimeColumn("created"), 10); FixedLengthParser parser = new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema); bool canRead = parser.Read(); Assert.IsTrue(canRead, "Could not read the record."); object[] expected = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; 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 TestGetValues_CustomFillCharacter_TrimsFill() { const string text = "@@@@@@@123@@@@@@@@@@@@@@@@@@@@@@Bob@1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10) .AddColumn(new StringColumn("name"), 25) .AddColumn(new DateTimeColumn("created"), 10); FixedLengthParserOptions options = new FixedLengthParserOptions() { FillCharacter = '@' }; FixedLengthParser parser = new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema, options); Assert.IsTrue(parser.Read(), "Could not read the first record."); object[] expected = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; object[] actual = parser.GetValues(); CollectionAssert.AreEqual(expected, actual, "The values for the first record were wrong."); }
public void TestGetValues_CustomRecordSeparator_SplitsFile() { const string text = " 123 Bob 1/19/2013BOOM 234 Sam12/20/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10) .AddColumn(new StringColumn("name"), 25) .AddColumn(new DateTimeColumn("created"), 10); FixedLengthParserOptions options = new FixedLengthParserOptions() { RecordSeparator = "BOOM" }; FixedLengthParser parser = new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema, options); Assert.IsTrue(parser.Read(), "Could not read the first record."); object[] expected = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; object[] actual = parser.GetValues(); CollectionAssert.AreEqual(expected, actual, "The values for the first record were wrong."); Assert.IsTrue(parser.Read(), "Could not read the second record."); expected = new object[] { 234, "Sam", new DateTime(2013, 12, 20) }; actual = parser.GetValues(); CollectionAssert.AreEqual(expected, actual, "The values for the second record were wrong."); }