Exemple #1
0
        public void ParseFile_ThrowsFileNotFound_IfFileDoesNtoExist()
        {
            // Arrange
            string wrongFileName = "non_existing_file_name.csv";

            // Act & Assert
            Assert.Throws <FileNotFoundException>(() => { _csvFileParser.ParseFile(wrongFileName); });
            Assert.Throws <FileNotFoundException>(() => { _csvFileParser.ParseFile(null); });
            Assert.Throws <FileNotFoundException>(() => { _csvFileParser.ParseFile(string.Empty); });
        }
Exemple #2
0
        public void ParseCsvFileTest()
        {
            string filePath = Path.Combine(this.TestContext.TestDeploymentDir, "Files\\1095_Import_Employees.csv");

            ICsvFileParser parser = new CsvFileParser(filePath);
            IDictionary <int, IList <string> > fileErrors = new Dictionary <int, IList <string> >();
            IObjectValidator validator = new ObjectValidator();

            int rowIndex = parser.RowStart;

            foreach (dynamic row in parser.ParseFile())
            {
                List <string> errors = new List <string>();

                Employee rowObj = FileIOUtilities.MapObject <Employee>(row, rowIndex, validator, null, ref errors);
                //rowObj.MapValues(rowIndex, row, validator, ref errors);
                validator.TryValidate(rowObj, ref errors);

                if (errors.Count > 0)
                {
                    fileErrors.Add(rowIndex, errors);
                }
                rowIndex++;
            }

            Assert.IsTrue(fileErrors.Count >= 2);
        }
Exemple #3
0
        public static IList <Company> ReadCsvFile1()
        {
            Console.WriteLine("Running ReadCsvFile1");
            var parser     = new CsvFileParser(Common.CsvDataPath);
            var fileErrors = new Dictionary <int, IList <string> >();
            var validator  = new ObjectValidator();

            //parser.Delimiters // can adjust the delimiters
            //parser.FixedWidths // can parse fixed widths

            var rowIndex  = parser.RowStart;
            var companies = new List <Company>();

            foreach (dynamic row in parser.ParseFile())
            {
                var errors = new List <string>();

                var rowObj = FileIOUtilities.MapObject <Company>(row, rowIndex, validator, null, ref errors);
                validator.TryValidate(rowObj, ref errors);
                companies.Add(rowObj);

                if (errors.Count > 0)
                {
                    fileErrors.Add(rowIndex, errors);
                }
                rowIndex++;
            }

            foreach (var errs in fileErrors)
            {
                foreach (var err in errs.Value)
                {
                    Console.WriteLine("Line:{0}, Error: {1}", errs.Key, err);
                }
            }

            return(companies);
        }
        static void Main(string[] args)
        {
            var entries = CsvFileParser.ParseFile(@"D:\temp\temperatures.csv").ToList();

            Console.WriteLine($"Total number of entries: {entries.Count}");

            var analyzer = new TemperatureAnalyzer(entries);

            var firstEntryBelowZero = analyzer.FirstEntryBelowZero();
            var coldest             = analyzer.ColdestEntry();
            var warmest             = analyzer.WarmestEntry();

            Console.WriteLine($"First entry below zero: {firstEntryBelowZero}");
            Console.WriteLine($"Coldest: {coldest}");
            Console.WriteLine($"Warmest: {warmest}");

            var averages = analyzer.AveragePerDay();

            foreach (var averageTemp in averages)
            {
                Console.WriteLine($"{averageTemp.Date.ToShortDateString()}: {Math.Round(averageTemp.Average, 1)}");
            }
        }