public async Task GetRecordsQueryHandler_Works()
        {
            var repository = new Mock <IRepository>();

            var selectorCaptor = new ExpressionCaptor <Record, RecordDto>();

            repository.Setup(x => x.ListAsync(selectorCaptor.Capture())).ReturnsAsync(new List <RecordDto>());

            var queryHandler = new GetRecordsQueryHandler(repository.Object);

            var result = await queryHandler
                         .Handle(new GetRecordsQuery(), It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            Assert.That(result, Is.InstanceOf <List <RecordDto> >());

            var record = new Record {
                Id = 1, CoffeeId = 1, DoseIn = 1, DoseOut = 1, Rating = 1, Time = 1, CreateDate = DateTime.Today
            };

            var transformedResult = selectorCaptor.Invoke(record);

            transformedResult.Id.Should().Be(1);
            transformedResult.DoseIn.Should().Be(1);
            transformedResult.DoseOut.Should().Be(1);
            transformedResult.Rating.Should().Be(1);
            transformedResult.Time.Should().Be(1);
            transformedResult.CreateDate.Should().Be(DateTime.Today);
        }
        public async Task GetCoffeesQueryHandler_Works()
        {
            var repository = new Mock <IRepository>();

            var selectorCaptor = new ExpressionCaptor <Coffee, CoffeeDto>();

            repository.Setup(x => x.ListAsync(selectorCaptor.Capture())).ReturnsAsync(new List <CoffeeDto>());

            var queryHandler = new GetCoffeesQueryHandler(repository.Object);

            var result = await queryHandler
                         .Handle(new GetCoffeesQuery(), It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            Assert.That(result, Is.InstanceOf <List <CoffeeDto> >());
            var coffee = new Coffee {
                Id = 1, Country = "a", CoffeeType = Core.Enums.CoffeeType.Arabica, BrandId = 1, loggedRecords = 1, Rating = 1
            };
            var transformedResult = selectorCaptor.Invoke(coffee);

            transformedResult.Id.Should().Be(1);
            transformedResult.BrandId.Should().Be(1);
            transformedResult.Country.Should().Be("a");
            transformedResult.LoggedRecords.Should().Be(1);
            transformedResult.Rating.Should().Be(1);
            transformedResult.CoffeeType.Should().Be(Core.Enums.CoffeeType.Arabica);
        }
        public async Task NewRecordCommandHandler_Verify_Dependencies()
        {
            var repository = new Mock <IRepository>();

            var record = GetRecord();

            var flavors = new string[] { "a", "b", "c" };

            var flavorPredicateCaptor = new ExpressionCaptor <Flavor, bool>();

            repository.Setup(x => x.ListAsync(flavorPredicateCaptor.Capture()))
            .ReturnsAsync(new List <Flavor> {
                new Flavor {
                    Id = 1, Name = "a",
                }
            });

            var recordCaptor = new ArgumentCaptor <Record>();

            repository.Setup(x => x.InsertAsync(recordCaptor.Capture()))
            .ReturnsAsync(record);

            var flavorCaptor = new ArgumentCaptor <List <Flavor> >();

            repository.Setup(x => x.InsertRangeAsync(flavorCaptor.Capture()))
            .ReturnsAsync(
                new List <Flavor> {
                new Flavor {
                    Id = 2, Name = "b",
                },
                new Flavor {
                    Id = 3, Name = "c",
                }
            });

            var recordFlavorCaptor = new ArgumentCaptor <List <RecordFlavor> >();

            repository.Setup(x => x.InsertRangeAsync(recordFlavorCaptor.Capture()));

            var handler = new NewRecordCommandHandler(repository.Object);

            var result = await handler
                         .Handle(new NewRecordCommand(record.CoffeeId, record.DoseIn, record.DoseOut, record.Time, flavors, record.Rating),
                                 It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            repository.Verify(x => x.InsertAsync(recordCaptor.Value), Times.Once);
            repository.Verify(x => x.InsertRangeAsync(flavorCaptor.Value), Times.Once);
            repository.Verify(x => x.InsertRangeAsync(recordFlavorCaptor.Value), Times.Once);
            repository.Verify(x => x.ListAsync(flavorPredicateCaptor.Value), Times.Once);
            repository.VerifyNoOtherCalls();
        }
예제 #4
0
        public async Task GetCoffeeQueryHandler_Works()
        {
            var Coffees = new List <Coffee> {
                new Coffee {
                    Id = 1, Country = "a", CoffeeType = Core.Enums.CoffeeType.Arabica, BrandId = 1, loggedRecords = 1, Rating = 1
                },
                new Coffee {
                    Id = 2, Country = "b", CoffeeType = Core.Enums.CoffeeType.Robusto, BrandId = 2, loggedRecords = 2, Rating = 2
                },
            };

            var repository = new Mock <IRepository>();

            var predicateCaptor = new ExpressionCaptor <Coffee, bool>();
            var selectorCaptor  = new ExpressionCaptor <Coffee, CoffeeDto>();

            repository.Setup(x => x.SingleAsync(
                                 predicateCaptor.Capture(),
                                 selectorCaptor.Capture())
                             ).ReturnsAsync(new CoffeeDto());

            var queryHandler = new GetCoffeeQueryHandler(repository.Object);

            var result = await queryHandler
                         .Handle(new GetCoffeeQuery(1), It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            result.Should().BeOfType <CoffeeDto>();

            var actualResult      = Coffees.Single(predicateCaptor.Compile());
            var transformedResult = selectorCaptor.Invoke(actualResult);

            transformedResult.Id.Should().Be(1);
            transformedResult.BrandId.Should().Be(1);
            transformedResult.Country.Should().Be("a");
            transformedResult.LoggedRecords.Should().Be(1);
            transformedResult.Rating.Should().Be(1);
            transformedResult.CoffeeType.Should().Be(Core.Enums.CoffeeType.Arabica);
        }
예제 #5
0
        public async Task GetRecordQueryHandler_Works()
        {
            var Records = new List <Record> {
                new Record {
                    Id = 1, CoffeeId = 1, DoseIn = 1, DoseOut = 1, Rating = 1, Time = 1, CreateDate = DateTime.Today
                },
                new Record {
                    Id = 2, CoffeeId = 2, DoseIn = 2, DoseOut = 2, Rating = 2, Time = 2, CreateDate = DateTime.Today.AddDays(-1)
                },
            };

            var repository = new Mock <IRepository>();

            var predicateCaptor = new ExpressionCaptor <Record, bool>();
            var selectorCaptor  = new ExpressionCaptor <Record, RecordDto>();

            repository.Setup(x => x.SingleAsync(
                                 predicateCaptor.Capture(),
                                 selectorCaptor.Capture())
                             ).ReturnsAsync(new RecordDto());

            var queryHandler = new GetRecordQueryHandler(repository.Object);

            var result = await queryHandler
                         .Handle(new GetRecordQuery(1), It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            result.Should().BeOfType <RecordDto>();

            var actualResult      = Records.Single(predicateCaptor.Compile());
            var transformedResult = selectorCaptor.Invoke(actualResult);

            transformedResult.Id.Should().Be(1);
            transformedResult.DoseIn.Should().Be(1);
            transformedResult.DoseOut.Should().Be(1);
            transformedResult.Rating.Should().Be(1);
            transformedResult.Time.Should().Be(1);
            transformedResult.CreateDate.Should().Be(DateTime.Today);
        }
        public async Task GetBrandsQueryHandler_Works()
        {
            var repository = new Mock <IRepository>();

            var selectorCaptor = new ExpressionCaptor <Brand, BrandDto>();

            repository.Setup(x => x.ListAsync(selectorCaptor.Capture())).ReturnsAsync(new List <BrandDto>());

            var queryHandler = new GetBrandsQueryHandler(repository.Object);

            var result = await queryHandler
                         .Handle(new GetBrandsQuery(), It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            Assert.That(result, Is.InstanceOf <List <BrandDto> >());
            var brand = new Brand {
                Id = 1, Name = "a", ImageUri = "uri1"
            };
            var transformedResult = selectorCaptor.Invoke(brand);

            transformedResult.Id.Should().Be(1);
            transformedResult.Name.Should().Be("a");
            transformedResult.ImageUri.Should().Be("uri1");
        }
예제 #7
0
        public async Task GetBrandQueryHandler_Works()
        {
            var brands = new List <Brand> {
                new Brand {
                    Id = 1, Name = "a", ImageUri = "uri1"
                },
                new Brand {
                    Id = 2, Name = "b", ImageUri = "uri2"
                }
            };

            var repository = new Mock <IRepository>();

            var predicateCaptor = new ExpressionCaptor <Brand, bool>();
            var selectorCaptor  = new ExpressionCaptor <Brand, BrandDto>();

            repository.Setup(x => x.SingleAsync(
                                 predicateCaptor.Capture(),
                                 selectorCaptor.Capture())
                             ).ReturnsAsync(new BrandDto());

            var queryHandler = new GetBrandQueryHandler(repository.Object);

            var result = await queryHandler
                         .Handle(new GetBrandQuery(1), It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            Assert.That(result, Is.InstanceOf <BrandDto>());

            var actualResult      = brands.Single(predicateCaptor.Compile());
            var transformedResult = selectorCaptor.Invoke(actualResult);

            transformedResult.Id.Should().Be(1);
            transformedResult.Name.Should().Be("a");
            transformedResult.ImageUri.Should().Be("uri1");
        }
        public async Task NewRecordCommandHandler_Verify_Expressions()
        {
            var repository = new Mock <IRepository>();

            var record = GetRecord();

            var flavors = new string[] { "a", "b", "c" };

            var flavorPredicateCaptor = new ExpressionCaptor <Flavor, bool>();

            repository.Setup(x => x.ListAsync(flavorPredicateCaptor.Capture()))
            .ReturnsAsync(new List <Flavor> {
                new Flavor {
                    Id = 1, Name = "a",
                }
            });

            var recordCaptor = new ArgumentCaptor <Record>();

            repository.Setup(x => x.InsertAsync(recordCaptor.Capture()))
            .ReturnsAsync(record);

            var insertFlavorCaptor = new ArgumentCaptor <List <Flavor> >();

            repository.Setup(x => x.InsertRangeAsync(insertFlavorCaptor.Capture()))
            .ReturnsAsync(
                new List <Flavor> {
                new Flavor {
                    Id = 2, Name = "b",
                },
                new Flavor {
                    Id = 3, Name = "c",
                }
            });

            var recordFlavorCaptor = new ArgumentCaptor <List <RecordFlavor> >();

            repository.Setup(x => x.InsertRangeAsync(recordFlavorCaptor.Capture()));

            var handler = new NewRecordCommandHandler(repository.Object);

            var result = await handler
                         .Handle(new NewRecordCommand(record.CoffeeId, record.DoseIn, record.DoseOut, record.Time, flavors, record.Rating),
                                 It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            var existingFlavors = new List <Flavor> {
                new Flavor {
                    Name = "a"
                }, new Flavor {
                    Name = "d"
                }, new Flavor {
                    Name = "e"
                },
            };
            var queriedFlavors = existingFlavors.Where(flavorPredicateCaptor.Compile());

            queriedFlavors.Select(x => x.Name).Should().Equal("a");

            recordCaptor.Value.CoffeeId.Should().Be(record.CoffeeId);
            recordCaptor.Value.DoseIn.Should().Be(record.DoseIn);
            recordCaptor.Value.DoseOut.Should().Be(record.DoseOut);
            recordCaptor.Value.Time.Should().Be(record.Time);

            insertFlavorCaptor.Value.Select(x => x.Name).Should().Equal("b", "c");
            recordFlavorCaptor
            .Value
            .Select(x => (x.FlavorId, x.RecordId))
            .Should().Equal(
                new List <(long, long)>
            {
                (1, 1),
                (2, 1),
                (3, 1)
            });
예제 #9
0
        public async Task GetCoffeesQueryHandler_Works()
        {
            var repository = new Mock <IRepository>();

            var coffeeSelectorCaptor = new ExpressionCaptor <Coffee, CoffeeDto>();
            var brandSelectorCaptor  = new ExpressionCaptor <Brand, BrandDto>();
            var brandPredicateCaptor = new ExpressionCaptor <Brand, bool>();

            repository.Setup(x => x.ListAsync(coffeeSelectorCaptor.Capture()))
            .ReturnsAsync(new List <CoffeeDto>
            {
                new CoffeeDto {
                    Id = 1, BrandId = 1, CoffeeType = Core.Enums.CoffeeType.Arabica, Country = "a", LoggedRecords = 1, Rating = 1
                },
                new CoffeeDto {
                    Id = 2, BrandId = 1, CoffeeType = Core.Enums.CoffeeType.Robusto, Country = "b", LoggedRecords = 2, Rating = 2
                },
                new CoffeeDto {
                    Id = 3, BrandId = 2, CoffeeType = Core.Enums.CoffeeType.Robusto, Country = "c", LoggedRecords = 3, Rating = 3
                }
            });

            repository.Setup(x => x.ListAsync(brandPredicateCaptor.Capture(), brandSelectorCaptor.Capture()))
            .ReturnsAsync(new List <BrandDto>
            {
                new BrandDto {
                    Id = 1, Name = "a", ImageUri = "uri-1"
                },
                new BrandDto {
                    Id = 2, Name = "b", ImageUri = "uri-2"
                }
            });

            var queryHandler = new GetDetailedCoffeesQueryHandler(repository.Object);

            var result = await queryHandler
                         .Handle(new GetDetailedCoffeesQuery(), It.IsAny <CancellationToken>())
                         .ConfigureAwait(false);

            result.Should().BeOfType <List <DetailedCoffeeDto> >();
            result.Count.Should().Be(3);
            result.Should().BeEquivalentTo(new List <DetailedCoffeeDto>
            {
                new DetailedCoffeeDto {
                    Id = 1, BrandName = "a", CoffeeType = Core.Enums.CoffeeType.Arabica.ToString(), Country = "a", ImageUri = "uri-1", LoggedRecords = 1, Rating = 1
                },
                new DetailedCoffeeDto {
                    Id = 2, BrandName = "a", CoffeeType = Core.Enums.CoffeeType.Robusto.ToString(), Country = "b", ImageUri = "uri-1", LoggedRecords = 2, Rating = 2
                },
                new DetailedCoffeeDto {
                    Id = 3, BrandName = "b", CoffeeType = Core.Enums.CoffeeType.Robusto.ToString(), Country = "c", ImageUri = "uri-2", LoggedRecords = 3, Rating = 3
                }
            });

            var coffee = new Coffee {
                Id = 1, Country = "a", CoffeeType = Core.Enums.CoffeeType.Arabica, BrandId = 1, loggedRecords = 1, Rating = 1
            };
            var transformedCoffeeResult = coffeeSelectorCaptor.Invoke(coffee);

            transformedCoffeeResult.Id.Should().Be(1);
            transformedCoffeeResult.BrandId.Should().Be(1);
            transformedCoffeeResult.Country.Should().Be("a");
            transformedCoffeeResult.LoggedRecords.Should().Be(1);
            transformedCoffeeResult.Rating.Should().Be(1);
            transformedCoffeeResult.CoffeeType.Should().Be(Core.Enums.CoffeeType.Arabica);

            var brands = new List <Brand>
            {
                new Brand {
                    Id = 1, Name = "a", ImageUri = "uri-1"
                },
                new Brand {
                    Id = 2, Name = "b", ImageUri = "uri-2"
                },
                new Brand {
                    Id = 3, Name = "c", ImageUri = "uri-3"
                },
            };

            var transformedBrandResult = brandSelectorCaptor.Invoke(brands.First());

            transformedBrandResult.Id.Should().Be(1);
            transformedBrandResult.Name.Should().Be("a");
            transformedBrandResult.ImageUri.Should().Be("uri-1");

            var filteredBrandIds = brands.Where(brandPredicateCaptor.Compile()).Select(x => x.Id);

            filteredBrandIds.Should().Equal(1, 2);
        }