Ejemplo n.º 1
0
        public void TestWriter_WithSchema_SchemaCounted()
        {
            var outputMapper = new FixedLengthTypeMapper <Person>(() => new Person());

            outputMapper.Property(x => x.Name, 10);
            outputMapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber")
            {
                IncludeSchema = true
            }, 10);
            outputMapper.Property(x => x.CreatedOn, 10).OutputFormat("MM/dd/yyyy");

            var people = new[]
            {
                new Person()
                {
                    Name = "Bob", CreatedOn = new DateTime(2018, 04, 25)
                },
                new Person()
                {
                    Name = "Tom", CreatedOn = new DateTime(2018, 04, 26)
                },
                new Person()
                {
                    Name = "Jane", CreatedOn = new DateTime(2018, 04, 27)
                }
            };

            StringWriter writer = new StringWriter();

            outputMapper.Write(writer, people, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            });
            string output = writer.ToString();

            var inputMapper = new FixedLengthTypeMapper <Person>(() => new Person());

            inputMapper.Property(x => x.Name, 10);
            inputMapper.Property(x => x.RecordNumber, 10);
            inputMapper.Property(x => x.CreatedOn, 10).InputFormat("MM/dd/yyyy");

            StringReader reader  = new StringReader(output);
            var          results = inputMapper.Read(reader, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            }).ToArray();

            Assert.AreEqual(3, results.Length);
            Assert.AreEqual("Bob", results[0].Name);
            Assert.AreEqual(2, results[0].RecordNumber);
            Assert.AreEqual(new DateTime(2018, 04, 25), results[0].CreatedOn);
            Assert.AreEqual("Tom", results[1].Name);
            Assert.AreEqual(3, results[1].RecordNumber);
            Assert.AreEqual(new DateTime(2018, 04, 26), results[1].CreatedOn);
            Assert.AreEqual("Jane", results[2].Name);
            Assert.AreEqual(4, results[2].RecordNumber);
            Assert.AreEqual(new DateTime(2018, 04, 27), results[2].CreatedOn);
        }
        public void TestReader_WrappedWithIgnoredColumns()
        {
            var mapper = new FixedLengthTypeMapper <ComplicatedPerson>(() => new ComplicatedPerson());

            mapper.Property(x => x.PersonId, 10);
            mapper.Ignored(1);
            mapper.Property(x => x.Name, 10);
            mapper.Ignored(1);
            mapper.Property(x => x.CreatedOn, 10).OutputFormat("MM/dd/yyyy");

            var people = new[]
            {
                new ComplicatedPerson()
                {
                    PersonId = 1, Name = "Bob", CreatedOn = new DateTime(2018, 04, 25)
                },
                new ComplicatedPerson()
                {
                    PersonId = 2, Name = "Tom", CreatedOn = new DateTime(2018, 04, 26)
                },
                new ComplicatedPerson()
                {
                    PersonId = 3, Name = "Jane", CreatedOn = new DateTime(2018, 04, 27)
                }
            };

            StringWriter writer = new StringWriter();

            mapper.Write(writer, people, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            });
            string output = writer.ToString();

            mapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber"), 10);
            StringReader stringReader = new StringReader(output);
            var          options      = new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            };
            var reader = mapper.GetReader(stringReader, options);

            reader.RecordPartitioned += (sender, e) =>
            {
                e.IsSkipped = e.Values.Length >= 2 && e.Values[2] == "Tom";
            };
            var results = reader.ReadAll().ToArray();

            Assert.AreEqual(2, results.Length);
            Assert.AreEqual(1, results[0].PersonId);
            Assert.AreEqual("Bob", results[0].Name);
            Assert.AreEqual(new DateTime(2018, 04, 25), results[0].CreatedOn);
            Assert.AreEqual(1, results[0].RecordNumber);
            Assert.AreEqual(3, results[1].PersonId);
            Assert.AreEqual("Jane", results[1].Name);
            Assert.AreEqual(new DateTime(2018, 04, 27), results[1].CreatedOn);
            Assert.AreEqual(2, results[1].RecordNumber);
        }
Ejemplo n.º 3
0
        public void TestTypeMapper_TrailingText_RoundTripsExtra()
        {
            var mapper = new FixedLengthTypeMapper <ExtraPerson>(() => new ExtraPerson());

            mapper.Property(p => p.Id, new Window(10)
            {
                Alignment = FixedAlignment.RightAligned
            })
            .ColumnName("id");
            mapper.Property(p => p.Name, new Window(25)
            {
                Alignment = FixedAlignment.RightAligned
            })
            .ColumnName("name");
            mapper.Property(p => p.Created, new Window(10)
            {
                Alignment = FixedAlignment.RightAligned
            })
            .ColumnName("created")
            .InputFormat("M/d/yyyy")
            .OutputFormat("M/d/yyyy");
            mapper.Property(p => p.Extra, Window.Trailing).ColumnName("extra");

            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);
            List <ExtraPerson> people       = mapper.Read(stringReader).ToList();

            Assert.AreEqual(3, people.Count, "The wrong number of records were read.");
            Assert.AreEqual("This", people[0].Extra);
            Assert.AreEqual("is", people[1].Extra);
            Assert.AreEqual("extra", people[2].Extra);

            StringWriter stringWriter = new StringWriter();

            mapper.Write(stringWriter, people);

            string formatted = stringWriter.ToString();

            Assert.AreEqual(lines, formatted, "The records did not round-trip.");
        }
        public void TestReader_WithSchema_SchemaNotCounted_WithFilter_LineCount()
        {
            var mapper = new FixedLengthTypeMapper <Person>(() => new Person());

            mapper.Property(x => x.Name, 10);

            var people = new[]
            {
                new Person()
                {
                    Name = "Bob"
                },
                new Person()
                {
                    Name = "Tom"
                },
                new Person()
                {
                    Name = "Jane"
                }
            };

            StringWriter writer = new StringWriter();

            mapper.Write(writer, people, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            });
            string output = writer.ToString();

            mapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber")
            {
                IncludeSchema          = false,
                IncludeFilteredRecords = true
            }, 10);
            StringReader stringReader = new StringReader(output);
            var          options      = new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            };
            var reader = mapper.GetReader(stringReader, options);

            reader.RecordPartitioned += (sender, e) =>
            {
                e.IsSkipped = e.Values.Length == 1 && e.Values[0] == "Tom";
            };
            var results = reader.ReadAll().ToArray();

            Assert.AreEqual(2, results.Length);
            Assert.AreEqual("Bob", results[0].Name);
            Assert.AreEqual(1, results[0].RecordNumber);
            Assert.AreEqual("Jane", results[1].Name);
            Assert.AreEqual(3, results[1].RecordNumber);
        }
        public void TestReader_WithSchema_WithIgnoredColumn_NoRecordSeparator_WithFilter_LogicalRecordsOnly()
        {
            var mapper = new FixedLengthTypeMapper <Person>(() => new Person());

            mapper.Property(x => x.Name, 10);
            mapper.Ignored(1);

            var people = new[]
            {
                new Person()
                {
                    Name = "Bob"
                },
                new Person()
                {
                    Name = "Tom"
                },
                new Person()
                {
                    Name = "Jane"
                }
            };

            StringWriter writer = new StringWriter();

            mapper.Write(writer, people, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true,
                HasRecordSeparator  = false
            });
            string output = writer.ToString();

            mapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber"), 10);
            StringReader stringReader = new StringReader(output);
            var          options      = new FixedLengthOptions()
            {
                IsFirstRecordHeader = true,
                HasRecordSeparator  = false
            };
            var reader = mapper.GetReader(stringReader, options);

            reader.RecordPartitioned += (sender, e) =>
            {
                e.IsSkipped = e.Values.Length >= 1 && e.Values[0] == "Tom";
            };
            var results = reader.ReadAll().ToArray();

            Assert.AreEqual(2, results.Length);
            Assert.AreEqual("Bob", results[0].Name);
            Assert.AreEqual(1, results[0].RecordNumber);
            Assert.AreEqual("Jane", results[1].Name);
            Assert.AreEqual(2, results[1].RecordNumber);
        }
        public void TestReader_WithSchema_SchemaNotCounted_NoFilter_LineCountMinusOne()
        {
            var mapper = new FixedLengthTypeMapper <Person>(() => new Person());

            mapper.Property(x => x.Name, 10);

            var people = new[]
            {
                new Person()
                {
                    Name = "Bob"
                },
                new Person()
                {
                    Name = "Tom"
                },
                new Person()
                {
                    Name = "Jane"
                }
            };

            StringWriter writer = new StringWriter();

            mapper.Write(writer, people, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            });
            string output = writer.ToString();

            mapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber")
            {
                IncludeSchema          = false,
                IncludeFilteredRecords = true
            }, 10);
            StringReader reader  = new StringReader(output);
            var          results = mapper.Read(reader, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            }).ToArray();

            Assert.Equal(3, results.Length);
            Assert.Equal("Bob", results[0].Name);
            Assert.Equal(1, results[0].RecordNumber);
            Assert.Equal("Tom", results[1].Name);
            Assert.Equal(2, results[1].RecordNumber);
            Assert.Equal("Jane", results[2].Name);
            Assert.Equal(3, results[2].RecordNumber);
        }
        public void TestReader_WithSchema_WithFilter_LineCount()
        {
            var mapper = new FixedLengthTypeMapper <Person>(() => new Person());

            mapper.Property(x => x.Name, 10);

            var people = new[]
            {
                new Person()
                {
                    Name = "Bob"
                },
                new Person()
                {
                    Name = "Tom"
                },
                new Person()
                {
                    Name = "Jane"
                }
            };

            StringWriter writer = new StringWriter();

            mapper.Write(writer, people, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            });
            string output = writer.ToString();

            mapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber")
            {
                IncludeSchema          = true,
                IncludeFilteredRecords = true
            }, 10);
            StringReader reader  = new StringReader(output);
            var          options = new FixedLengthOptions()
            {
                IsFirstRecordHeader     = true,
                PartitionedRecordFilter = (values) => values.Length == 1 && values[0] == "Tom"
            };
            var results = mapper.Read(reader, options).ToArray();

            Assert.Equal(2, results.Length);
            Assert.Equal("Bob", results[0].Name);
            Assert.Equal(2, results[0].RecordNumber);
            Assert.Equal("Jane", results[1].Name);
            Assert.Equal(4, results[1].RecordNumber);
        }
        public void TestReader_WithSchema_NoFilter_WithIgnoredColumn_LogicalRecordsOnly()
        {
            var mapper = new FixedLengthTypeMapper <Person>(() => new Person());

            mapper.Property(x => x.Name, 10);
            mapper.Ignored(1);

            var people = new[]
            {
                new Person()
                {
                    Name = "Bob"
                },
                new Person()
                {
                    Name = "Tom"
                },
                new Person()
                {
                    Name = "Jane"
                }
            };

            StringWriter writer = new StringWriter();

            mapper.Write(writer, people, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            });
            string output = writer.ToString();

            mapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber"), 10);
            StringReader reader  = new StringReader(output);
            var          results = mapper.Read(reader, new FixedLengthOptions()
            {
                IsFirstRecordHeader = true
            }).ToArray();

            Assert.AreEqual(3, results.Length);
            Assert.AreEqual("Bob", results[0].Name);
            Assert.AreEqual(1, results[0].RecordNumber);
            Assert.AreEqual("Tom", results[1].Name);
            Assert.AreEqual(2, results[1].RecordNumber);
            Assert.AreEqual("Jane", results[2].Name);
            Assert.AreEqual(3, results[2].RecordNumber);
        }