예제 #1
0
        public void InvalidKeyThrowsException()
        {
            // prepare
            var mockHttp = new MockHttpMessageHandler();

            mockHttp
            .When(OpenWeatherService.EndPointRoot + "*")
            .Respond(HttpStatusCode.Unauthorized);
            // assert
            IOpenWeatherService client = new OpenWeatherService(null, mockHttp);

            Assert.ThrowsAsync <WeatherException>(async() => await client.GetCurrentWeather(AlcatrazLatitude, AlcatrazLongitude));
            Assert.ThrowsAsync <WeatherException>(async() => await client.GetForecast(AlcatrazLatitude, AlcatrazLongitude));

            client = new OpenWeatherService("", mockHttp);
            Assert.ThrowsAsync <WeatherException>(async() => await client.GetForecastDSL(AlcatrazLatitude, AlcatrazLongitude, OWUnit.Imperial));
            Assert.ThrowsAsync <WeatherException>(async() => await client.GetForecast(AlcatrazLatitude, AlcatrazLongitude));

            client = new OpenWeatherService("fake_key", mockHttp);
            Assert.ThrowsAsync <WeatherException>(async() => await client.GetCurrentWeather(BolognaCityName, OWUnit.Standard, Language.Catalan));
        }
예제 #2
0
        public async void GetForecastDSL_ByCoordinates_Test()
        {
            // prepare SI
            ForecastDSL output = null;

            using (var stream = new BufferedStream(File.OpenRead("./Resources/OpenW_onecall_SI.json"), 8192)) {
                var mockHttp = new MockHttpMessageHandler();
                mockHttp
                .When(OpenWeatherService.EndPointRoot + "*")
                .Respond("application/json", stream);
                IOpenWeatherService client = new OpenWeatherService("12345", mockHttp);

                output = await client.GetForecastDSL(BolognaLatitude, BolognaLongitude, OWUnit.Metric, Language.Italian);

                stream.Close();
            }
            // assert
            output.ShouldNotBeNull();
            output.Coordinates.Latitude.ShouldBe(44.47);
            output.Coordinates.Longitude.ShouldBe(11.43);
            output.TimeZone.ShouldBe("Europe/Rome");
            output.TimeZoneOffset.ShouldBe(3600);

            output.Currently.ShouldNotBeNull();
            output.Currently.Time.ToUnixTimeSeconds().ShouldBe(1609711216L);
            output.Currently.Wind.Speed.ShouldBe(1.5f);
            output.Currently.Wind.Bearing.ShouldBe(260);
            output.Currently.Visibility.ShouldBe(10000);
            output.Currently.Rain.ShouldBe(0.24f);
            output.Currently.Snow.ShouldBe(1.01f);
            output.Currently.ProbOfPrecipitation.ShouldBeNull();
            output.Currently.SunriseTime.Value.ToUnixTimeSeconds().ShouldBe(1609656638);
            output.Currently.SunsetTime.Value.ToUnixTimeSeconds().ShouldBe(1609688798);
            //output.Current.SunriseTime.ShouldBe(((int)1609656638).ToDateTimeOffset());
            output.Currently.SunriseTime.ShouldBe(new DateTime(2021, 01, 03, 6, 50, 38, DateTimeKind.Utc));;
            output.Currently.SunsetTime.Value.ShouldBeGreaterThan(output.Currently.SunriseTime.Value);
            output.Currently.Temperature.Daily.ShouldBe(4.14f);
            output.Currently.Temperature.DewPoint.ShouldBe(4.11f);
            output.Currently.ApparentTemperature.Daily.ShouldBe(1.8f);
            output.Currently.Temperature.Humidity.ShouldBe(100);
            output.Currently.Temperature.Pressure.ShouldBe(1010);

            output.Hourly.Count.ShouldBe(48);
            output.Hourly[0].Visibility.ShouldBe(10000);
            output.Hourly[0].Rain.ShouldBe(0.24f);
            output.Hourly[0].ProbOfPrecipitation.ShouldBe(0.3f);
            output.Hourly[0].Temperature.Daily.ShouldBe(4.14f);
            output.Hourly[0].Temperature.DewPoint.ShouldBe(4.11f);
            output.Hourly[0].Temperature.Min.ShouldBeNull();
            output.Hourly[0].Temperature.Max.ShouldBeNull();
            output.Hourly[0].Temperature.Night.ShouldBeNull();
            output.Hourly[0].Temperature.Morning.ShouldBeNull();
            output.Hourly[0].Temperature.Humidity.ShouldBe(100);
            output.Hourly[0].Temperature.Pressure.ShouldBe(1010);
            output.Hourly[0].ApparentTemperature.Daily.ShouldBe(1.7f);
            output.Hourly[0].ApparentTemperature.Min.ShouldBeNull();
            output.Hourly[0].ApparentTemperature.Max.ShouldBeNull();
            output.Hourly[0].ApparentTemperature.Night.ShouldBeNull();
            output.Hourly[0].ApparentTemperature.Morning.ShouldBeNull();
            output.Hourly[0].Temperature.Humidity.ShouldBe(100);

            output.Daily.Count.ShouldBe(8);
            output.Daily[0].Visibility.ShouldBeNull();
            output.Daily[0].Rain.ShouldBe(0.58f);
            output.Daily[0].Snow.ShouldBe(0.99f);
            output.Daily[0].ProbOfPrecipitation.ShouldBe(0.6f);
            output.Daily[0].UVIndex.ShouldBe(0.94f);
            output.Daily[0].Temperature.Daily.ShouldBe(9.5f);
            output.Daily[0].Temperature.Night.ShouldBe(4.16f);
            output.Daily[0].Temperature.Evening.ShouldBe(6.87f);
            output.Daily[0].Temperature.Morning.ShouldBe(5.93f);
            output.Daily[0].Temperature.Min.ShouldBe(4.14f);
            output.Daily[0].Temperature.Max.ShouldBe(9.5f);
            output.Daily[0].Temperature.Humidity.ShouldBe(74);
            output.Daily[0].Temperature.Pressure.ShouldBe(1009);
            output.Daily[0].Temperature.DewPoint.ShouldBe(4.83f);
            output.Daily[0].ApparentTemperature.Daily.ShouldBe(7.3f);
            output.Daily[0].ApparentTemperature.Night.ShouldBe(1.7f);
            output.Daily[0].ApparentTemperature.Evening.ShouldBe(5.18f);
            output.Daily[0].ApparentTemperature.Morning.ShouldBe(2.8f);
            output.Daily[0].ApparentTemperature.Min.ShouldBeNull();
            output.Daily[0].ApparentTemperature.Max.ShouldBeNull();

            output.Alerts.ShouldNotBeNull();
            output.Alerts.Count.ShouldBe(2);
            output.Alerts[0].Sender.ShouldBe("NWS Tulsa (Eastern Oklahoma)");
            output.Alerts[0].Title.ShouldBe("Heat Advisory");
            output.Alerts[0].StartTime.ToUnixTimeSeconds().ShouldBe(1597341600);
            output.Alerts[0].ExpiresTime.ToUnixTimeSeconds().ShouldBe(1597366800);
            output.Alerts[0].Description.ShouldStartWith("...HEAT ADVISORY REMAINS IN EFFECT");
            output.Alerts[0].Severity.ShouldBe(Severity.Unknown);

            output.Alerts[1].Sender.ShouldBe("Second sender name");
            output.Alerts[1].Title.ShouldBe("Cold Advisory");
            output.Alerts[1].Description.ShouldBe("...Test\nWeather\nCondition.");

            //// prepare imperial
            //var outputImperial = await client.GetForecastDSL(BolognaLatitude, BolognaLongitude, OWUnit.Imperial, Language.English);
            //// assert
            //outputImperial.Coordinates.Latitude.ShouldBe(output.Coordinates.Latitude);
            //outputImperial.Coordinates.Longitude.ShouldBe(output.Coordinates.Longitude);
            //outputImperial.Currently.Temperature.Daily.ShouldNotBeNull();
            //outputImperial.Currently.Temperature.Daily.Value.ShouldBeGreaterThan(output.Currently.Temperature.Daily.Value);
            //outputImperial.Currently.Wind.Speed.ShouldNotBe(output.Currently.Wind.Speed);
            //outputImperial.Currently.Wind.Bearing.ShouldBe(output.Currently.Wind.Bearing);
        }