public async void TestGetWeather() { var openForecast = Option <OpenWeathermapForecast> .Some(new OpenWeathermapForecast { list = new WeatherList[] {} }); openWeathermapService.GetCurrentWeather(Arg.Any <int>()) .Returns(Task.FromResult(Option <OpenWeathermapCurrent> .Some(new OpenWeathermapCurrent()))); openWeathermapService.GetWeatherforecast(Arg.Any <int>()) .Returns(Task.FromResult(openForecast)); mapper.Map <OpenWeathermapCurrent, Backend.Weatherforecast.Service.Weather>(Arg.Any <OpenWeathermapCurrent>()) .Returns(new Backend.Weatherforecast.Service.Weather()); mapper.Map <WeatherList[], Backend.Weatherforecast.Service.Weather[]>(Arg.Any <WeatherList[]>()) .Returns(new Backend.Weatherforecast.Service.Weather[] { new Backend.Weatherforecast.Service.Weather { Humidity = 42, Temperature = 13 } }); IWeatherService service = new WeatherService(logger, mapper, openWeathermapService, TestUtilities.ZipcodeToCitiesProvider, citynamesIds); var resultOpt = await service.GetWeather(TestUtilities.CityIdHamburg).ConfigureAwait(false); resultOpt .Some(result => { Assert.NotNull(result); Assert.Equal(42f, result.AverageHumidity); Assert.Equal(13f, result.AverageTemperature); }) .None(() => Assert.False(true, "Test Failed")); }
/// <summary> /// Retrieves the data for the weather /// </summary> /// <param name="cityId">Id of the city</param> /// <returns>Some(data) for the weather. None, when the cityId is unknown</returns> public async Task <Option <WeatherModel> > GetWeather(int cityId) { var currentTaskOption = openWeathermapService.GetCurrentWeather(cityId); var forecastTaskOption = openWeathermapService.GetWeatherforecast(cityId); Task.WaitAll(currentTaskOption, forecastTaskOption); Option <OpenWeathermapCurrent> openWeatherMapCurrentOption = await currentTaskOption; Option <OpenWeathermapForecast> openWeatherMapForecastOption = await forecastTaskOption; return(openWeatherMapCurrentOption .Some(openWeatherMapCurrent => { Weather current = mapper.Map <OpenWeathermapCurrent, Weather>(openWeatherMapCurrent); return openWeatherMapForecastOption .Some(openWeatherMapForecast => { Weather[] forecast = mapper.Map <WeatherList[], Weather[]>(openWeatherMapForecast.list); var model = new WeatherModel(current, forecast); model.AverageHumidity = model.CalculateAverageHumidity(); model.AverageTemperature = model.CalculateAverageTemperature(); return Option <WeatherModel> .Some(model); }) .None(() => Option <WeatherModel> .None); }) .None(() => Option <WeatherModel> .None)); }