Example #1
0
        public List<Diamond> DiamondsBySearchParameters(DiamondSearchParameters mappedSearchParameters)
        {
            if (mappedSearchParameters.PriceFrom > mappedSearchParameters.PriceTo && mappedSearchParameters.PriceTo != 0)
            {
                var temp = mappedSearchParameters.PriceFrom;
                mappedSearchParameters.PriceFrom = mappedSearchParameters.PriceTo;
                mappedSearchParameters.PriceTo = temp;
            }

            if (mappedSearchParameters.WeightFrom > mappedSearchParameters.WeightTo && mappedSearchParameters.WeightTo != 0)
            {
                decimal temp2 = mappedSearchParameters.WeightFrom;
                mappedSearchParameters.WeightFrom = mappedSearchParameters.WeightTo;
                mappedSearchParameters.WeightTo = temp2;
            }

            using (var db = new JONEntities())
            {
                var dbdiamonds = db.v_jd_diamonds
                .ExtWhereIn(cond => cond.color, mappedSearchParameters.Color)
                .ExtWhereIn(cond => cond.clarity, mappedSearchParameters.Clarity)
                .ExtWhereIn(cond => cond.shape, mappedSearchParameters.Shape)
                .ExtWhereIn(cond => cond.report, mappedSearchParameters.Report)
                .ExtWhereIn(cond => cond.cut, mappedSearchParameters.Cut)
                .ExtWhereFromToRangeAndIgnoreZero(x => x.totalprice,mappedSearchParameters.PriceFrom,mappedSearchParameters.PriceTo)
                .ExtWhereFromToRangeAndIgnoreZero(x => x.weight, mappedSearchParameters.WeightFrom, mappedSearchParameters.WeightTo)
                ;

                if (mappedSearchParameters.ItemsPerPage == 0)
                {
                    mappedSearchParameters.ItemsPerPage = 10;
                }

                totalRecords = dbdiamonds.Count();
                if (TotalRecords % mappedSearchParameters.ItemsPerPage == 0)
                {
                    lastOporationTotalPages = TotalRecords / mappedSearchParameters.ItemsPerPage;
                }
                else
                {
                    lastOporationTotalPages = TotalRecords / mappedSearchParameters.ItemsPerPage + 1;
                }

                if (mappedSearchParameters.OrderBy != null)
                {
                    dbdiamonds = dbdiamonds.OrderBy(mappedSearchParameters.OrderBy.SQLString);
                }

            var dbdiamondslist = dbdiamonds.Skip((mappedSearchParameters.Page - 1) * mappedSearchParameters.ItemsPerPage).Take(mappedSearchParameters.ItemsPerPage).ToList();

            var diamonds = mapper.Map<IList<v_jd_diamonds>, List<Diamond>>(dbdiamondslist);

            return diamonds;

            }
        }
        public void DiamondsBySearchParameters_ShouldFilterResultsByColorButMoreThenOneColorSelected()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 1;
            searchParameters.ItemsPerPage = 10;
            searchParameters.Color = new List<string>() { "H","G" };

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(4);
        }
        public List<Diamond> DiamondsBySearchParameters(DiamondSearchParameters mappedSearchParameters)
        {
            if (mappedSearchParameters.PriceFrom > mappedSearchParameters.PriceTo && mappedSearchParameters.PriceTo != 0)
            {
                var temp = mappedSearchParameters.PriceFrom;
                mappedSearchParameters.PriceFrom = mappedSearchParameters.PriceTo;
                mappedSearchParameters.PriceTo = temp;
            }

            if (mappedSearchParameters.WeightFrom > mappedSearchParameters.WeightTo && mappedSearchParameters.WeightTo != 0)
            {
                decimal temp2 = mappedSearchParameters.WeightFrom;
                mappedSearchParameters.WeightFrom = mappedSearchParameters.WeightTo;
                mappedSearchParameters.WeightTo = temp2;
            }

            var dbdiamonds = dbmock
                .Where(cond => MakeEmptyListActLikeAnAllFilter(cond.color, mappedSearchParameters.Color))
                .Where(cond => MakeEmptyListActLikeAnAllFilter(cond.clarity, mappedSearchParameters.Clarity))
                .Where(cond => MakeEmptyListActLikeAnAllFilter(cond.shape, mappedSearchParameters.Shape))
                .Where(cond => MakeEmptyListActLikeAnAllFilter(cond.report, mappedSearchParameters.Report))
                .Where(cond => MakeEmptyListActLikeAnAllFilter(cond.cut, mappedSearchParameters.Cut))
                .Where(x => MakeZeroActAsIgnoreParameter(p => p.totalprice >= mappedSearchParameters.PriceFrom, x, mappedSearchParameters.PriceFrom) && MakeZeroActAsIgnoreParameter(p => p.totalprice <= mappedSearchParameters.PriceTo, x, mappedSearchParameters.PriceTo))
                .Where(x => MakeZeroActAsIgnoreParameter(p => p.weight >= mappedSearchParameters.WeightFrom, x, mappedSearchParameters.WeightFrom) && MakeZeroActAsIgnoreParameter(p => p.weight <= mappedSearchParameters.WeightTo, x, mappedSearchParameters.WeightTo))
                ;

            if (mappedSearchParameters.ItemsPerPage == 0)
            {
                mappedSearchParameters.ItemsPerPage = 10;
            }

            totalRecords = dbdiamonds.Count();
            if (TotalRecords % mappedSearchParameters.ItemsPerPage == 0)
            {
                lastOporationTotalPages = TotalRecords/mappedSearchParameters.ItemsPerPage;
            } else
            {
                lastOporationTotalPages = TotalRecords/mappedSearchParameters.ItemsPerPage + 1;
            }

            if (mappedSearchParameters.OrderBy != null)
            {
                dbdiamonds = dbdiamonds.OrderBy(mappedSearchParameters.OrderBy.SQLString);
            }

            var dbdiamondslist  = dbdiamonds.Skip((mappedSearchParameters.Page-1)*mappedSearchParameters.ItemsPerPage).Take(mappedSearchParameters.ItemsPerPage).ToList();

            var diamonds = mapper.Map<IList<v_jd_diamonds>, List<Diamond>>(dbdiamondslist);

            return diamonds;
        }
        public void TotalPagesNumber_ShouldSortByWeightInDescOrderWhenCalledAsPartOFTheSearchParams()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.OrderBy = new DynamicOrderBy("totalprice", "desc");

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);

            //Assert
            var unsortedCollectio = GetDiamondCollectionUnSorted();
            diamonds.Should().ContainInOrder(unsortedCollectio.OrderByDescending(x => x.Price).ToList());
        }
        private List<Diamond> GetDiamondCollectionUnSorted()
        {
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 1;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            return diamondRepository.DiamondsBySearchParameters(searchParameters);
        }
        public void DiamondsBySearchParameters_ShouldReturnZeroItemsIfThePageRequestedDoesntExists()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 3;
            searchParameters.ItemsPerPage = 3;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(0);
        }
        public void TotalPagesNumber_ShouldReturnTheTotalNumberOfPagesCorrectly()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 1;
            searchParameters.ItemsPerPage = 2;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            diamondRepository.DiamondsBySearchParameters(searchParameters);
            var totalPages = diamondRepository.LastOporationTotalPages;
            //Assert
            totalPages.Should().Be(3);
        }
        public void DiamondsBySearchParameters_ShouldReturnAllTheItemsInTheRepostoryWhenNoFiltersAreAskedForAndWeWantTheFirstPage()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 1;
            searchParameters.ItemsPerPage = 10;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(THE_TOTAL_NUMBER_OF_ITEMS);
        }
        public void DiamondsBySearchParameters_ShouldReturnOnly3ItemsOnPage2AsRequestedBySearchParams()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 2;
            searchParameters.ItemsPerPage = 3;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(3);
        }
        public void DiamondsBySearchParameters_ShouldReturn1ItemOnPage2WhenPageSizeIs5()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 2;
            searchParameters.ItemsPerPage = 5;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(1);
        }
        public void DiamondsBySearchParameters_ShouldFilterResultsByWeightMarginsWhenOnlyToWeightIsSpecified()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 1;
            searchParameters.ItemsPerPage = 10;
            searchParameters.WeightFrom = 0;
            searchParameters.WeightTo = (decimal) 1.35;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(3);
        }
        public void DiamondsBySearchParameters_ShouldFilterResultsByWeightMarginsWhenBothAreSpecifiedEvenIfTheFromFieldIBiggerThenTheToField()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 1;
            searchParameters.ItemsPerPage = 10;
            searchParameters.WeightFrom = (decimal) 1.35;
            searchParameters.WeightTo = 1;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(3);
        }
        public void DiamondsBySearchParameters_ShouldFilterResultsByShapeWhenOneShapeSelected()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 1;
            searchParameters.ItemsPerPage = 10;
            searchParameters.Shape = new List<string>() { "Round" };

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(2);
        }
        public void DiamondsBySearchParameters_ShouldFilterResultsByPriceMarginsWhenOnlyToPriceIsSpecified()
        {
            //Arrange
            var searchParameters = new DiamondSearchParameters();
            searchParameters.Page = 1;
            searchParameters.ItemsPerPage = 10;
            searchParameters.PriceFrom = 0;
            searchParameters.PriceTo = 26000;

            var diamondRepository = new FakeDiamondRepository(mapper);
            //Act
            var diamonds = diamondRepository.DiamondsBySearchParameters(searchParameters);
            //Assert
            diamonds.Should().HaveCount(3);
        }