public void CsvHillRepository_GetHills_MultipleFilters()
        {
            var search = new HillSearch
            {
                Category      = "MUN",
                MinHeight     = 500,
                MaxHeight     = 1000,
                Take          = 10,
                SortBy        = "Height",
                SortDirection = "Desc"
            };

            //get all hills with search criteria
            var results = csvHillRespoitory.GetHills(search);

            //expecting first result to be greater in height than last result
            Assert.IsTrue(results.First().Height > results.Last().Height);

            //heights over 500
            Assert.IsTrue(results.All(x => x.Height >= 500));

            //heights under 1000
            Assert.IsTrue(results.All(x => x.Height <= 1000));

            //only munroes
            Assert.IsTrue(results.All(x => x.Category == "MUN"));

            //take 10 only
            Assert.AreEqual(10, results.Count());
        }
示例#2
0
        public void HillSearch_Check_Category_TopCategory()
        {
            var hillSearch = new HillSearch
            {
                Category = "Top"
            };

            var result = hillSearch.Check();

            Assert.AreEqual("", result);
        }
示例#3
0
        public void HillSearch_Check_Sort_Height()
        {
            var hillSearch = new HillSearch
            {
                SortBy = "Height"
            };

            var result = hillSearch.Check();

            Assert.AreEqual("", result);
        }
示例#4
0
        public void HillSearch_Check_Sort_Invalid()
        {
            var hillSearch = new HillSearch
            {
                SortBy = "Mun"
            };

            var result = hillSearch.Check();

            Assert.AreEqual("Sort by can be either NAME or HEIGHT", result);
        }
示例#5
0
        public void HillSearch_Check_Category_InvalidCategory()
        {
            var hillSearch = new HillSearch
            {
                Category = "asdfs"
            };

            var result = hillSearch.Check();

            Assert.AreEqual("Category can be either MUN, TOP or left blank for both", result);
        }
示例#6
0
        public void HillSearch_Check_SortDirection_Descending()
        {
            var hillSearch = new HillSearch
            {
                SortDirection = "desc"
            };

            var result = hillSearch.Check();

            Assert.AreEqual("", result);
        }
示例#7
0
        public void HillSearch_Check_SortDirection_Invalid()
        {
            var hillSearch = new HillSearch
            {
                SortDirection = "Height"
            };

            var result = hillSearch.Check();

            Assert.AreEqual("Sort direction can be either ASC or DESC. Defaults to ASC if left blank.", result);
        }
示例#8
0
        public void HillSearch_Check_MaxHeightLessThanMinHeight()
        {
            var hillSearch = new HillSearch
            {
                MaxHeight = 500,
                MinHeight = 1000
            };

            var result = hillSearch.Check();

            Assert.AreEqual("Max height must be greater than min height", result);
        }
        public void CsvHillRepository_GetHills_CategoryFilter_Mun()
        {
            var search = new HillSearch
            {
                Category = "MUN"
            };

            //get all hills with search criteria
            var results = csvHillRespoitory.GetHills(search);

            //expecting only results with MUN category
            Assert.IsTrue(results.All(x => x.Category == "MUN"));
        }
示例#10
0
        public void CsvHillRepository_GetHills_MaxHeightFilter()
        {
            var search = new HillSearch
            {
                MaxHeight = 1000
            };

            //get all hills with search criteria
            var results = csvHillRespoitory.GetHills(search);

            //expecting only results with heights under 1000
            Assert.IsTrue(results.All(x => x.Height <= 1000));
        }
示例#11
0
        public void CsvHillRepository_GetHills_SortByHeight_Descending()
        {
            var search = new HillSearch
            {
                SortBy        = "Height",
                SortDirection = "Desc"
            };

            //get all hills with search criteria
            var results = csvHillRespoitory.GetHills(search);

            //expecting first result to be greater in height than last result
            Assert.IsTrue(results.First().Height > results.Last().Height);
        }
示例#12
0
        public void CsvHillRepository_GetHills_SortByName_Descending()
        {
            var search = new HillSearch
            {
                SortBy        = "Name",
                SortDirection = "Desc"
            };

            //get all hills with search criteria
            var results = csvHillRespoitory.GetHills(search);

            //expecting last result to start with an a
            Assert.IsTrue(results.Last().Name.StartsWith("A"));
        }
示例#13
0
        public void CsvHillRepository_GetHills_Take_AscendingByName()
        {
            var search = new HillSearch
            {
                Take          = 10,
                SortBy        = "Name",
                SortDirection = "Asc"
            };

            //get all hills with search criteria
            var results = csvHillRespoitory.GetHills(search);

            //expecting first result to start with an a
            Assert.IsTrue(results.First().Name.StartsWith("A"));
            Assert.AreEqual(10, results.Count());
        }
示例#14
0
        public void HillSearch_Check_BlankFilters_NoErrors()
        {
            var hillSearch = new HillSearch
            {
                Category      = "",
                MaxHeight     = 0,
                MinHeight     = 0,
                Take          = 0,
                SortBy        = "",
                SortDirection = ""
            };

            var result = hillSearch.Check();

            Assert.AreEqual("", result);
        }
示例#15
0
        public IActionResult Get(string category, int?minHeight, int?maxHeight, int?take, string sortBy, string sortDirection)
        {
            var hillSearch = new HillSearch
            {
                Category      = category,
                MinHeight     = minHeight ?? 0,
                MaxHeight     = maxHeight ?? 0,
                Take          = take ?? 0,
                SortBy        = sortBy,
                SortDirection = sortDirection
            };

            //do some sanity checks on the passed filters
            var error = hillSearch.Check();

            if (!string.IsNullOrWhiteSpace(error))
            {
                return(BadRequest(error));
            }

            return(Ok(HillRepository.GetHills(hillSearch)));
        }
示例#16
0
        /// <summary>
        /// Get hills matching search criteria
        /// </summary>
        /// <param name="search"></param>
        /// <returns></returns>
        public IEnumerable <Hill> GetHills(HillSearch search)
        {
            if (string.IsNullOrWhiteSpace(search.SortDirection) || search.SortDirection.ToUpper() != "DESC")
            {
                //default sort ascending
                search.SortDirection = "ASC";
            }

            //should never return hills that are uncategorised
            var hills = _hills.Where(x => x.Post1997 != "");

            if (!string.IsNullOrWhiteSpace(search.Category))
            {
                hills = hills.Where(x => x.Post1997.ToUpper() == search.Category.ToUpper()).ToList();
            }

            if (search.MinHeight > 0)
            {
                hills = hills.Where(x => x.HeightM >= search.MinHeight).ToList();
            }

            if (search.MaxHeight > 0)
            {
                hills = hills.Where(x => x.HeightM <= search.MaxHeight).ToList();
            }

            //sorting by name or height(m)
            if (!string.IsNullOrWhiteSpace(search.SortBy))
            {
                hills = hills.ToList();

                switch (search.SortBy.ToUpper())
                {
                case "NAME":
                    hills = hills.ToList().OrderBy(x => x.Name);
                    break;

                case "HEIGHT":
                    hills = hills.ToList().OrderBy(x => x.HeightM);
                    break;
                }
            }

            //sort direction
            if (search.SortDirection.ToUpper() == "DESC")
            {
                hills = hills.Reverse();
            }

            //take specifies a limit to the results
            if (search.Take > 0)
            {
                hills = hills.Take(search.Take);
            }

            var results = new List <Hill>();

            foreach (var hill in hills)
            {
                //could use something like automapper here
                results.Add(new Hill
                {
                    Name          = hill.Name,
                    Height        = hill.HeightM,
                    Category      = hill.Post1997,
                    GridReference = hill.GridRef,
                });
            }

            return(results);
        }