示例#1
0
        public async Task Get_Returns_Instance_Of_IActionResult()
        {
            // Act
            var result = await _weatherController.Get("London,uk");

            // Assert
            Assert.IsInstanceOf <ActionResult <WeatherViewModel> >(result);
        }
示例#2
0
        public void Get_Given_CountryAndCity_Returns_WeatherModelMappedFromWeather()
        {
            //arrange
            var weatherFromService = new Weather(
                new Temperature("", 0), 0);
            var weatherService = Substitute.For <IWeatherService>();

            weatherService.GetWeather(Arg.Any <string>(), Arg.Any <string>())
            .Returns(weatherFromService);

            WeatherModel resultFromMapper = new WeatherModel();
            var          mapper           = Substitute.For <ICustomMapper>();

            mapper.Map <WeatherModel>(weatherFromService).Returns(resultFromMapper);

            var sut = new WeatherController(weatherService, mapper)
            {
                Request       = new HttpRequestMessage(),
                Configuration = new HttpConfiguration()
            };

            //act
            var result = sut.Get(Arg.Any <string>(), Arg.Any <string>());

            //assert
            result.ShouldBe(resultFromMapper);
        }
        public async void GetWeatherInfo_ReturnBadRequest_EmptyStringOrNullOrSpaces(string query)
        {
            var mock = new Mock <IWeatherAggregator>();
            WeatherController controller = new WeatherController(mock.Object);

            var response = await controller.Get(query);

            Assert.IsType <BadRequestObjectResult>(response.Result);
        }
示例#4
0
 public void CanGetWeatherEvents()
 {
     _context = CreateAndSeedContext();
     using (var controller = new WeatherController(_context, null))
     {
         var results = controller.Get();
         Assert.Equal(7, results.Count());
     }
 }
示例#5
0
 public void CanGetWeatherEventsFilteredByDate()
 {
     _context = CreateAndSeedContext();
     using (var controller = new WeatherController(_context, null))
     {
         var results = controller.Get(DateTime.Now.Date);
         Assert.Equal(2, results.Count());
     }
 }
示例#6
0
        public void WeatherResponse()
        {
            WeatherController _weather = new WeatherController();
            var     responseString     = _weather.Get();
            dynamic _json  = JObject.Parse(responseString.ToString());
            int     status = (int)_json.Status;

            Assert.Equal(0, status);
        }
        public void Test3()
        {
            // not found location
            WeatherService weatherService = new WeatherService();
            var            controller     = new WeatherController(weatherService);
            var            result         = controller.Get("bogus", "fake");

            Assert.Equal("404", result.StatusCode.ToString());
        }
        public async void GetWeatherInfo_ReturnBadRequest_WrongCoordinates(double lat, double lng)
        {
            var mock = new Mock <IWeatherAggregator>();
            WeatherController controller = new WeatherController(mock.Object);

            var response = await controller.Get(lat, lng);

            Assert.IsType <BadRequestObjectResult>(response.Result);
        }
示例#9
0
        public void Get_Method_Returns_Forbidden()
        {
            WeatherController controller = new WeatherController();

            controller.Request       = new HttpRequestMessage();
            controller.Configuration = new HttpConfiguration();

            var response = controller.Get();

            Assert.AreEqual(response.StatusCode, System.Net.HttpStatusCode.Forbidden, "GET method should return a Forbidden response message.");
        }
示例#10
0
        public async void Get()
        {
            //Arrange
            WeatherService    service    = new WeatherService();
            WeatherController controller = new WeatherController(service);

            //Act
            var msg = await controller.Get(1);

            //Assert
            Assert.IsNotNull(msg);
        }
示例#11
0
        public void GetReturnsNullWhenApiReponseIsNull()
        {
            IForecastProxy forecastProxy = MockRepository.GenerateMock <IForecastProxy>();

            int cityId = 12345;

            forecastProxy.Expect(x => x.GetForecastByCityId(cityId)).Return(null);

            WeatherController controller = new WeatherController(forecastProxy);
            var response = controller.Get(cityId);

            Assert.IsNull(response);
        }
示例#12
0
        public void Test1()
        {
            // warsaw location
            WeatherService weatherService = new WeatherService();
            var            controller     = new WeatherController(weatherService);
            var            result         = controller.Get("Poland", "Warsaw");
            WeatherInfo    w = (WeatherInfo)result.Value;

            Assert.Equal("poland", w.Location.Country.ToLower());
            Assert.Equal("warsaw", w.Location.City.ToLower());
            Assert.Equal("Celcius", w.Temperature.format.ToString());
            Assert.Equal("16", w.Temperature.value.ToString());
            Assert.Equal("88", w.Humidity.ToString());
        }
示例#13
0
        public void Test2()
        {
            // gdansk location
            WeatherService weatherService = new WeatherService();
            var            controller     = new WeatherController(weatherService);
            var            result         = controller.Get("Poland", "Gdansk");
            WeatherInfo    w = (WeatherInfo)result.Value;

            Assert.Equal("poland", w.Location.Country.ToLower());
            Assert.Equal("gdansk", w.Location.City.ToLower());
            Assert.Equal("Celcius", w.Temperature.format.ToString());
            Assert.Equal("22", w.Temperature.value.ToString());
            Assert.Equal("33", w.Humidity.ToString());
        }
示例#14
0
        public void GetReturnsEmptyObjectWhenApiResponseIsEmptyJson()
        {
            IForecastProxy forecastProxy = MockRepository.GenerateMock <IForecastProxy>();

            int cityId = 12345;
            ForecastResponse apiresponse = new ForecastResponse("{\"cod\":\"0\"}");

            forecastProxy.Expect(x => x.GetForecastByCityId(cityId)).Return(apiresponse);

            WeatherController controller = new WeatherController(forecastProxy);
            var response = controller.Get(cityId);

            Assert.IsNotNull(response);
            Assert.AreEqual(response, apiresponse);
        }
        public async void GetWeatherInfo_ReturnsOk_CorrectCoordinates(double lat, double lng)
        {
            var expected = new CommonWeatherDto {
                Description = "Test description"
            };

            var mock = new Mock <IWeatherAggregator>();

            mock.Setup(x => x.GetFirstResponseAsync(lat, lng)).Returns(Task.FromResult(expected));
            WeatherController controller = new WeatherController(mock.Object);

            var result = await controller.Get(lat, lng);

            Assert.Equal(result.Value, expected);
        }
示例#16
0
        public void GetCapturesAllForecastsWhenApiResponseContainsMultipleForecasts()
        {
            IForecastProxy forecastProxy = MockRepository.GenerateMock <IForecastProxy>();

            int cityId = 2643743;
            ForecastResponse apiresponse = new ForecastResponse(GetJson());

            forecastProxy.Expect(x => x.GetForecastByCityId(cityId)).Return(apiresponse);

            WeatherController controller = new WeatherController(forecastProxy);
            var response = controller.Get(cityId);

            Assert.IsNotNull(response);
            Assert.AreEqual(response, apiresponse);
            Assert.AreEqual(7, response.Forecasts.Count);
        }
示例#17
0
        public void Get()
        {
            // Arrange
            WeatherController controller    = new WeatherController(new WeatherService());
            WeatherFilter     weatherFilter = new WeatherFilter()
            {
                Country = "US", City = "Abbeville"
            };
            // Act
            WeatherDataVIewModel weatherDataVIewModel = controller.Get(weatherFilter);

            // Assert
            Assert.IsNotNull(weatherDataVIewModel);
            Assert.AreEqual(weatherFilter.Country, weatherDataVIewModel.Country);
            Assert.AreEqual(weatherFilter.City, weatherDataVIewModel.City);
        }
        public void PolandWarsawTest()
        {
            var weatherService = Substitute.For <IWeatherService>();

            var result = new WeatherData
            {
                Format   = "F",
                Humidity = 88,
                Value    = 49
            };

            weatherService.GetData("Poland", "Warsaw").Returns(result);

            var weatherController = new WeatherController(weatherService);

            Assert.NotNull(weatherController.Get("Poland", "Warsaw"));
        }
        public void NullValuesTest()
        {
            var weatherService = Substitute.For <IWeatherService>();

            var result = new WeatherData
            {
                Format   = "F",
                Humidity = 88,
                Value    = 49
            };

            weatherService.GetData(Arg.Any <string>(), Arg.Any <string>()).Returns((WeatherData)null);

            var weatherController = new WeatherController(weatherService);

            Assert.Throws <Exception>(() => weatherController.Get("A", "B"));
        }
示例#20
0
        private void button1_Click(object sender, EventArgs e)
        {
            REST_WinformDEMO.Controllers.WeatherController controller = new WeatherController();
            List <WeatherInfo> get = controller.Get();
            StringBuilder      sb  = new StringBuilder(
                );

            foreach (WeatherInfo itemInfo in get)
            {
                sb.Append(itemInfo.Location);
                sb.Append(Environment.NewLine);
                sb.Append(itemInfo.Degree);
                sb.Append(Environment.NewLine);
                sb.Append(itemInfo.DateTime);
            }

            this.richTextBox1.Text = sb.ToString();
        }
示例#21
0
        public void Test5()
        {
            // use NSubstitute to return a not found location
            var         ws = Substitute.For <WeatherService>();
            WeatherInfo w  = new WeatherInfo {
                Location = new Location {
                    LocationId = 0, Country = "bogus", City = "fake"
                }
            };

            ws.Get(Arg.Any <Location>()).Returns(w);

            WeatherService weatherService = new WeatherService();
            var            controller     = new WeatherController(ws);
            var            result         = controller.Get("Poland", "Warsaw");

            Assert.Equal("404", result.StatusCode.ToString());
        }
        public void GermanyBerlinTest()
        {
            var weatherService = Substitute.For <IWeatherService>();

            var result = new WeatherData
            {
                Format   = "C",
                Humidity = 70,
                Value    = 19
            };

            weatherService.GetData("Germany", "Berlin").Returns(result);


            var weatherController = new WeatherController(weatherService);

            Assert.NotNull(weatherController.Get("Germany", "Berlin"));
        }
示例#23
0
        public void GetWeatherDescriptionWhenApiResponseContainsDescription()
        {
            IForecastProxy forecastProxy = MockRepository.GenerateMock <IForecastProxy>();

            int cityId = 2643743;
            ForecastResponse apiresponse = new ForecastResponse(GetJson());

            forecastProxy.Expect(x => x.GetForecastByCityId(cityId)).Return(apiresponse);

            WeatherController controller = new WeatherController(forecastProxy);
            var response = controller.Get(cityId);

            Assert.IsNotNull(response);
            Assert.AreEqual(response, apiresponse);
            Assert.AreEqual("Rain", response.Forecasts[0].WeatherList[0].Summary);
            Assert.AreEqual("light rain", response.Forecasts[0].WeatherList[0].Description);
            Assert.AreEqual("10d", response.Forecasts[0].WeatherList[0].Icon);
        }
        public void GetWeatherDetails()
        {
            var cityName = "Sydney Airport";
            var data     = "{\"coord\":{\"lon\":151.21,\"lat\":-33.87},\"weather\":[{\"id\":500,\"main\":\"Rain\",\"description\":\"light rain\",\"icon\":\"10n\"}],\"base\":\"stations\",\"main\":{\"temp\":295.291,\"pressure\":1022.65,\"humidity\":96,\"temp_min\":295.291,\"temp_max\":295.291,\"sea_level\":1028.67,\"grnd_level\":1022.65},\"wind\":{\"speed\":0.82,\"deg\":17.5047},\"rain\":{\"3h\":0.6075},\"clouds\":{\"all\":92},\"dt\":1486073421,\"sys\":{\"message\":0.0111,\"country\":\"AU\",\"sunrise\":1485976708,\"sunset\":1486025936},\"id\":2147714,\"name\":\"Sydney\",\"cod\":200}";

            var weatherDetails = new ServiceResult <WeatherMap>
            {
                Result  = JsonConvert.DeserializeObject <WeatherMap>(data),
                Error   = "",
                Success = true
            };

            weatherMapService.Setup(s => s.GetWeatherDetails(It.IsAny <string>())).Returns(weatherDetails);

            var target = new WeatherController(weatherMapService.Object);
            HttpResponseMessage result = target.Get(request, cityName) as HttpResponseMessage;

            Assert.IsTrue(result.StatusCode == HttpStatusCode.OK);
        }
示例#25
0
        public async Task weather_controller_get_should_return_weather_object()
        {
            var fixture            = new Fixture();
            var city               = "Katowice";
            var weatherDto         = fixture.Create <WeatherDto>();
            var weatherServiceMock = new Mock <IWeatherService>();

            weatherServiceMock.Setup(x => x.GetAsync(city)).ReturnsAsync(weatherDto);

            var controller = new WeatherController(weatherServiceMock.Object);

            var result = await controller.Get(city) as OkObjectResult;

            result.Should().NotBeNull();
            var expectedDto = result.Value as WeatherDto;

            weatherDto.ShouldBeEquivalentTo(expectedDto);
            weatherServiceMock.Verify(x => x.GetAsync(city), Times.Once);
        }
示例#26
0
        public void TestGetCitiesInputValidation()
        {
            IDynamicRepository repository = new DynamicRepository();
            var controller = new WeatherController(repository);
            var moqContext = new Mock <HttpContextBase>();

            controller.Request = new HttpRequestMessage()
            {
                Properties = { { HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration() } }
            };

            // as the web service faling need to mock.
            controller.Request.Headers.Add("USE-MOCK", "True");
            controller.Configuration = new HttpConfiguration();

            var response    = controller.Get(null);
            var httpReponse = response as System.Web.Http.Results.BadRequestErrorMessageResult;

            Assert.AreEqual("Country Name is missing", httpReponse.Message);
        }
示例#27
0
        public void TestGetCitiesForCountryAustralia()
        {
            IDynamicRepository repository = new DynamicRepository();
            var controller = new WeatherController(repository);
            var moqContext = new Mock <HttpContextBase>();

            controller.Request = new HttpRequestMessage()
            {
                Properties = { { HttpPropertyKeys.HttpConfigurationKey, new HttpConfiguration() } }
            };

            // as the web service faling need to mock.
            controller.Request.Headers.Add("USE-MOCK", "True");
            controller.Configuration = new HttpConfiguration();

            var response    = controller.Get("Australia");
            var httpReponse = response as System.Web.Http.Results.OkNegotiatedContentResult <IEnumerable <string> >;

            Assert.AreEqual(3, httpReponse.Content.Count());
        }
示例#28
0
        public void GetReturnsCityObjectWhenApiResponseContainsCityJson()
        {
            IForecastProxy forecastProxy = MockRepository.GenerateMock <IForecastProxy>();

            int cityId = 2643743;
            ForecastResponse apiresponse = new ForecastResponse(GetJson());

            forecastProxy.Expect(x => x.GetForecastByCityId(cityId)).Return(apiresponse);

            WeatherController controller = new WeatherController(forecastProxy);
            var response = controller.Get(cityId);

            Assert.IsNotNull(response);
            Assert.AreEqual(response, apiresponse);
            Assert.AreEqual(2643743, response.City.Id);
            Assert.AreEqual("London", response.City.Name);
            Assert.AreEqual("GB", response.City.CountryCode);
            Assert.AreEqual(51.5085, response.City.Latitude);
            Assert.AreEqual(-0.1258, response.City.Longitude);
        }
        public async Task WeatherController_returns_Weather()
        {
            _serviceMock
            .Setup(repo => repo.GetWeatherAsync())
            .Returns(Task.FromResult(MockHelpers.GetMockWeatherView()));

            var result = await _controller.Get();

            var viewResult = Assert.IsType <OkObjectResult>(result);
            var model      = Assert.IsAssignableFrom <WeatherViewModel>(viewResult.Value);

            WeatherViewModel srcView = MockHelpers.GetMockWeatherView();

            Assert.Equal(srcView.ConditionsLabel, model.ConditionsLabel);
            Assert.Equal(srcView.ConditionsDesc, model.ConditionsDesc);
            Assert.Equal(srcView.DayOrNight, model.DayOrNight);
            Assert.Equal(srcView.CurrentDate, model.CurrentDate);
            Assert.Equal(srcView.CurrentTime, model.CurrentTime);
            Assert.Equal(srcView.FeelsLike, model.FeelsLike);
            Assert.Equal(srcView.Temperature, model.Temperature);
        }
示例#30
0
        public void GetTempratureForTheDayWhenApiResponseContainsDailyTempratureForecast()
        {
            IForecastProxy forecastProxy = MockRepository.GenerateMock <IForecastProxy>();

            int cityId = 2643743;
            ForecastResponse apiresponse = new ForecastResponse(GetJson());

            forecastProxy.Expect(x => x.GetForecastByCityId(cityId)).Return(apiresponse);

            WeatherController controller = new WeatherController(forecastProxy);
            var response = controller.Get(cityId);

            Assert.IsNotNull(response);
            Assert.AreEqual(response, apiresponse);
            Assert.AreEqual(17.6, response.Forecasts[0].Temperature.Day);
            Assert.AreEqual(17.6, response.Forecasts[0].Temperature.Evening);
            Assert.AreEqual(17.6, response.Forecasts[0].Temperature.Max);
            Assert.AreEqual(17.6, response.Forecasts[0].Temperature.Max);
            Assert.AreEqual(14.16, response.Forecasts[0].Temperature.Night);
            Assert.AreEqual(17.6, response.Forecasts[0].Temperature.Morning);
        }