public void GetAverageReading() { var location = "London"; var testReadings = new List<Reading>(); testReadings.Add(new Reading { Location = location, WeatherSourceName = "BBC Weather", TemperatureValue = 10.0, TemperatureUnit = TemperatureUnit.Celsius, WindSpeedValue = 8.0, WindSpeedUnit = WindSpeedUnit.Kph }); testReadings.Add(new Reading { Location = location, WeatherSourceName = "Accu Weather", TemperatureValue = 68.0, TemperatureUnit = TemperatureUnit.Fahrenheit, WindSpeedValue = 10.0, WindSpeedUnit = WindSpeedUnit.Mph }); Mock<IWeatherProvider> mockWeatherProvider = new Mock<IWeatherProvider>(); mockWeatherProvider.Setup(x => x.GetAllReadings(It.IsAny<string>(), It.IsAny<List<WeatherSource>>())).ReturnsAsync(testReadings); var testSettings = TestHelpers.SetupDefaultSettings(); Mock<IRepository> mockRepository = new Mock<IRepository>(); mockRepository.Setup(x => x.LoadSettings(It.IsAny<string>())).Returns(testSettings); IUnitConverter unitConverter = new UnitConverter(); var homeController = new HomeController(mockRepository.Object, mockWeatherProvider.Object, unitConverter); //Given temperatures of 10c from bbc and 68f from accuweather when searching then display either 15c or 59f (the average). var weatherForecast1 = new WeatherForecast { Readings = testReadings, TemperatureUnit = TemperatureUnit.Celsius, WindSpeedUnit = WindSpeedUnit.Kph }; homeController.EvaluateAverageReading(weatherForecast1); weatherForecast1.AverageTemperature.Should().BeApproximately(15, 0.001); weatherForecast1.AverageWindSpeed.Should().BeApproximately(12, 0.001); // Given wind speeds of 8kph from bbc and 10mph from accuweather when searching then display either 12kph or 7.5mph (the average). var weatherForecast2 = new WeatherForecast { Readings = testReadings, TemperatureUnit = TemperatureUnit.Fahrenheit, WindSpeedUnit = WindSpeedUnit.Mph }; homeController.EvaluateAverageReading(weatherForecast2); weatherForecast2.AverageTemperature.Should().BeApproximately(59, 0.001); weatherForecast2.AverageWindSpeed.Should().BeApproximately(7.5, 0.001); }
public void EvaluateAverageReading(WeatherForecast weatherForecast) { if (weatherForecast.Readings.Count == 0) return; weatherForecast.AverageTemperature = weatherForecast.AverageWindSpeed = 0.0; foreach (var reading in weatherForecast.Readings) { double temperatureValue = _unitConverter.Convert(reading.TemperatureValue, reading.TemperatureUnit, weatherForecast.TemperatureUnit); double windSpeedValue = _unitConverter.Convert(reading.WindSpeedValue, reading.WindSpeedUnit, weatherForecast.WindSpeedUnit); if (!double.IsNaN(temperatureValue)) weatherForecast.AverageTemperature += temperatureValue; if (!double.IsNaN(windSpeedValue)) weatherForecast .AverageWindSpeed += windSpeedValue; } weatherForecast.AverageTemperature /= weatherForecast.Readings.Count; weatherForecast.AverageWindSpeed /= weatherForecast.Readings.Count; }
public async Task<ActionResult> WeatherForecast(WeatherForecast weatherForecast) { if (weatherForecast != null && !string.IsNullOrWhiteSpace(weatherForecast.Location)) { var rootFolder = Server.MapPath("~/App_Data"); var settings = _repository.LoadSettings(rootFolder); try { weatherForecast.Readings = await _weatherProvider.GetAllReadings(weatherForecast.Location, settings.WeatherSources); EvaluateAverageReading(weatherForecast); } catch (Exception ex) { LoggerManager.WriteError(_thisClass, ex.Message); } } return View(weatherForecast); }