示例#1
0
        public void TestWriter_WithSchema_SchemaCounted()
        {
            var outputMapper = new SeparatedValueTypeMapper <Person>(() => new Person());

            outputMapper.Property(x => x.Name);
            outputMapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber")
            {
                IncludeSchema = true
            });
            outputMapper.Property(x => x.CreatedOn).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 SeparatedValueOptions()
            {
                IsFirstRecordSchema = true
            });
            string output = writer.ToString();

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

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

            StringReader reader  = new StringReader(output);
            var          results = inputMapper.Read(reader, new SeparatedValueOptions()
            {
                IsFirstRecordSchema = 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_WithSchema_SchemaNotCounted_WithFilter_LineCount()
        {
            var mapper = new SeparatedValueTypeMapper <Person>(() => new Person());

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

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

            StringWriter writer = new StringWriter();

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

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

            reader.RecordRead += (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_SchemaNotCounted_NoFilter_LineCountMinusOne()
        {
            var mapper = new SeparatedValueTypeMapper <Person>(() => new Person());

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

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

            StringWriter writer = new StringWriter();

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

            mapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber")
            {
                IncludeSchema          = false,
                IncludeFilteredRecords = true
            });
            StringReader reader  = new StringReader(output);
            var          results = mapper.Read(reader, new SeparatedValueOptions()
            {
                IsFirstRecordSchema = 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 SeparatedValueTypeMapper <Person>(() => new Person());

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

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

            StringWriter writer = new StringWriter();

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

            mapper.CustomProperty(x => x.RecordNumber, new RecordNumberColumn("RecordNumber")
            {
                IncludeSchema          = true,
                IncludeFilteredRecords = true
            });
            StringReader reader  = new StringReader(output);
            var          options = new SeparatedValueOptions()
            {
                IsFirstRecordSchema     = 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);
        }