Exemple #1
0
        public void DaysWithRainfall_Year_Test(string year)
        {
            CsvParserJsonWriterService cps = new CsvParserJsonWriterService();
            List <WeatherDataModel>    weatherDataModel = cps.ReadCsvFile($"{_filePath}Full_Download_Data.csv");

            var  convertDataService = new ConvertDataService();
            Root root = convertDataService.ConvertCSVToJson(weatherDataModel);

            int countDaysWithRainfall = 0;

            foreach (var w in weatherDataModel)
            {
                if (w.Year == year && !string.IsNullOrWhiteSpace(w.RainfallAmount) && decimal.Parse(w.RainfallAmount) > 0)
                {
                    countDaysWithRainfall += 1;
                }
            }

            var theDRecord = from y in root.WeatherData.WeatherDataForYear
                             where y.Year == int.Parse(year)
                             select y;

            var theDaysWithRainfall = theDRecord.FirstOrDefault().DaysWithRainfall;

            Assert.Equal(theDaysWithRainfall, countDaysWithRainfall);
        }
Exemple #2
0
        public void AverageDailyRainfall_Year_Test(string year)
        {
            CsvParserJsonWriterService cps = new CsvParserJsonWriterService();
            List <WeatherDataModel>    weatherDataModel = cps.ReadCsvFile($"{_filePath}Full_Download_Data.csv");

            var  convertDataService = new ConvertDataService();
            Root root = convertDataService.ConvertCSVToJson(weatherDataModel);


            List <decimal> totalRainfall = new List <decimal>();

            foreach (var w in weatherDataModel)
            {
                if (w.Year == year && !string.IsNullOrWhiteSpace(w.RainfallAmount))
                {
                    totalRainfall.Add(decimal.Parse(w.RainfallAmount));
                }
            }

            var theDRecord = from y in root.WeatherData.WeatherDataForYear
                             where y.Year == int.Parse(year)
                             select y;

            var theAverageDailyRainfall = theDRecord.FirstOrDefault().AverageDailyRainfall;

            Assert.Equal(theAverageDailyRainfall, Math.Round(totalRainfall.Average(), 3));
        }
Exemple #3
0
        public void LastRecordedDate_Year_Test(string year)
        {
            CsvParserJsonWriterService cps = new CsvParserJsonWriterService();
            List <WeatherDataModel>    weatherDataModel = cps.ReadCsvFile($"{_filePath}Full_Download_Data.csv");

            var  convertDataService = new ConvertDataService();
            Root root = convertDataService.ConvertCSVToJson(weatherDataModel);


            List <DateTime> alldateTimes = new List <DateTime>();

            foreach (var w in weatherDataModel)
            {
                if (w.Year == year && !string.IsNullOrWhiteSpace(w.RainfallAmount))
                {
                    alldateTimes.Add(new DateTime(int.Parse(w.Year), int.Parse(w.Month), int.Parse(w.Day)));
                }
            }

            var theDRecord = from y in root.WeatherData.WeatherDataForYear
                             where y.Year == int.Parse(year)
                             select y;

            var theDate = theDRecord.FirstOrDefault().LastRecordedDate;

            Assert.Equal(theDate, alldateTimes.Max());
        }
Exemple #4
0
        public void ConvertCsvToJson(string csvFilePath)
        {
            // Read CSV
            var csvParserService = new CsvParserJsonWriterService();
            var result           = csvParserService.ReadCsvFile(csvFilePath);

            // Convert to Object
            var  convertDataService = new ConvertDataService();
            Root root = convertDataService.ConvertCSVToJson(result);

            // Write json File
            var jsonfilePath = Configuration["jsonfilePath"];

            csvParserService.WriteJsonFile(root, jsonfilePath);
        }
Exemple #5
0
        public void GetLongestNumberOfDaysRaining_Test(int year)
        {
            CsvParserJsonWriterService cps = new CsvParserJsonWriterService();
            List <WeatherDataModel>    weatherDataModel = cps.ReadCsvFile($"{_filePath}Full_Download_Data.csv");

            var weatherDataModelForYear = (from x in weatherDataModel
                                           where int.Parse(x.Year) == year
                                           orderby x.Month, x.Day
                                           select x);

            int longestNumber      = 0;
            int longestNumberSoFar = 0;

            foreach (var w in weatherDataModelForYear)
            {
                if (string.IsNullOrWhiteSpace(w.RainfallAmount))
                {
                    continue;
                }

                if (decimal.Parse(w.RainfallAmount) > 0)
                {
                    longestNumber++;
                }
                else
                {
                    longestNumberSoFar = longestNumber > longestNumberSoFar ? longestNumber : longestNumberSoFar;
                    longestNumber      = 0;
                }
            }

            var  convertDataService = new ConvertDataService();
            Root root = convertDataService.ConvertCSVToJson(weatherDataModel);

            var longestNoInObject =
                (from y in root.WeatherData.WeatherDataForYear
                 where y.Year == year
                 select y.LongestNumberOfDaysRaining).FirstOrDefault();

            Assert.Equal(longestNumberSoFar, longestNoInObject);
        }
Exemple #6
0
        public void FutureDatesRemoved_Test(int monthNo)
        {
            CsvParserJsonWriterService cps = new CsvParserJsonWriterService();
            List <WeatherDataModel>    weatherDataModel = cps.ReadCsvFile($"{_filePath}Data_With_Future_Dates.csv");

            var  convertDataService = new ConvertDataService();
            Root root = convertDataService.ConvertCSVToJson(weatherDataModel);

            Assert.Equal(2020, root.WeatherData.WeatherDataForYear.LastOrDefault().Year);

            Assert.Equal(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(monthNo),
                         root.WeatherData.WeatherDataForYear.LastOrDefault().MonthlyAggregates.WeatherDataForMonth
                         .LastOrDefault().Month);

            // Future Data entered with 1.0 as Rainfall for each day until 20/Sep, test to ensure , data read is less than today's date
            if (DateTime.Now < new DateTime(2020, 09, 21))
            {
                Assert.Equal(DateTime.Now.Day,
                             root.WeatherData.WeatherDataForYear.LastOrDefault().MonthlyAggregates.WeatherDataForMonth
                             .LastOrDefault().TotalRainfall);
            }
        }