public void ShouldTruncateOverflow() { MemoryStream stream = new MemoryStream(); FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("Default"), new Window(5)); schema.AddColumn(new StringColumn("Leading"), new Window(5) { TruncationPolicy = OverflowTruncationPolicy.TruncateLeading }); schema.AddColumn(new StringColumn("Trailing"), new Window(5) { TruncationPolicy = OverflowTruncationPolicy.TruncateTrailing }); FixedLengthOptions options = new FixedLengthOptions(); options.TruncationPolicy = OverflowTruncationPolicy.TruncateLeading; // this is the default anyway using (FixedLengthWriter writer = new FixedLengthWriter(stream, schema, options)) { writer.Write(new object[] { "Pineapple", "Pineapple", "Pineapple" }); } stream.Position = 0; string output = Encoding.Default.GetString(stream.ToArray()); string expected = "appleapplePinea" + Environment.NewLine; Assert.AreEqual(expected, output, "The values were not truncated properly."); }
public void TestAddColumn_DuplicateColumnName_Throws() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("Name"), new Window(1)); Assert.ThrowsException <ArgumentException>(() => schema.AddColumn(new Int32Column("name"), new Window(1))); }
public void ShouldTruncateOverflow() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("Default"), new Window(5)); schema.AddColumn(new StringColumn("Leading"), new Window(5) { TruncationPolicy = OverflowTruncationPolicy.TruncateLeading }); schema.AddColumn(new StringColumn("Trailing"), new Window(5) { TruncationPolicy = OverflowTruncationPolicy.TruncateTrailing }); FixedLengthOptions options = new FixedLengthOptions(); options.TruncationPolicy = OverflowTruncationPolicy.TruncateLeading; // this is the default anyway StringWriter stringWriter = new StringWriter(); FixedLengthWriter writer = new FixedLengthWriter(stringWriter, schema, options); writer.Write(new object[] { "Pineapple", "Pineapple", "Pineapple" }); string output = stringWriter.ToString(); string expected = "appleapplePinea" + Environment.NewLine; Assert.AreEqual(expected, output); }
public void ShouldWriteHeader() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("First"), new Window(10) { FillCharacter = '@' }); schema.AddColumn(new StringColumn("Second"), new Window(10) { FillCharacter = '!' }); schema.AddColumn(new StringColumn("Third"), new Window(10) { FillCharacter = '$' }); FixedLengthOptions options = new FixedLengthOptions() { IsFirstRecordHeader = true }; StringWriter stringWriter = new StringWriter(); FixedLengthWriter writer = new FixedLengthWriter(stringWriter, schema, options); writer.Write(new object[] { "Apple", "Grape", "Pear" }); string output = stringWriter.ToString(); string expected = "First@@@@@Second!!!!Third$$$$$" + Environment.NewLine + "Apple@@@@@Grape!!!!!Pear$$$$$$" + Environment.NewLine; Assert.AreEqual(expected, output); }
public void TestAddColumn_DuplicateColumnName_Throws() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("Name"), new Window(1)); schema.AddColumn(new Int32Column("name"), new Window(1)); }
public void TestRead_RecordWithCP1251Characters_ReturnsCorrectCharacters() { //---- Arrange ----------------------------------------------------- // Need to convert the string to target encoding because otherwise a string declared in VS will always be encoded as UTF-8 var text = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding(1251), Encoding.UTF8.GetBytes(@" 123 Лучиано 1/17/2014")); var schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); var options = new FixedLengthOptions() { Encoding = Encoding.GetEncoding(1251) }; var testee = new FixedLengthReader(new MemoryStream(text), schema, options); //---- Act --------------------------------------------------------- var result = testee.Read(); //---- Assert ------------------------------------------------------ Assert.IsTrue(result, "Could not read the record."); object[] expected = { 123, "Лучиано", new DateTime(2014, 1, 17) }; object[] actual = testee.GetValues(); CollectionAssert.AreEqual(expected, actual, "The wrong values were parsed."); }
public void TestGetValues_WithPartitionedRecordFilter_SkipAllRecords() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10) { Alignment = FixedAlignment.RightAligned }) .AddColumn(new StringColumn("name"), new Window(25) { Alignment = FixedAlignment.RightAligned }) .AddColumn(new DateTimeColumn("created") { InputFormat = "M/d/yyyy" }, new Window(10) { Alignment = FixedAlignment.RightAligned }); const string lines = @" 123 Bob Smith 4/21/2017 -1 Jay Smith 8/14/2017 234 Jay Smith 5/21/2017"; StringReader stringReader = new StringReader(lines); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); parser.RecordPartitioned += (sender, e) => { e.IsSkipped = true; }; Assert.IsFalse(parser.Read(), "All records should have been skipped."); }
public void TestParseValues_WrongNumber_Throws() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("name"), 10); schema.ParseValues(new string[] { "bob", "smith" }); }
public void TestParseValues_ParsesValues() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("first_name"), new Window(10)) .AddColumn(new StringColumn("last_name"), new Window(10)) .AddColumn(new DateTimeColumn("birth_date") { InputFormat = "yyyyMMdd" }, new Window(8)) .AddColumn(new Int32Column("weight"), new Window(5)); string[] values = new string[] { "bob", "smith", "20120123", "185" }; var recordContext = new RecordContext() { ExecutionContext = new FixedLengthExecutionContext() { Schema = schema, Options = new FixedLengthOptions() } }; object[] parsed = schema.ParseValues(recordContext, values); object[] expected = new object[] { "bob", "smith", new DateTime(2012, 1, 23), 185 }; CollectionAssert.AreEqual(expected, parsed); }
public void TestGetValues_NoRecordSeparator_SplitsFile() { const string text = " 123 Bob 1/19/2013 234 Sam12/20/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); FixedLengthOptions options = new FixedLengthOptions() { HasRecordSeparator = false }; StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, 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); 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); }
public FixedLengthFileSchema BuildSchema(RawFileSchema rawSchema) { var fixedLengthFileSchema = new FixedLengthFileSchema(); var rowDefinitionList = rawSchema.Schema.RowDefinitions; foreach (var rowDefinition in rowDefinitionList) { var columnDefinitions = rowDefinition.ColumnDefinitions; var rowSchema = new FixedLengthSchema(); foreach (var columnDefinition in columnDefinitions) { var definition = GetColumnDefinition(columnDefinition); definition.NullHandler = ConstantNullHandler.For("NULL"); rowSchema.AddColumn( definition, BuildWindow(columnDefinition) ); } fixedLengthFileSchema.FixedLengthRecordSchemas.Add(new FixedLengthRecordSchema { RecordIdentifier = rowDefinition.Identifier, FixedLengthSchema = rowSchema }); } return(fixedLengthFileSchema); }
public void ShouldRoundTrip() { const string message = @"Tom,Hanselman,2016-06-0426 Walking Ice,Ace "; StringReader stringReader = new StringReader(message); SeparatedValueSchema outerSchema = new SeparatedValueSchema(); outerSchema.AddColumn(new StringColumn("FirstName")); outerSchema.AddColumn(new StringColumn("LastName")); FixedLengthSchema innerSchema = new FixedLengthSchema(); innerSchema.AddColumn(new DateTimeColumn("StartDate") { InputFormat = "yyyy-MM-dd", OutputFormat = "yyyy-MM-dd" }, 10); innerSchema.AddColumn(new Int32Column("Age"), 2); innerSchema.AddColumn(new StringColumn("StageName"), new Window(20) { Alignment = FixedAlignment.RightAligned }); outerSchema.AddColumn(new FixedLengthComplexColumn("PlayerStats", innerSchema)); outerSchema.AddColumn(new StringColumn("Nickname")); SeparatedValueReader reader = new SeparatedValueReader(stringReader, outerSchema); Assert.True(reader.Read(), "A record should have been read."); object[] values = reader.GetValues(); Assert.Equal("Tom", values[0]); Assert.Equal("Hanselman", values[1]); Assert.IsType <object[]>(values[2]); object[] playerValues = (object[])values[2]; Assert.Equal(new DateTime(2016, 06, 04), playerValues[0]); Assert.Equal(26, playerValues[1]); Assert.Equal("Walking Ice", playerValues[2]); Assert.Equal("Ace", values[3]); StringWriter stringWriter = new StringWriter(); SeparatedValueWriter writer = new SeparatedValueWriter(stringWriter, outerSchema); writer.Write(values); string output = stringWriter.GetStringBuilder().ToString(); Assert.Equal(message, output); }
public void TestRead_SkipRecord_NoParsingError() { const string text = "a b c"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("A"), 8); schema.AddColumn(new DateTimeColumn("B"), 23); schema.AddColumn(new GuidColumn("C"), 2); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); bool canRead = parser.Skip(); Assert.IsTrue(canRead, "Could not skip the record."); canRead = parser.Read(); Assert.IsFalse(canRead, "No more records should have been read."); }
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 TestColumnDefinitions_GetEnumerable_Explicit() { FixedLengthSchema schema = new FixedLengthSchema(); ColumnDefinition id = new Int32Column("id"); ColumnDefinition name = new StringColumn("name"); ColumnDefinition created = new DateTimeColumn("created"); schema.AddColumn(id, 10).AddColumn(name, 25).AddColumn(created, 10); IEnumerable collection = schema.ColumnDefinitions; IEnumerator enumerator = collection.GetEnumerator(); }
public void TestColumnDefinitions_WithColumns_CountEqualsColumnCount() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), 10) .AddColumn(new StringColumn("name"), 25) .AddColumn(new DateTimeColumn("created"), 10); ColumnCollection collection = schema.ColumnDefinitions; Assert.AreEqual(3, collection.Count, "The column collection count was wrong."); }
public void TestColumnDefinitions_WithColumns_CountEqualsColumnCount() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); ColumnCollection collection = schema.ColumnDefinitions; Assert.AreEqual(3, collection.Count); }
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 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 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 TestGetSchema_SchemaProvided_WrongNumberOfColumns_Throws() { const string text = @" 123 Bob"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); Assert.Throws<FlatFileException>(() => parser.Read()); }
public void TestGetSchema_SchemaProvided_ParsesValues() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); IReader parser = new FixedLengthReader(stringReader, schema); ISchema actual = parser.GetSchema(); Assert.Same(schema, actual); }
public void TestRead_GetValuesWithoutReading_Throws() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); Assert.ThrowsException <InvalidOperationException>(() => parser.GetValues()); }
public void TestGetSchema_SchemaProvided_WrongNumberOfColumns_Throws() { const string text = @" 123 Bob"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); Assert.ThrowsException <RecordProcessingException>(() => parser.Read()); }
public void TestReaderWriter_TrailingText_RoundTripsExtra() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10) { Alignment = FixedAlignment.RightAligned }) .AddColumn(new StringColumn("name"), new Window(25) { Alignment = FixedAlignment.RightAligned }) .AddColumn(new DateTimeColumn("created") { InputFormat = "M/d/yyyy", OutputFormat = "M/d/yyyy" }, new Window(10) { Alignment = FixedAlignment.RightAligned }) .AddColumn(new StringColumn("extra"), Window.Trailing); const string lines = @" 123 Bob Smith 4/21/2017This -1 Jay Smith 8/14/2017is 234 Jay Smith 5/21/2017extra "; StringReader stringReader = new StringReader(lines); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); List <object[]> records = new List <object[]>() { AssertExtra(parser, "This"), AssertExtra(parser, "is"), AssertExtra(parser, "extra") }; Assert.IsFalse(parser.Read()); StringWriter stringWriter = new StringWriter(); FixedLengthWriter writer = new FixedLengthWriter(stringWriter, schema); foreach (object[] record in records) { writer.Write(record); } string formatted = stringWriter.ToString(); Assert.AreEqual(lines, formatted, "The records did not round-trip."); }
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 TestColumnDefinitions_FindByIndex() { FixedLengthSchema schema = new FixedLengthSchema(); ColumnDefinition id = new Int32Column("id"); ColumnDefinition name = new StringColumn("name"); ColumnDefinition created = new DateTimeColumn("created"); schema.AddColumn(id, new Window(10)) .AddColumn(name, new Window(25)) .AddColumn(created, new Window(10)); ColumnCollection collection = schema.ColumnDefinitions; Assert.AreSame(id, collection[0], "The first column definition was wrong."); Assert.AreSame(name, collection[1], "The second column definition was wrong."); Assert.AreSame(created, collection[2], "The third column definition was wrong."); }
public void TestGetSchema_SchemaProvided_ParsesValues() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); IReader parser = new FixedLengthReader(stringReader, schema); ISchema actual = parser.GetSchema(); Assert.AreSame(schema, actual); }
public void ShouldSubstituteBadValues_FixedLength() { const string data = @"ABC 2018-02-30{1234-5678-9123-000000} "; var stringReader = new StringReader(data); var schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("Int32"), 5); schema.AddColumn(new DateTimeColumn("DateTime"), 10); schema.AddColumn(new GuidColumn("Guid"), 32); var csvReader = new FixedLengthReader(stringReader, schema); csvReader.ColumnError += (sender, e) => { if (e.ColumnContext.ColumnDefinition.ColumnName == "Int32") { e.Substitution = 1; e.IsHandled = true; } else if (e.ColumnContext.ColumnDefinition.ColumnName == "DateTime") { e.Substitution = new DateTime(2018, 07, 08); e.IsHandled = true; } else if (e.ColumnContext.ColumnDefinition.ColumnName == "Guid") { e.Substitution = Guid.Empty; e.IsHandled = true; } }; Assert.IsTrue(csvReader.Read(), "Could not read the first record."); var values = csvReader.GetValues(); var expected = new object[] { 1, new DateTime(2018, 07, 08), Guid.Empty }; CollectionAssert.AreEqual(expected, values, "The wrong values were substituted."); Assert.IsFalse(csvReader.Read(), "Read too many records."); }
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 TestParseValues_ParsesValues() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("first_name"), new Window(10)) .AddColumn(new StringColumn("last_name"), new Window(10)) .AddColumn(new DateTimeColumn("birth_date") { InputFormat = "yyyyMMdd" }, new Window(8)) .AddColumn(new Int32Column("weight"), new Window(5)); string[] values = new string[] { "bob", "smith", "20120123", "185" }; object[] parsed = schema.ParseValues(values); object[] expected = new object[] { "bob", "smith", new DateTime(2012, 1, 23), 185 }; CollectionAssert.AreEqual(expected, parsed, "The values were not parsed as expected."); }
public void TestRead_ValuesAfterEndOfFile_Throws() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); Assert.IsTrue(parser.Read(), "Could not read the record."); Assert.IsFalse(parser.Read(), "We should have reached the end of the file."); Assert.ThrowsException <InvalidOperationException>(() => parser.GetValues()); }
public void TestColumnDefinitions_FindByIndex() { FixedLengthSchema schema = new FixedLengthSchema(); IColumnDefinition id = new Int32Column("id"); IColumnDefinition name = new StringColumn("name"); IColumnDefinition created = new DateTimeColumn("created"); schema.AddColumn(id, new Window(10)) .AddColumn(name, new Window(25)) .AddColumn(created, new Window(10)); ColumnCollection collection = schema.ColumnDefinitions; Assert.AreSame(id, collection[0]); Assert.AreSame(name, collection[1]); Assert.AreSame(created, collection[2]); }
public void ShouldHandleNullValues() { MemoryStream stream = new MemoryStream(); FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("NullableInt32"), new Window(5)); using (FixedLengthWriter writer = new FixedLengthWriter(stream, schema)) { writer.Write(new object[] { null }); } stream.Position = 0; string output = Encoding.Default.GetString(stream.ToArray()); string expected = " " + Environment.NewLine; Assert.AreEqual(expected, output, "The values were not truncated properly."); }
public void TestRead_ValuesAfterEndOfFile_Throws() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); Assert.True(parser.Read(), "Could not read the record."); Assert.False(parser.Read(), "We should have reached the end of the file."); Assert.Throws<InvalidOperationException>(() => parser.GetValues()); }
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_CustomFillCharacter_TrimsFill() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10) { Alignment = FixedAlignment.LeftAligned }) .AddColumn(new StringColumn("name"), new Window(25) { Alignment = FixedAlignment.LeftAligned }) .AddColumn(new DateTimeColumn("created") { InputFormat = "M/d/yyyy", OutputFormat = "M/d/yyyy" }, new Window(10) { Alignment = FixedAlignment.LeftAligned }); FixedLengthOptions options = new FixedLengthOptions() { FillCharacter = '@' }; object[] sources = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; StringWriter stringWriter = new StringWriter(); FixedLengthWriter builder = new FixedLengthWriter(stringWriter, schema, options); builder.Write(sources); StringReader stringReader = new StringReader(stringWriter.ToString()); FixedLengthReader parser = new FixedLengthReader(stringReader, schema, options); Assert.True(parser.Read(), "Could not read the first record."); object[] actual = parser.GetValues(); Assert.Equal(sources, actual); }
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."); }
public void TestAddColumn_WidthZero_Throws() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new StringColumn("name"), 0); }
public void TestAddColumn_NullDefinition_Throws() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(null, new Window(1)); }
public void TestRead_SkipRecord_NoParsingError() { const string text = "a b c"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("A"), 8); schema.AddColumn(new DateTimeColumn("B"), 23); schema.AddColumn(new GuidColumn("C"), 2); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); bool canRead = parser.Skip(); Assert.True(canRead, "Could not skip the record."); canRead = parser.Read(); Assert.False(canRead, "No more records should have been read."); }
public void TestRead_SingleRecord_ReturnsTrueOnce() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); Assert.True(parser.Read(), "Could not read the record."); object[] expected = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; object[] actual = parser.GetValues(); Assert.Equal(expected, actual); Assert.False(parser.Read(), "No more records should have been read."); }
public void TestRead_RecordWithCP1252Characters_ReturnsCorrectCharacters() { //---- Arrange ----------------------------------------------------- // Need to convert the string to target encoding because otherwise a string declared in VS will always be encoded as UTF-8 var text = Encoding.Convert(Encoding.UTF8, Encoding.GetEncoding(1252), Encoding.UTF8.GetBytes(@" 123 Müller 1/17/2014")); var schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); var options = new FixedLengthOptions() { Encoding = Encoding.GetEncoding(1252) }; var testee = new FixedLengthReader(new MemoryStream(text), schema, options); //---- Act --------------------------------------------------------- var result = testee.Read(); //---- Assert ------------------------------------------------------ Assert.IsTrue(result, "Could not read the record."); object[] expected = { 123, "Müller", new DateTime(2014, 1, 17) }; object[] actual = testee.GetValues(); CollectionAssert.AreEqual(expected, actual, "The wrong values were parsed."); }
public void TestRead_GetValuesWithoutReading_Throws() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); Assert.Throws<InvalidOperationException>(() => parser.GetValues()); }
public void TestGetValues_CustomFillCharacter_TrimsFill() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10) { Alignment = FixedAlignment.LeftAligned }) .AddColumn(new StringColumn("name"), new Window(25) { Alignment = FixedAlignment.LeftAligned }) .AddColumn(new DateTimeColumn("created") { InputFormat = "M/d/yyyy", OutputFormat = "M/d/yyyy" }, new Window(10) { Alignment = FixedAlignment.LeftAligned }); FixedLengthOptions options = new FixedLengthOptions() { FillCharacter = '@' }; object[] sources = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; using (MemoryStream stream = new MemoryStream()) { using (FixedLengthWriter builder = new FixedLengthWriter(stream, schema, options)) { builder.Write(sources); } stream.Position = 0; FixedLengthReader parser = new FixedLengthReader(stream, schema, options); Assert.IsTrue(parser.Read(), "Could not read the first record."); object[] actual = parser.GetValues(); CollectionAssert.AreEqual(sources, actual, "The values for the first record were wrong."); } }
public void TestGetValues_NoRecordSeparator_SplitsFile() { const string text = " 123 Bob 1/19/2013 234 Sam12/20/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); FixedLengthOptions options = new FixedLengthOptions() { RecordSeparator = null }; StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema, options); Assert.True(parser.Read(), "Could not read the first record."); object[] expected = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; object[] actual = parser.GetValues(); Assert.Equal(expected, actual); Assert.True(parser.Read(), "Could not read the second record."); expected = new object[] { 234, "Sam", new DateTime(2013, 12, 20) }; actual = parser.GetValues(); Assert.Equal(expected, actual); }
public void TestRead_MultipleCallsToValues_ReturnsSameValues() { const string text = @" 123 Bob 1/19/2013"; FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); StringReader stringReader = new StringReader(text); FixedLengthReader parser = new FixedLengthReader(stringReader, schema); bool canRead = parser.Read(); Assert.True(canRead, "Could not read the record."); object[] expected = new object[] { 123, "Bob", new DateTime(2013, 1, 19) }; object[] actual = parser.GetValues(); Assert.Equal(expected, actual); actual = parser.GetValues(); Assert.Equal(expected, actual); }
public void TestColumnDefinitions_WithColumns_CountEqualsColumnCount() { FixedLengthSchema schema = new FixedLengthSchema(); schema.AddColumn(new Int32Column("id"), new Window(10)) .AddColumn(new StringColumn("name"), new Window(25)) .AddColumn(new DateTimeColumn("created"), new Window(10)); ColumnCollection collection = schema.ColumnDefinitions; Assert.AreEqual(3, collection.Count, "The column collection count was wrong."); }