public async Task When_GettingBucketsOrderedBySizeWithLimtAndOffset_Expect_OrderedLimitedAndOffsetBuckets()
            {
                // Arrange
                foreach (Bucket bucket in this.testBuckets)
                {
                    await this.repository.AddAsync(bucket);
                }

                Ordering <Bucket> ordering = new Ordering <Bucket> {
                    new OrderBy("Size", true)
                };

                GetBuckets.Query query = new GetBuckets.Query(
                    predicate: bucket => true,
                    limit: 6,
                    offset: 3,
                    ordering: ordering);

                GetBuckets.Handler handler = new GetBuckets.Handler(this.repository);

                // Act
                GetBuckets.Response response = await handler.Handle(query, default);

                // Assert
                for (int i = 1; i <= 6; i++)
                {
                    Assert.Equal(i + 3, response.Buckets[i - 1].Size);
                }
            }
            public async Task When_GettingBucketsOrderedBySizeDescending_Expect_OrderedBySizeDescending()
            {
                // Arrange
                foreach (Bucket bucket in this.testBuckets)
                {
                    await this.repository.AddAsync(bucket);
                }

                Ordering <Bucket> ordering = new Ordering <Bucket> {
                    new OrderBy("Size", false)
                };

                GetBuckets.Query query = new GetBuckets.Query(
                    predicate: bucket => true,
                    limit: null,
                    offset: null,
                    ordering: ordering);

                GetBuckets.Handler handler = new GetBuckets.Handler(this.repository);

                // Act
                GetBuckets.Response response = await handler.Handle(query, default);

                // Assert
                for (int i = 1; i <= 12; i++)
                {
                    Assert.Equal(12 - (i - 1), response.Buckets[i - 1].Size);
                }
            }
            public async Task When_GettingBucketsWithLimitAndOffset_Expect_LimitedAndOffsetBuckets()
            {
                // Arrange
                await this.repository.AddAsync(new Bucket { Name = "Bucket 1" });

                await this.repository.AddAsync(new Bucket { Name = "Bucket 2" });

                await this.repository.AddAsync(new Bucket { Name = "Bucket 3" });

                GetBuckets.Query query = new GetBuckets.Query(
                    predicate: bucket => true,
                    limit: 1,
                    offset: 1,
                    ordering: null);

                GetBuckets.Handler handler = new GetBuckets.Handler(this.repository);

                // Act
                GetBuckets.Response response = await handler.Handle(query, default);

                // Assert
                Assert.NotNull(response.Buckets);
                Assert.NotEmpty(response.Buckets);
                Assert.Equal(1, response.Buckets.Count);
                Assert.Equal("Bucket 2", response.Buckets[0].Name);
            }
        public async Task <IList <Bucket> > GetBuckets([FromQuery] int?limit, [FromQuery] int?offset)
        {
            GetBuckets.Query query = new GetBuckets.Query(
                predicate: bucket => true,
                limit: limit,
                offset: offset,
                ordering: null);

            GetBuckets.Response response = await this.mediator.Send(query);

            return(this.mapper.Map <List <Bucket> >(response.Buckets));
        }
            public async Task When_GettingBucketsOrderedByNameDescendingThenByDescriptionAscending_Expect_OrderedByNameDescendingThenByDescriptionAscending()
            {
                // Arrange
                foreach (Bucket bucket in this.testBuckets)
                {
                    await this.repository.AddAsync(bucket);
                }

                Ordering <Bucket> ordering = new Ordering <Bucket>
                {
                    new OrderBy("Name", false),
                    new OrderBy("Description", true),
                };

                GetBuckets.Query query = new GetBuckets.Query(
                    predicate: bucket => true,
                    limit: null,
                    offset: null,
                    ordering: ordering);

                GetBuckets.Handler handler = new GetBuckets.Handler(this.repository);

                // Act
                GetBuckets.Response response = await handler.Handle(query, default);

                // Assert
                Assert.Equal("B", response.Buckets[0].Name);
                Assert.Equal("B", response.Buckets[1].Name);
                Assert.Equal("B", response.Buckets[2].Name);
                Assert.Equal("B", response.Buckets[3].Name);
                Assert.Equal("B", response.Buckets[4].Name);
                Assert.Equal("B", response.Buckets[5].Name);
                Assert.Equal("A", response.Buckets[6].Name);
                Assert.Equal("A", response.Buckets[7].Name);
                Assert.Equal("A", response.Buckets[8].Name);
                Assert.Equal("A", response.Buckets[9].Name);
                Assert.Equal("A", response.Buckets[10].Name);
                Assert.Equal("A", response.Buckets[11].Name);

                Assert.Equal("1", response.Buckets[0].Description);
                Assert.Equal("1", response.Buckets[1].Description);
                Assert.Equal("2", response.Buckets[2].Description);
                Assert.Equal("2", response.Buckets[3].Description);
                Assert.Equal("3", response.Buckets[4].Description);
                Assert.Equal("3", response.Buckets[5].Description);
                Assert.Equal("1", response.Buckets[6].Description);
                Assert.Equal("1", response.Buckets[7].Description);
                Assert.Equal("2", response.Buckets[8].Description);
                Assert.Equal("2", response.Buckets[9].Description);
                Assert.Equal("3", response.Buckets[10].Description);
                Assert.Equal("3", response.Buckets[11].Description);
            }
            public async Task When_GettingNonExistingBuckets_Expect_EmptyList()
            {
                // Arrange
                GetBuckets.Query   query   = new GetBuckets.Query();
                GetBuckets.Handler handler = new GetBuckets.Handler(this.repository);

                // Act
                GetBuckets.Response response = await handler.Handle(query, default);

                // Assert
                Assert.NotNull(response.Buckets);
                Assert.Empty(response.Buckets);
            }
            public void When_InstantiatingQuery_Expect_PredicateNotNull()
            {
                // Arrange
                GetBuckets.Query query;

                // Act
                query = new GetBuckets.Query();

                // Assert
                Assert.NotNull(query.Predicate);
                Assert.Null(query.Limit);
                Assert.Null(query.Offset);
                Assert.Null(query.Ordering);
            }
            public void When_InstantiatingQueryWithPredicate_Expect_Predicate()
            {
                // Arrange
                Expression <Func <Bucket, bool> > predicate = bucket => false;

                GetBuckets.Query query;

                // Act
                query = new GetBuckets.Query(predicate);

                // Assert
                Assert.Equal(predicate, query.Predicate);
                Assert.Null(query.Limit);
                Assert.Null(query.Offset);
                Assert.Null(query.Ordering);
            }
            public async Task When_GettingBuckets_Expect_AllBuckets()
            {
                // Arrange
                await this.repository.AddAsync(new Bucket());

                await this.repository.AddAsync(new Bucket());

                await this.repository.AddAsync(new Bucket());

                GetBuckets.Query   query   = new GetBuckets.Query();
                GetBuckets.Handler handler = new GetBuckets.Handler(this.repository);

                // Act
                GetBuckets.Response response = await handler.Handle(query, default);

                // Assert
                Assert.NotNull(response.Buckets);
                Assert.NotEmpty(response.Buckets);
                Assert.Equal(3, response.Buckets.Count);
            }
            public async Task When_GettingBucketsWithPredicate_Expect_SomeBuckets()
            {
                // Arrange
                await this.repository.AddAsync(new Bucket { Size = 1 });

                await this.repository.AddAsync(new Bucket { Size = 2 });

                await this.repository.AddAsync(new Bucket { Size = 3 });

                await this.repository.AddAsync(new Bucket { Size = 5 });

                await this.repository.AddAsync(new Bucket { Size = 8 });

                GetBuckets.Query   query   = new GetBuckets.Query(bucket => bucket.Size == 2 || bucket.Size == 5);
                GetBuckets.Handler handler = new GetBuckets.Handler(this.repository);

                // Act
                GetBuckets.Response response = await handler.Handle(query, default);

                // Assert
                Assert.NotNull(response.Buckets);
                Assert.NotEmpty(response.Buckets);
                Assert.Equal(2, response.Buckets.Count);
            }