Beispiel #1
0
        /// <summary>Creates the column to property mapper</summary>
        protected override void CreateMappings()
        {
            ColumnMapList.Clear();
            var mapper = new ColumnToPropertyMapper <T>(Configuration, DefaultConverterFactory, ColumnIndexDefaultValue);

            ColumnMapList.AddRange(mapper.CreateWriteMap());
        }
        public void IgnoreWhenReading_WhenSpecifiedWith_IgnoreWhenWriting_InTwoAttributes_OneMapIsCreatedAndIgnoreWhenReadingIsFalse()
        {
            // Why?
            // You got both a READ and WRITE converter here!  You basically told the mapper that you wanted the
            // the CsvConverterNumber converter for reading and writing on this property type.  The purpose behind letting a user
            // specify two converter attributes is so that they could be DIFFERENT for reading and writing.  If your intent
            // is to tell the mapper to ignore it use  [CsvConverterNumber(IgnoreWhenReading = true, IgnoreWhenWriting = true)] instead!

            // Arrange
            var classUnderTest = new ColumnToPropertyMapper <ColumnToPropertyMapperTestsData1>(
                new CsvConverterConfiguration(), new DefaultTypeConverterFactory(), -1);

            var listOfHeaders = new List <string>()
            {
                "Data1", "Data2", "PercentageMuscle"
            };

            // Act
            List <ColumnToPropertyMap> maps = classUnderTest.CreateReadMap(listOfHeaders);

            // Assert
            ColumnToPropertyMap map = maps.Where(w => w.ColumnName == "PercentageMuscle").SingleOrDefault();

            Assert.IsNotNull(map);
            Assert.IsFalse(map.IgnoreWhenReading);
            Assert.IsNotNull(map.ReadConverter);
        }
        public void IgnoreWhenWriting_WhenSpecifiedWith_IgnoreWhenReading_InOneAttribute_NoMapIsCreated()
        {
            // Arrange
            var classUnderTest = new ColumnToPropertyMapper <ColumnToPropertyMapperTestsData1>(
                new CsvConverterConfiguration(), new DefaultTypeConverterFactory(), -1);

            // Act
            List <ColumnToPropertyMap> maps = classUnderTest.CreateWriteMap();

            // Assert
            ColumnToPropertyMap map = maps.Where(w => w.ColumnName == "Length").FirstOrDefault();

            Assert.IsNull(map);
        }
        public void IgnoreWhenWriting_WhenSpecifiedWithBothAsFalse_OneMapIsCreatedAndIgnoreWhenWritingIsFalse()
        {
            // It's a stupid thing to do, but it's legal.

            // Arrange
            var classUnderTest = new ColumnToPropertyMapper <ColumnToPropertyMapperTestsData1>(
                new CsvConverterConfiguration(), new DefaultTypeConverterFactory(), -1);

            // Act
            List <ColumnToPropertyMap> maps = classUnderTest.CreateWriteMap();

            // Assert
            ColumnToPropertyMap map = maps.Where(w => w.ColumnName == "HasJumperCables").SingleOrDefault();

            Assert.IsNotNull(map);
            Assert.IsFalse(map.IgnoreWhenWriting);
            Assert.IsNotNull(map.WriteConverter);
        }
        public void IgnoreWhenReading_WhenSpecifiedWith_IgnoreWhenWriting_InOneAttribute_OneMapIsCreatedAndIgnoreWhenReadingIsTrue()
        {
            // Arrange
            var classUnderTest = new ColumnToPropertyMapper <ColumnToPropertyMapperTestsData1>(
                new CsvConverterConfiguration(), new DefaultTypeConverterFactory(), -1);

            var listOfHeaders = new List <string>()
            {
                "Data1", "Data2", "Length"
            };

            // Act
            List <ColumnToPropertyMap> maps = classUnderTest.CreateReadMap(listOfHeaders);

            // Assert
            ColumnToPropertyMap map = maps.Where(w => w.ColumnName == "Length").SingleOrDefault();

            Assert.IsNotNull(map);
            Assert.IsTrue(map.IgnoreWhenReading);
            Assert.IsNull(map.ReadConverter);
        }
        /// <summary>Creates the mappings necessary for each property.</summary>
        /// <returns></returns>
        protected override void CreateMappings()
        {
            ColumnMapList.Clear();
            _columnDictionary.Clear();

            // Retrieve the header row if the file has one!
            List <string> headerColumns = Configuration.HasHeaderRow ? _rowReader.ReadRow() : new List <string>();

            // Map the class properties to a mapper
            var mapper = new ColumnToPropertyMapper <T>(Configuration, DefaultConverterFactory, ColumnIndexDefaultValue);

            ColumnMapList.AddRange(mapper.CreateReadMap(headerColumns));

            // Map all the columns into a dictionary
            foreach (ColumnToPropertyMap map in ColumnMapList)
            {
                if (map.ColumnIndex > 0)
                {
                    _columnDictionary.Add(map.ColumnIndex, map);
                }
            }
        }