public void ReturnsProperlyFilteredCategories(string searchText, UsersSortingCriterion criterion, bool descending, int pageIndex, int pageSize) { var model = new UsersFilteringModel(searchText, criterion, descending, pageIndex, pageSize); var users = AddUsers(31); var categories = AddCategories(); var flashards = AddFlashcards(categories[0], 50); var userProgress = AddUserProgress(flashards, users.GetRange(0, 20)); var query = Context.Users.AsQueryable(); var filteredUserDtos = FilteringServices.Filter(query, model).ToList(); var expected = users.Where(u => u.Name.Contains(model.SearchText) || u.Surname.Contains(model.SearchText) || u.UserName.Contains(model.SearchText)); var lambda = GetLambda(criterion); expected = descending ? expected.OrderByDescending(lambda) : expected.OrderBy(lambda); var expectedList = expected.Skip(model.PageIndex * model.PageSize).Take(model.PageSize) .Select(u => new UserDto(u.Name, u.Surname, u.UserName, new Score(userProgress.Where(up => up.UserId == u.Id).Sum(progress => progress.Progress)))) .ToList(); Assert.Equal(expectedList.Count, filteredUserDtos.Count); for (var i = 0; i < expectedList.Count; i++) { Assert.True(expectedList[i].UserName.Equals(filteredUserDtos[i].UserName)); } }
public void InvalidFilteringModel(bool descending, UsersSortingCriterion criterion) { var model = new UsersFilteringModel("", criterion, descending, 1, 1); var query = Context.Users.AsQueryable(); Assert.Throws <ArgumentOutOfRangeException>(() => FilteringServices.Filter(query, model)); }
private Func <User, object> GetLambda(UsersSortingCriterion criterion) { switch (criterion) { case UsersSortingCriterion.None: return(u => u.UserName); case UsersSortingCriterion.Name: return(u => u.Name); case UsersSortingCriterion.Surname: return(u => u.Surname); case UsersSortingCriterion.UserName: return(u => u.UserName); case UsersSortingCriterion.Points: return(u => u.UserProgress.Sum(up => up.Progress)); default: throw new ArgumentOutOfRangeException(); } }
public UsersFilteringModel(string searchText, UsersSortingCriterion sortingCriterion, bool descending, int pageIndex, int pageSize) : base(searchText, descending, pageIndex, pageSize) { SortingCriterion = sortingCriterion; }