public async Task Should_Filter_Works_Properly( Mock <IMemoryDatabaseProvider <TestDbContext> > mockProvider, List <Book> source) { // Arrange string searchText = "a"; var queryable = source.AsQueryable(); var repository = new MockingMemoryRepository <TestDbContext, Book, Guid>(mockProvider.Object, queryable); var sut = CreateAppService(repository); var filter = new BookFilterDto { Filter = searchText }; // Act var actual = await sut.GetListAsync(filter); var expectedItems = queryable.ApplyFilter(filter).ToList(); var expectedCount = filter.ApplyFilterWithoutPaginationAndOrdering(queryable).Count(); // Assert actual.Items.ShouldNotBeNull(); actual.TotalCount.ShouldBe(expectedCount); actual.Items.Count.ShouldBe(expectedItems.Count); }
public async Task Should_Sorting_Work_Properly( Mock <IMemoryDatabaseProvider <TestDbContext> > mockProvider, List <Book> source) { // Arrange string sorting = "Title DESC"; var query = source.AsQueryable(); var repository = new MockingMemoryRepository <TestDbContext, Book, Guid>(mockProvider.Object, query); var sut = CreateAppService(repository); var filter = new BookFilterDto { Sorting = sorting }; // Act var actual = await sut.GetListAsync(filter); var expected = query .OrderByDescending(x => x.Title) .Skip(filter.SkipCount) .Take(filter.MaxResultCount) .ToList(); actual.ShouldNotBeNull(); actual.Items.ShouldNotBeNull(); actual.Items.Count.ShouldBe(expected.Count); for (int i = 0; i < actual.Items.Count; i++) { actual.Items[i].Title.ShouldBe(expected[i].Title); // Id cannot be mocked. All ids are same. } }
public async Task Should_Filter_Work_Properly( Mock <IMemoryDatabaseProvider <TestDbContext> > mockProvider, List <Book> source) { // Arrange string sorting = "Title DESC"; string searchText = "ab"; var query = source.AsQueryable(); var repository = new MockingMemoryRepository <TestDbContext, Book, Guid>(mockProvider.Object, query); var sut = CreateAppService(repository); var filter = new BookFilterDto { Sorting = sorting, Filter = searchText }; // Act var actual = await sut.GetListAsync(filter); var expected = query // ToLowerContainsComparison attribute should generate exactly following expression: .Where(x => x.Title.ToLowerInvariant().Contains(searchText.ToLowerInvariant())) .OrderByDescending(x => x.Title) .Skip(filter.SkipCount) .Take(filter.MaxResultCount) .ToList(); actual.ShouldNotBeNull(); actual.Items.ShouldNotBeNull(); actual.Items.Count.ShouldBe(expected.Count); for (int i = 0; i < actual.Items.Count; i++) { actual.Items[i].Title.ShouldBe(expected[i].Title); // Id cannot be mocked. All ids are same. } }
public async Task Should_RangeFilter_Work_Properly( Mock <IMemoryDatabaseProvider <TestDbContext> > mockProvider, List <Book> source) { // Arrange string sorting = "Title DESC"; var query = source.AsQueryable(); var repository = new MockingMemoryRepository <TestDbContext, Book, Guid>(mockProvider.Object, query); var sut = CreateAppService(repository); var filter = new BookFilterDto { Sorting = sorting, PublishDate = new Range <DateTime>() { Min = DateTime.UtcNow } }; // Act var actual = await sut.GetListAsync(filter); var expected = query // TRange type should generate exactly following expression: .Where(x => x.PublishDate >= filter.PublishDate.Min) .OrderByDescending(x => x.Title) .Skip(filter.SkipCount) .Take(filter.MaxResultCount) .ToList(); actual.ShouldNotBeNull(); actual.Items.ShouldNotBeNull(); actual.Items.Count.ShouldBe(expected.Count); for (int i = 0; i < actual.Items.Count; i++) { actual.Items[i].Title.ShouldBe(expected[i].Title); // Id cannot be mocked. All ids are same. } }
public async Task Should_Pagination_Works_Properly( Mock <IMemoryDatabaseProvider <TestDbContext> > mockProvider, List <Book> source) { // Arrange int skip = 3, take = 2; var repository = new MockingMemoryRepository <TestDbContext, Book, Guid>(mockProvider.Object, source.AsQueryable()); var sut = CreateAppService(repository); var filter = new BookFilterDto { SkipCount = skip, MaxResultCount = take }; // Act var actual = await sut.GetListAsync(filter); var expectedItems = source.AsQueryable().ApplyFilter(filter).ToList(); var expectedCount = source.Count; // Assert actual.Items.ShouldNotBeNull(); actual.TotalCount.ShouldBe(expectedCount); actual.Items.Count.ShouldBe(expectedItems.Count); }