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

            outputMapper.Property(x => x.Name, 10);
            outputMapper.Ignored(1);
            outputMapper.WriteOnlyProperty("RecordNumber", new RecordNumberColumn("RecordNumber")
            {
                IncludeSchema = true
            }, 10);
            outputMapper.Ignored(1);
            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.Ignored(1);
            inputMapper.Property(x => x.RecordNumber, 10);
            inputMapper.Ignored(1);
            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);
        }