Пример #1
0
        static void Main(string[] args)
        {
            IWeatherServiceSettings settings = new OpenWeatherSettings(key, url);
            IWeatherService         service  = new OpenWeatherService(settings);

            // View Component

            var cities = Enum.GetValues(typeof(City)).Cast <City>().ToArray();

            while (true)
            {
                Console.WriteLine("Select city:");

                for (int i = 0; i < cities.Length; i++)
                {
                    Console.WriteLine($"{i+1}. {cities[i]}");
                }

                Console.Write("Enter city number: ");

                if (!int.TryParse(Console.ReadLine(), out int cityNumber) || (cityNumber - 1) < 0 || (cityNumber - 1) >= cities.Length)
                {
                    Console.Clear();
                    Console.WriteLine("Wrong city number. Enter one more time.");
                    continue;
                }

                var city = cities[cityNumber - 1];

                var query = new QueryByCityId((int)city);

                //var query = new QueryByCityName("London");

                var weather = service.GetForecast(query);

                Console.Clear();

                WeatherForecastConsolePresenter.Show(weather);

                Console.WriteLine("\nOne more time? \"Y\" - yes, other input or empty string - no.");

                var input = Console.ReadLine();

                if (input.ToLower() == "y")
                {
                    Console.Clear();
                    continue;
                }
                else
                {
                    break;
                }
            }
        }
Пример #2
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));
        }
Пример #3
0
        public async void GetForecast_ByCoordinates_Test()
        {
            // prepare SI
            MultiWeatherConditions output = null;

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

                output = await client.GetForecast(BolognaLatitude, BolognaLongitude, OWUnit.Metric, Language.French);

                stream.Close();
            }
            // assert
            output.ShouldNotBeNull();
            output.City.ShouldNotBeNull();
            output.City.Coordinates.ShouldNotBeNull();
            output.City.Coordinates.Latitude.ShouldBe(44.4743);
            output.City.Coordinates.Longitude.ShouldBe(11.4301);
            output.City.ShouldNotBeNull();
            output.City.Country.ShouldBe("IT");
            output.City.CountryCode.ShouldBeNullOrEmpty();
            output.City.Name.ShouldBe("Bologne");
            output.City.TimeZone.ShouldBeNullOrEmpty();
            output.City.TimeZoneOffset.ShouldBe(3600);
            output.City.SunriseTime.ShouldBe(1611470448.ToDateTimeOffset());
            output.City.SunsetTime.ShouldBe(1611504699.ToDateTimeOffset());
            output.Items.ShouldNotBeNull();
            output.Items.ShouldNotBeEmpty();
            output.Items[0].Visibility.ShouldBe(10000);
            output.Items[0].Icon.ShouldBe("10d");
            output.Items[0].Description.ShouldBe("légère pluie");
            output.Items[0].UnixTime.ShouldBe(1611489600);
            output.Items[0].Wind.Speed.ShouldBe(3.47f);
            output.Items[0].Wind.Bearing.ShouldBe(310);
        }