public async void GetCurrentWeatherByCoordinates_Test() { // prepare Forecast output = null; using (var stream = new BufferedStream(File.OpenRead("./Resources/DarkSky_GetCurrentWeather_US.json"), 8192)) { var mockHttp = new MockHttpMessageHandler(); mockHttp .When(DarkSkyService.EndPointRoot + "*") .Respond("application/json", stream); IDarkSkyService client = new DarkSkyService("a_valid_key", mockHttp); output = await client.GetCurrentWeather(AlcatrazLatitude, AlcatrazLongitude, DSUnit.US, Language.English); stream.Close(); } // assert Assert.NotNull(output); Assert.NotNull(output.Currently); Assert.NotNull(output.Hourly); Assert.NotNull(output.Daily); output.Coordinates.ShouldNotBeNull(); output.Coordinates.Latitude.ShouldBe(37.8267); output.Coordinates.Longitude.ShouldBe(-122.423); output.TimeZone.ShouldBe("America/Los_Angeles"); output.TimeZoneOffset.ShouldBe(-8.0f); output.Alerts.ShouldNotBeNull(); output.Alerts.Count.ShouldBe(2); output.Alerts[0].Title.ShouldBe("High Wind Warning"); output.Alerts[0].Description.ShouldNotBeNullOrEmpty(); output.Alerts[0].ExpiresTime.ShouldBe(1611759600.ToDateTimeOffset()); output.Alerts[0].Uri.ShouldStartWith("https://alerts.weather.gov/"); output.Alerts[1].Title.ShouldBe("Wind Advisory"); output.Currently.ShouldNotBeNull(); output.Currently.UnixTime.ShouldBe(1611642210); output.Currently.Time.ToUnixTimeSeconds().ShouldBe(output.Currently.UnixTime); output.Currently.Wind.Speed.ShouldBe(9.23f); output.Currently.Wind.Bearing.ShouldBe(308); output.Currently.Visibility.ShouldBe(10f); output.Hourly.Data.Count.ShouldBe(49); output.Hourly.Data[0].ApparentTemperature.Daily.ShouldBe(40.9f); output.Daily.Data.Count.ShouldBe(8); output.Daily.Data[1].Temperature.Max.ShouldBe(49.4f); }
public void InvalidKeyThrowsException() { // prepare var mockHttp = new MockHttpMessageHandler(); mockHttp .When(DarkSkyService.EndPointRoot + "*") .Respond(HttpStatusCode.Unauthorized); // assert var client = new DarkSkyService(null, mockHttp); Assert.ThrowsAsync <WeatherException>(async() => await client.GetCurrentWeather(AlcatrazLatitude, AlcatrazLongitude)); Assert.ThrowsAsync <WeatherException>(async() => await client.GetForecast(AlcatrazLatitude, AlcatrazLongitude)); Assert.ThrowsAsync <WeatherException>(async() => await client.GetWeatherByDate(AlcatrazLatitude, AlcatrazLongitude, DateTime.Today.AddDays(+1))); client = new DarkSkyService(string.Empty, mockHttp); Assert.ThrowsAsync <WeatherException>(async() => await client.GetCurrentWeather(AlcatrazLatitude, AlcatrazLongitude)); Assert.ThrowsAsync <WeatherException>(async() => await client.GetForecast(AlcatrazLatitude, AlcatrazLongitude)); Assert.ThrowsAsync <WeatherException>(async() => await client.GetWeatherByDate(AlcatrazLatitude, AlcatrazLongitude, DateTime.Today.AddDays(+1))); client = new DarkSkyService("fake_key", mockHttp); Assert.ThrowsAsync <WeatherException>(async() => await client.GetWeatherByDate(AlcatrazLatitude, AlcatrazLongitude, DateTime.Today.AddDays(+1))); }