Ejemplo n.º 1
0
        public async void BuildSearchModelAsync_Returns_Model_With_User_PostalCode_When_No_PostalCodeCriteria_Given()
        {
            var postalCode = "12345";
            var distance   = 100;

            var users = new List <User>
            {
                User.Create(
                    _userValidator.Object,
                    _userPasswordService.Object,
                    "*****@*****.**",
                    "DisplayName",
                    "Password",
                    postalCode,
                    1)
            }.AsQueryable();

            var user = users.First();

            _userRepository
            .Setup(r => r.GetAsync(It.IsAny <Expression <Func <User, bool> > >()))
            .Returns((Expression <Func <User, bool> > predicate) => Task.FromResult(users.Where(predicate).ToList()));

            var model = new StudioSearchModel();

            var result = await _studioService.BuildSearchModelAsync(user.Id, model);

            Assert.NotNull(result);
            Assert.NotNull(result.PostalCodeSearchCriteria);
            Assert.Equal(user.PostalCode, model.PostalCodeSearchCriteria.FromPostalCode);
            Assert.Equal(distance, model.PostalCodeSearchCriteria.Distance);
        }
Ejemplo n.º 2
0
        public async Task <List <StudioSearchResultDto> > SearchAsync(StudioSearchModel searchModel)
        {
            ArgumentNotNullValidator.Validate(searchModel, nameof(searchModel));

            var postalCodes = await GetPostalCodes(searchModel);

            var studios = await _studioRepository.GetAsync();

            var studiosWithPostalCodes = studios
                                         .Where(
                s =>
                postalCodes.Any(p => p.Code == s.PostalCode));

            var result = studiosWithPostalCodes.Select(s =>
                                                       new StudioSearchResultDto
            {
                Id          = s.Id,
                Name        = s.Name,
                FriendlyUrl = s.FriendlyUrl,
                OwnerUserId = s.OwnerUserId,
                PostalCode  = s.PostalCode,
                Distance    = (double)postalCodes.First(p => p.Code == s.PostalCode).Distance
            })
                         .OrderBy(s => s.Distance)
                         .ToList();

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Search([FromBody] StudioSearchModel model)
        {
            model = await _studioService.BuildSearchModelAsync(UserId, model);

            var results = await _studioService.SearchAsync(model);

            return(Ok(results));
        }
Ejemplo n.º 4
0
        public async void BuildSearchModelAsync_Throws_ArgumentException_When_Anonymous_And_No_PostalCode_Criteria_Given()
        {
            var model = new StudioSearchModel();

            var ex = await Assert.ThrowsAsync <DataValidationException>(() => _studioService.BuildSearchModelAsync(null, model));

            Assert.NotNull(ex);
            Assert.NotNull(ex.InnerException);
            Assert.IsType <ArgumentException>(ex.InnerException);
        }
Ejemplo n.º 5
0
        public async Task <StudioSearchModel> BuildSearchModelAsync(Guid?userId, StudioSearchModel model)
        {
            if (model == null)
            {
                model = new StudioSearchModel();
            }

            var isAnonymous                  = !userId.HasValue;
            var hasPostalCodeCriteria        = model.PostalCodeSearchCriteria != null;
            var isPostalCodeCriteriaRequired = isAnonymous && !hasPostalCodeCriteria;

            if (isPostalCodeCriteriaRequired)
            {
                throw new DataValidationException(new ArgumentException("Postal Code and Distance are required"));
            }

            var postalCode = string.Empty;
            var distance   = 0;

            if (userId.HasValue && !hasPostalCodeCriteria)
            {
                var user =
                    (await _userRepository.GetAsync(
                         u =>
                         u.Id == userId.Value))
                    .SingleOrDefault();

                if (user == null)
                {
                    throw new ArgumentException("User Id is invalid");
                }

                postalCode = user.PostalCode;
                distance   = DefaultDistance;
            }

            if (!hasPostalCodeCriteria)
            {
                model.PostalCodeSearchCriteria = new PostalCodeSearchCriteria(postalCode, distance);
            }

            if (string.IsNullOrWhiteSpace(model.PostalCodeSearchCriteria.FromPostalCode))
            {
                throw new DataValidationException(new ArgumentException("Postal Code is required"));
            }

            if (model.PostalCodeSearchCriteria.Distance < 0)
            {
                throw new DataValidationException(new ArgumentOutOfRangeException("Distance must be greater than or equal to 0 (zero)"));
            }

            return(model);
        }
Ejemplo n.º 6
0
        public async void BuildSearchModelAsync_Throws_ArgumentException_When_Anonymous_And_Distance_Invalid()
        {
            var model = new StudioSearchModel();

            model.PostalCodeSearchCriteria = new PostalCodeSearchCriteria("11111", -5);

            var ex = await Assert.ThrowsAsync <DataValidationException>(() => _studioService.BuildSearchModelAsync(null, model));

            Assert.NotNull(ex);
            Assert.NotNull(ex.InnerException);
            Assert.IsType <ArgumentOutOfRangeException>(ex.InnerException);
        }
Ejemplo n.º 7
0
        private async Task <List <PostalCodeDistance> > GetPostalCodes(
            StudioSearchModel searchModel)
        {
            var criteria = searchModel.PostalCodeSearchCriteria;

            ArgumentNotNullValidator.Validate(criteria, nameof(criteria));

            var postalCodesWithinDistance =
                (await _postalCodeRepository.GetPostalCodesWithinDistance(
                     criteria.FromPostalCode,
                     criteria.Distance)).ToList();

            return(postalCodesWithinDistance);
        }
Ejemplo n.º 8
0
        public async void BuildSearchModelAsync_Returns_Unchanged_Model_For_Authenticated_User()
        {
            var postalCode = "12345";
            var distance   = 50;

            var model = new StudioSearchModel();

            model.PostalCodeSearchCriteria = new PostalCodeSearchCriteria(postalCode, distance);

            var result = await _studioService.BuildSearchModelAsync(Guid.NewGuid(), model);

            Assert.NotNull(result);
            Assert.NotNull(result.PostalCodeSearchCriteria);
            Assert.Equal(postalCode, result.PostalCodeSearchCriteria.FromPostalCode);
            Assert.Equal(distance, result.PostalCodeSearchCriteria.Distance);
        }
Ejemplo n.º 9
0
        public void Verify_Search_WithPaging_Should_ReturnAListOfStudiosWithDataMatchingSearchParameters()
        {
            // Arrange
            Mock <IDbSet <Studio> > mockSetStudios;
            var mockContext = StudiosMockingSetup.DoMockingSetupForContext(true, out mockSetStudios);
            var repository  = new StudiosRepository(mockContext.Object);
            var searchModel = new StudioSearchModel {
                Paging = new Paging {
                    Skip = 1, Take = 1
                }
            };
            // Act
            var studios = repository.Search(searchModel /*.Object*/).ToArray();

            // Assert
            Assert.Equal(1, studios.Length);
            Assert.Equal(2, studios[0].Id);
            Assert.Equal("/TEST/NIVEN-LARRY", studios[0].ApiDetailUrl);
            // Stephen King was filtered out because he was Skipped
        }
Ejemplo n.º 10
0
        public async void SearchAsync_Returns_Studio_When_In_Distance()
        {
            var name                 = "StudioName";
            var friendlyUrl          = "FriendlyUrl";
            var postalCode           = "12345";
            var outOfRangePostalCode = "54321";
            var ownerUserId          = Guid.NewGuid();

            var distance = 10;

            var postalCodesInDistance = new List <PostalCodeDistance>
            {
                PostalCodeDistance.Create(postalCode, distance)
            }.AsEnumerable();

            var searchModel = new StudioSearchModel
            {
                PostalCodeSearchCriteria = new PostalCodeSearchCriteria(postalCode, distance)
            };

            var studios = new List <Studio>
            {
                Studio.Create(name, friendlyUrl, postalCode, ownerUserId, _studioValidator.Object),
                Studio.Create(name, friendlyUrl, outOfRangePostalCode, ownerUserId, _studioValidator.Object)
            };

            _studioRepository
            .Setup(r => r.GetAsync(It.IsAny <Expression <Func <Studio, bool> > >()))
            .Returns(Task.FromResult(studios));

            _postalCodeRepository
            .Setup(r => r.GetPostalCodesWithinDistance(It.IsAny <string>(), It.IsAny <decimal>()))
            .Returns(Task.FromResult(postalCodesInDistance));

            var result = await _studioService.SearchAsync(searchModel);

            Assert.NotNull(result);
            Assert.Equal(1, result.Count);
            Assert.Equal(studios.First().Id, result.First().Id);
            Assert.Equal(distance, result.First().Distance);
        }
Ejemplo n.º 11
0
        public void Verify_Search_WithModifiedSince_Should_ReturnAListOfStudiosWithDataMatchingSearchParameters()
        {
            // Arrange
            Mock <IDbSet <Studio> > mockSetStudios;
            var mockContext = StudiosMockingSetup.DoMockingSetupForContext(true, out mockSetStudios, true);
            var repository  = new StudiosRepository(mockContext.Object);
            var createDate  = new System.DateTime(2015, 05, 28, 10, 45, 00).AddDays(1).AddMinutes(-1);
            //var searchModel = new Mock<IStudioSearchModel>();
            //searchModel.Setup(m => m.ModifiedSince).Returns(() => createDate);
            var searchModel = new StudioSearchModel {
                ModifiedSince = createDate
            };
            // Act
            var studios = repository.Search(searchModel /*.Object*/).ToArray();

            // Assert
            Assert.Equal(1, studios.Length);
            Assert.Equal(2, studios[0].Id);
            Assert.Equal("/TEST/NIVEN-LARRY", studios[0].ApiDetailUrl);
            // Stephen King was filtered out because he was created before the modified since date
        }
 public void Verify_Search_WithPaging_Should_ReturnAListOfStudiosWithDataMatchingSearchParameters()
 {
     // Arrange
     Mock<IDbSet<Studio>> mockSetStudios;
     var mockContext = StudiosMockingSetup.DoMockingSetupForContext(true, out mockSetStudios);
     var repository = new StudiosRepository(mockContext.Object);
     var searchModel = new StudioSearchModel { Paging = new Paging { Skip = 1, Take = 1 } };
     // Act
     var studios = repository.Search(searchModel/*.Object*/).ToArray();
     // Assert
     Assert.Equal(1, studios.Length);
     Assert.Equal(2, studios[0].Id);
     Assert.Equal("/TEST/NIVEN-LARRY", studios[0].ApiDetailUrl);
     // Stephen King was filtered out because he was Skipped
 }
 public void Verify_Search_WithModifiedSince_Should_ReturnAListOfStudiosWithDataMatchingSearchParameters()
 {
     // Arrange
     Mock<IDbSet<Studio>> mockSetStudios;
     var mockContext = StudiosMockingSetup.DoMockingSetupForContext(true, out mockSetStudios, true);
     var repository = new StudiosRepository(mockContext.Object);
     var createDate = new System.DateTime(2015, 05, 28, 10, 45, 00).AddDays(1).AddMinutes(-1);
     //var searchModel = new Mock<IStudioSearchModel>();
     //searchModel.Setup(m => m.ModifiedSince).Returns(() => createDate);
     var searchModel = new StudioSearchModel { ModifiedSince = createDate };
     // Act
     var studios = repository.Search(searchModel/*.Object*/).ToArray();
     // Assert
     Assert.Equal(1, studios.Length);
     Assert.Equal(2, studios[0].Id);
     Assert.Equal("/TEST/NIVEN-LARRY", studios[0].ApiDetailUrl);
     // Stephen King was filtered out because he was created before the modified since date
 }