示例#1
0
        public async Task <ClothingRecommendation> GetClothingRecommendation(ClothingRecommendationRequest request)
        {
            var rules = await _clothingRuleRepository.GetRulesByUser(request.User?.Id);

            if (rules == null)
            {
                return(null);
            }
            var placeList = PlaceList.Instance;

            if (!placeList.IsLoaded)
            {
                var places = _weatherForecastService.GetAllPlaces();
                placeList.Load(places);
            }
            var place           = placeList.GetClosestPlace(request.Location);
            var weatherForecast = await _weatherForecastService.GetWeatherForecast(place);

            weatherForecast.LimitTo(request.Time.From, request.Time.To);
            var stats = weatherForecast.GetStats();

            return(new ClothingRecommendation(
                       rules.Where(rule => rule.Match(stats)),
                       weatherForecast,
                       place));
        }
示例#2
0
        public async Task <ActionResult <IEnumerable <ClothingRule> > > Get(ClothingRecommendationRequest request)
        {
            var clothingRecommendationRequest = request.ToDomainModel();
            var recommendation = await _service.GetClothingRecommendation(clothingRecommendationRequest);

            var viewModel = new ClothingRecommendation(recommendation);

            return(Ok(viewModel));
        }
示例#3
0
        public async Task TestInvalidLatitudePosition()
        {
            var mockIClothingRuleRepository = new Mock <IClothingRuleRepository>();
            var mockIWeatherForecastService = new Mock <IWeatherForecastService>();

            var timeFrom = new DateTime(2000, 01, 01);
            var timeTo   = new DateTime(2010, 01, 01);

            var timePeriod = new TimePeriod(timeFrom, timeTo);
            var location   = new Location(null, null);

            var clothingService = new ClothingRecommendationService(mockIWeatherForecastService.Object, mockIClothingRuleRepository.Object);
            var clothingRequest = new ClothingRecommendationRequest(timePeriod, location);

            Assert.ThrowsAsync <NotAValidLocationException>(async() => await clothingService.GetClothingRecommendation(clothingRequest));
        }
        public async Task TestATimeThereIsNoForecastFor()
        {
            // arrange
            var testDate     = new DateTime(2000, 1, 1, 10, 0, 0);
            var testPeriod   = new TimePeriod(testDate, testDate.AddHours(10));
            var testLocation = new Location(59, 10);

            var mockWeatherForecastService = new Mock <IWeatherForecastService>();
            var mockClothingRuleRepository = new Mock <IClothingRuleRepository>();

            mockClothingRuleRepository
            .Setup(crr => crr.GetRulesByUser(It.IsAny <Guid?>()))
            .ReturnsAsync(new[]
            {
                new ClothingRule(-20, 10, null, "Bobledress"),
                new ClothingRule(10, 20, null, "Bukse og genser"),
                new ClothingRule(20, 40, null, "T-skjore og shorts"),
            });


            mockWeatherForecastService
            .Setup(fs => fs.GetAllPlaces())
            .Returns(new[] { new Place("", "", "", "Larvik", new Location(59, 0)), });

            mockWeatherForecastService
            .Setup(fs => fs.GetWeatherForecast(It.IsAny <Place>()))
            .ReturnsAsync(new WeatherForecast(new[] {
                new TemperatureForecast(
                    25,
                    new DateTime(2010, 1, 1, 12, 0, 0),
                    new DateTime(2010, 1, 1, 14, 0, 0)),
            }));

            // act
            var request        = new ClothingRecommendationRequest(testPeriod, testLocation);
            var service        = new ClothingRecommendationService(mockWeatherForecastService.Object, mockClothingRuleRepository.Object);
            var recommendation = await service.GetClothingRecommendation(request);

            // assert
            Assert.IsNull(recommendation);
            //var exception = Assert.ThrowsAsync<CannotGiveMinOrMaxWithNoNumbersException>(
            //    () => service.GetClothingRecommendation(request));
            //Assert.IsInstanceOf<CannotGiveMinOrMaxWithNoNumbersException>(exception);
            //Assert.That(recommendation.Rules, Has.Exactly(0).Items);
        }
        public async Task TestHappyCase()
        {
            // arrange
            var testDate     = new DateTime(2000, 1, 1, 10, 0, 0);
            var testPeriod   = new TimePeriod(testDate, testDate.AddHours(10));
            var testLocation = new Location(59, 10);

            var mockWeatherForecastService = new Mock <IWeatherForecastService>();
            var mockClothingRuleRepository = new Mock <IClothingRuleRepository>();

            mockClothingRuleRepository
            .Setup(crr => crr.GetRulesByUser(It.IsAny <Guid?>()))
            .ReturnsAsync(new[]
            {
                new ClothingRule(-20, 10, null, "Bobledress"),
                new ClothingRule(10, 20, null, "Bukse og genser"),
                new ClothingRule(20, 40, null, "T-skjore og shorts"),
            });


            mockWeatherForecastService
            .Setup(fs => fs.GetAllPlaces())
            .Returns(new[] { new Place("", "", "", "Andeby", new Location(59.1f, 10.1f)), });

            mockWeatherForecastService
            .Setup(fs => fs.GetWeatherForecast(It.IsAny <Place>()))
            .ReturnsAsync(new WeatherForecast(new[] {
                new TemperatureForecast(25, testDate.AddHours(2), testDate.AddHours(4)),
            }));

            // act
            var request = new ClothingRecommendationRequest(testPeriod, testLocation);
            var service = new ClothingRecommendationService(
                mockWeatherForecastService.Object,
                mockClothingRuleRepository.Object);
            var recommendation = await service.GetClothingRecommendation(request);

            // assert
            Assert.AreEqual("Andeby", recommendation.Place.Name);
            Assert.That(recommendation.Rules, Has.Exactly(1).Items);
            var rule = recommendation.Rules.First();

            Assert.AreEqual("T-skjore og shorts", rule.Clothes);
        }
        public async Task TestLocationIsTooFarFromAnyPlaceSoWeShouldNotGetAnyRecommendations()
        {
            // arrange
            var testDate     = new DateTime(2000, 1, 1, 10, 0, 0);
            var testPeriod   = new TimePeriod(testDate, testDate.AddHours(10));
            var testLocation = new Location(59, -10);

            var mockWeatherForecastService = new Mock <IWeatherForecastService>();
            var mockClothingRuleRepository = new Mock <IClothingRuleRepository>();

            mockClothingRuleRepository
            .Setup(crr => crr.GetRulesByUser(It.IsAny <Guid?>()))
            .ReturnsAsync(new[]
            {
                new ClothingRule(-20, 10, null, "Bobledress"),
                new ClothingRule(10, 20, null, "Bukse og genser"),
                new ClothingRule(20, 40, null, "T-skjore og shorts"),
            });


            mockWeatherForecastService
            .Setup(fs => fs.GetAllPlaces())
            .Returns(new[] { new Place("", "", "", "Larvik", new Location(59, 0)), });

            mockWeatherForecastService
            .Setup(fs => fs.GetWeatherForecast(It.IsAny <Place>()))
            .ReturnsAsync(new WeatherForecast(new[] {
                new TemperatureForecast(25, testDate.AddHours(2), testDate.AddHours(4)),
            }));

            // act
            var request = new ClothingRecommendationRequest(testPeriod, testLocation);
            var service = new ClothingRecommendationService(
                mockWeatherForecastService.Object,
                mockClothingRuleRepository.Object);
            var recommendation = await service.GetClothingRecommendation(request);

            // assert
            Assert.That(recommendation.Rules, Has.Exactly(0).Items);
        }