Example #1
0
        public void MapByFilePathCustomMappings()
        {
            // arrange
            var mapper = new CsvMapper<CurryOrder>();

            // make sure calling GetHeaders does not affect the result
            mapper.GetHeaders(this.CurryOrderFilePath);
            mapper.GetHeaders(this.CurryOrderFilePath);
            mapper.GetHeaders(this.CurryOrderFilePath);

            // act
            mapper.OnItemDataBound += this.CurryOrder_OnItemDataBound;
            var result = mapper.Map(this.CurryOrderFilePath);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
            Assert.AreEqual(3, result.Count);
            var lastRow = result.LastOrDefault();
            Assert.IsNotNull(lastRow);
            Assert.AreEqual("Korma", lastRow.Curry);
            Assert.AreEqual(2, lastRow.Quantity);
            Assert.AreEqual(12.5M, lastRow.Price);
            Assert.AreEqual(25M, lastRow.Total);
        }
Example #2
0
        public void MapByFilePathCustomMappings()
        {
            // arrange
            var mapper = new CsvMapper <CurryOrder>();

            // make sure calling GetHeaders does not affect the result
            mapper.GetHeaders(this.CurryOrderFilePath);
            mapper.GetHeaders(this.CurryOrderFilePath);
            mapper.GetHeaders(this.CurryOrderFilePath);

            // act
            mapper.OnItemDataBound += this.CurryOrder_OnItemDataBound;
            var result = mapper.Map(this.CurryOrderFilePath);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
            Assert.AreEqual(3, result.Count);
            var lastRow = result.LastOrDefault();

            Assert.IsNotNull(lastRow);
            Assert.AreEqual("Korma", lastRow.Curry);
            Assert.AreEqual(2, lastRow.Quantity);
            Assert.AreEqual(12.5M, lastRow.Price);
            Assert.AreEqual(25M, lastRow.Total);
        }
Example #3
0
        public void MapByFilePathParseValues()
        {
            // arrange
            var mapper   = new CsvMapper <CurryOrder>();
            var mappings = new NameValueCollection();

            mappings.Add("Curry", "0");
            mappings.Add("Quantity", "1");
            mappings.Add("Price", "2");
            mappings.Add("Total", "3");

            // make sure calling GetHeaders does not affect the result
            mapper.GetHeaders(this.CurryOrderFilePath);
            mapper.GetHeaders(this.CurryOrderFilePath);
            mapper.GetHeaders(this.CurryOrderFilePath);

            // act
            var result = mapper.Map(this.CurryOrderFilePath, mappings);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
            Assert.AreEqual(3, result.Count);
            var lastRow = result.LastOrDefault();

            Assert.IsNotNull(lastRow);
            Assert.AreEqual("Korma", lastRow.Curry);
            Assert.AreEqual(2, lastRow.Quantity);
            Assert.AreEqual(12.5M, lastRow.Price);
            Assert.AreEqual(25M, lastRow.Total);
        }
        public void MappingWithRowLimit2()
        {
            var mapper = new CsvMapper <TestClass>(new MappingOptions
            {
                RowsLimit = 100,
                HasHeader = true
            });
            var items = mapper.Map(TestCsvPath);

            Assert.IsNotNull(items, "Result is null");
            Assert.IsTrue(items.Any(), "Items empty");

            items.SimpleMappingTrace(TestCsvPath);
        }
        public void SimpleMappingByPath()
        {
            var mapper = new CsvMapper <TestClass>(new MappingOptions
            {
                MappingMode = MappingMode.ByNumber,
                HasHeader   = false
            });
            var items = mapper.Map(TestCsvPath);

            Assert.IsNotNull(items, "Result is null");
            Assert.IsTrue(items.Any(), "Items empty");

            items.SimpleMappingTrace(TestCsvPath);
        }
Example #6
0
        public void map_should_throw_missing_mapping_exception_for_not_mapings()
        {
            //Given
            var csvMapper = new CsvMapper <Employee>();
            var csvLines  = new List <string> {
                "john",
                "mary,[email protected]"
            };

            //When
            Action action = () => csvMapper.Map(csvLines);

            //Then
            action.Should().Throw <MissingMappingException>();
        }
        public void SimpleMappingByStream()
        {
            var mapper = new CsvMapper <TestClass>(new MappingOptions {
            });

            using (var fs = new FileStream(TestCsvPath, FileMode.Open))
            {
                var items = mapper.Map(fs);

                Assert.IsNotNull(items, "Result is null");
                Assert.IsTrue(items.Any(), "Result empty");

                items.SimpleMappingTrace(TestCsvPath);
            }
        }
 public void MappingWithRowLimit()
 {
     try
     {
         var mapper = new CsvMapper <TestClass>(new MappingOptions {
             RowsLimit = 99
         });
         var items = mapper.Map(TestCsvPath);
     }
     catch (TableMappingException ex)
     {
         Trace.WriteLine($"{ex.Message}; Row {ex.Row}");
         return;
     }
     Assert.Fail($"{nameof(TableMappingException)} has not been thrown");
 }
        public void SimpleMappingByPath()
        {
            var mapper = new CsvMapper <TestClass>(
                new MappingOptions
            {
                MappingMode = MappingMode.ByNumber,
                SuppressConvertTypeErrors = false,
                EnableValidation          = true,
                HasHeader = true
            });
            var items = mapper.Map(TestCsvPath);

            Assert.IsNotNull(items, "Result is null");
            Assert.IsTrue(items.Any(), "Items empty");

            items.SimpleMappingTrace(TestCsvPath);
        }
        public void MappingWithValidation()
        {
            var mapper = new CsvMapper <ValidationTestClass>(new MappingOptions {
                EnableValidation = true
            });

            try
            {
                var items = mapper.Map(ValidationTestCsvPath);
            }
            catch (TableMappingException ex)
            {
                Trace.WriteLine($"{ex.Message}; Row {ex.Row}");
                return;
            }

            Assert.Fail($"{nameof(TableMappingException)} has not been thrown");
        }
Example #11
0
        public void map_should_throw_exception_for_invalid_data()
        {
            //Given
            var csvMapper = new CsvMapper <Employee>();

            csvMapper.CreateMap(0, e => e.Name);
            csvMapper.CreateMap(1, e => e.Email);
            var csvLines = new List <string> {
                "john",
                "mary,[email protected]"
            };

            //When
            Action action = () => csvMapper.Map(csvLines);

            //then
            action.Should().Throw <Exception>();
        }
        public void MappingWithSuppressConvertTypeErrors()
        {
            try
            {
                var mapper = new CsvMapper <TestClass>(new MappingOptions {
                    SuppressConvertTypeErrors = false
                });
                var items = mapper.Map(SuppressConvertTypeErrorsTestCsvPath);
                Assert.IsNotNull(items, "Result is null");
                Assert.IsTrue(items.Any(), "Items empty");

                items.SimpleMappingTrace(TestCsvPath);
            }
            catch (TableMappingException ex)
            {
                Trace.WriteLine($"{ex.Message}; Row {ex.Row}");
                return;
            }
            Assert.Fail($"{nameof(TableMappingException)} has not been thrown");
        }
Example #13
0
        public void map_should_map_csv_data()
        {
            //Given
            var csvMapper = new CsvMapper <Employee>();

            csvMapper.CreateMap(0, e => e.Name);
            csvMapper.CreateMap(1, e => e.Email);
            var csvLines = new List <string> {
                "john,[email protected]",
                "mary,[email protected]"
            };

            //When
            var output = csvMapper.Map(csvLines);

            //Then
            output.Should().NotBeNullOrEmpty();
            output.Count().Should().Be(2);
            output.First().Name.Should().Be("john");
            output.First().Email.Should().Be("*****@*****.**");
        }
Example #14
0
        public void MapByFilePath()
        {
            // arrange
            var mapper = new CsvMapper<TranslationLabel>();
            var mappings = this.GetTranslationLabelMappings();

            // make sure calling GetHeaders does not affect the result
            mapper.GetHeaders(this.TranslationLabelFilePath);
            mapper.GetHeaders(this.TranslationLabelFilePath);
            mapper.GetHeaders(this.TranslationLabelFilePath);

            // act
            var result = mapper.Map(this.TranslationLabelFilePath, mappings);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
            Assert.AreEqual(22, result.Count);
            var firstRow = result.FirstOrDefault();
            Assert.IsNotNull(firstRow);
            Assert.AreEqual("InfinitiMonthlyPayment", firstRow.LabelName);
        }
Example #15
0
        public virtual MappingResponse Map(List <string> filePaths, NameValueCollection mappings)
        {
            var rows = new List <T>();
            var log  = new List <LogEntry>();

            foreach (var filePath in filePaths)
            {
                var mapper = new CsvMapper <T>();
                rows.AddRange(mapper.Map(filePath, mappings).ToList());
                log.AddRange(mapper.Log);
            }

            this.Rows = rows;

            var response = new MappingResponse
            {
                ResultsAsTable = ReflectionUtility.GetDataTableByReflection <T>(rows),
                Log            = log
            };

            return(response);
        }
Example #16
0
        public void MapByFilePath()
        {
            // arrange
            var mapper   = new CsvMapper <TranslationLabel>();
            var mappings = this.GetTranslationLabelMappings();

            // make sure calling GetHeaders does not affect the result
            mapper.GetHeaders(this.TranslationLabelFilePath);
            mapper.GetHeaders(this.TranslationLabelFilePath);
            mapper.GetHeaders(this.TranslationLabelFilePath);

            // act
            var result = mapper.Map(this.TranslationLabelFilePath, mappings);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
            Assert.AreEqual(22, result.Count);
            var firstRow = result.FirstOrDefault();

            Assert.IsNotNull(firstRow);
            Assert.AreEqual("InfinitiMonthlyPayment", firstRow.LabelName);
        }
Example #17
0
        public void MapByFilePathParseValues()
        {
            // arrange
            var mapper = new CsvMapper<CurryOrder>();
            var mappings = new NameValueCollection();
            mappings.Add("Curry", "0");
            mappings.Add("Quantity", "1");
            mappings.Add("Price", "2");
            mappings.Add("Total", "3");

            // make sure calling GetHeaders does not affect the result
            mapper.GetHeaders(this.CurryOrderFilePath);
            mapper.GetHeaders(this.CurryOrderFilePath);
            mapper.GetHeaders(this.CurryOrderFilePath);

            // act
            var result = mapper.Map(this.CurryOrderFilePath, mappings);

            // assert
            Assert.IsNotNull(result);
            Assert.IsTrue(result.Count > 0);
            Assert.AreEqual(3, result.Count);
            var lastRow = result.LastOrDefault();
            Assert.IsNotNull(lastRow);
            Assert.AreEqual("Korma", lastRow.Curry);
            Assert.AreEqual(2, lastRow.Quantity);
            Assert.AreEqual(12.5M, lastRow.Price);
            Assert.AreEqual(25M, lastRow.Total);
        }