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 TestCtor_Options_TextNull_Throws()
 {
     TextReader reader = null;
     FixedLengthSchema schema = new FixedLengthSchema();
     FixedLengthOptions options = new FixedLengthOptions();
     Assert.Throws<ArgumentNullException>(() => new FixedLengthReader(reader, schema, options));
 }
Exemple #3
0
        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 TestCtor_TextNull_Throws()
        {
            string            text   = null;
            FixedLengthSchema schema = new FixedLengthSchema();

            new FixedLengthReader(text, schema);
        }
        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 TestCtor_SchemaNull_Throws()
        {
            string            text   = String.Empty;
            FixedLengthSchema schema = null;

            new FixedLengthReader(new MemoryStream(Encoding.Default.GetBytes(text)), schema);
        }
        public void TestParseValues_WrongNumber_Throws()
        {
            FixedLengthSchema schema = new FixedLengthSchema();

            schema.AddColumn(new StringColumn("name"), 10);
            schema.ParseValues(new string[] { "bob", "smith" });
        }
Exemple #8
0
        public void TestColumnDefinitions_NoColumns_CountZero()
        {
            FixedLengthSchema schema     = new FixedLengthSchema();
            ColumnCollection  collection = schema.ColumnDefinitions;

            Assert.Equal(0, collection.Count);
        }
        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 TestCtor_OptionsNull_Throws()
 {
     string text = String.Empty;
     FixedLengthSchema schema = new FixedLengthSchema();
     FixedLengthParserOptions options = null;
     new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema, options);
 }
        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));
            Assert.ThrowsException <ArgumentException>(() => schema.AddColumn(new Int32Column("name"), new Window(1)));
        }
Exemple #13
0
        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 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 TestColumnDefinitions_NoColumns_CountZero()
        {
            FixedLengthSchema schema     = new FixedLengthSchema();
            ColumnCollection  collection = schema.ColumnDefinitions;

            Assert.AreEqual(0, collection.Count, "The column collection count was wrong.");
        }
        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
            {
                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 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 FixedLengthRecordContext()
            {
                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 TestCtor_SchemaNull_Throws()
        {
            StringReader      reader = new StringReader(String.Empty);
            FixedLengthSchema schema = null;

            Assert.Throws <ArgumentNullException>(() => new FixedLengthReader(reader, schema));
        }
 public void TestCtor_Options_TextNull_Throws()
 {
     Stream stream = null;
     FixedLengthSchema schema = new FixedLengthSchema();
     FixedLengthParserOptions options = new FixedLengthParserOptions();
     new FixedLengthParser(stream, schema, options);
 }
        public void TestCtor_Options_TextNull_Throws()
        {
            TextReader         reader  = null;
            FixedLengthSchema  schema  = new FixedLengthSchema();
            FixedLengthOptions options = new FixedLengthOptions();

            Assert.ThrowsException <ArgumentNullException>(() => new FixedLengthReader(reader, schema, options));
        }
Exemple #21
0
        public void TestCtor_OptionsNull_Throws()
        {
            string                   text    = String.Empty;
            FixedLengthSchema        schema  = new FixedLengthSchema();
            FixedLengthParserOptions options = null;

            new FixedLengthParser(new MemoryStream(Encoding.Default.GetBytes(text)), schema, options);
        }
Exemple #22
0
        public void TestCtor_Options_TextNull_Throws()
        {
            Stream                   stream  = null;
            FixedLengthSchema        schema  = new FixedLengthSchema();
            FixedLengthParserOptions options = new FixedLengthParserOptions();

            new FixedLengthParser(stream, schema, options);
        }
 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();
 }
Exemple #24
0
        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_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);
        }
Exemple #26
0
        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 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.");
        }
Exemple #29
0
        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());
        }
Exemple #31
0
        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();
        }
        private static object[] AssertExtra(FixedLengthReader reader, String expected)
        {
            Assert.IsTrue(reader.Read(), "Could not read the next record.");
            object[]          values = reader.GetValues();
            FixedLengthSchema schema = reader.GetSchema();

            Assert.AreEqual(schema.ColumnDefinitions.Count, values.Length, "The wrong number of values were parsed.");
            object value = values[schema.ColumnDefinitions.Count - 1];

            Assert.AreEqual(expected, value, "The wrong extra value was found for the record.");
            return(values);
        }
        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 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.");
 }
Exemple #35
0
        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 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 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 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 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.");
        }
 /// <summary>
 /// Initializes a new instance of a FixedLengthParser.
 /// </summary>
 /// <param name="stream">A stream containing the records to parse.</param>
 /// <param name="schema">The schema object defining which columns are in each record.</param>
 /// <param name="options">An object containing settings for configuring the parser.</param>
 /// <exception cref="System.ArgumentNullException">The stream is null.</exception>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options object is null.</exception>
 public FixedLengthParser(Stream stream, FixedLengthSchema schema, FixedLengthParserOptions options)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (schema == null)
     {
         throw new ArgumentNullException("schema");
     }
     if (options == null)
     {
         throw new ArgumentNullException("options");
     }
     this.stream = stream;
     StreamReader reader = new StreamReader(stream);
     text = reader.ReadToEnd();
     this.schema = schema;
     recordSeparator = options.RecordSeparator;
     filler = options.FillCharacter;
 }
 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 TestAddColumn_NullDefinition_Throws()
 {
     FixedLengthSchema schema = new FixedLengthSchema();
     schema.AddColumn(null, new Window(1));
 }
 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_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 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 TestCtor_TextNull_Throws()
 {
     string text = null;
     FixedLengthSchema schema = new FixedLengthSchema();
     new FixedLengthParser(text, schema);
 }
 public void TestColumnDefinitions_NoColumns_CountZero()
 {
     FixedLengthSchema schema = new FixedLengthSchema();
     ColumnCollection collection = schema.ColumnDefinitions;
     Assert.AreEqual(0, collection.Count, "The column collection count was wrong.");
 }
 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();
 }
 /// <summary>
 /// Initializes a new instance of a FixedLengthParser.
 /// </summary>
 /// <param name="stream">A stream containing the records to parse.</param>
 /// <param name="schema">The schema object defining which columns are in each record.</param>
 /// <exception cref="System.ArgumentNullException">The stream is null.</exception>
 /// <exception cref="System.ArgumentNullException">The schema object is null.</exception>
 public FixedLengthParser(Stream stream, FixedLengthSchema schema)
     : this(stream, schema, new FixedLengthParserOptions())
 {
 }
 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 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.");
 }
        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 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_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_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());
        }
 /// <summary>
 /// Initializes a new instance of a FixedLengthParser.
 /// </summary>
 /// <param name="fileName">The path to the file containing the records to parse.</param>
 /// <param name="schema">The schema object defining which columns are in each record.</param>
 /// <param name="options">An object containing settings for configuring the parser.</param>
 /// <exception cref="System.ArgumentNullException">The schema is null.</exception>
 /// <exception cref="System.ArgumentNullException">The options object is null.</exception>
 public FixedLengthParser(string fileName, FixedLengthSchema schema, FixedLengthParserOptions options)
     : this(File.OpenRead(fileName), schema, options)
 {
 }
        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_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 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);
        }