Пример #1
0
        public async Task Should_Return_Existing_Items_Test(string guid)           //products from in-memory-db or combined
        //Arrange
        {
            AlzaDbContext inMemoryDbContext = _factory.Create();

            bool isDbReady = await inMemoryDbContext.Database.EnsureCreatedAsync();

            isDbReady.ShouldBeTrue();

            try {
                _factory.SeedTestingData(inMemoryDbContext);
            }
            catch (Exception e) {
                e.ShouldBeNull();
            }

            var sut = new GetProductRequest.Handler(_mapper, inMemoryDbContext);

            //Act
            var result = await sut.Handle(new GetProductRequest { ProductId = Guid.Parse(guid) }, CancellationToken.None);

            //Assert
            result.ShouldNotBeNull();
            result.ShouldBeOfType <GetProductResponse>();

            await inMemoryDbContext.Database.EnsureDeletedAsync();

            await inMemoryDbContext.DisposeAsync();
        }
Пример #2
0
        public async Task Should_Verify_Item_Update_Test()           //product update within in-memory-db or combined
        //Arrange
        {
            AlzaDbContext inMemoryDbContext = _factory.Create();

            bool isDbReady = await inMemoryDbContext.Database.EnsureCreatedAsync();

            isDbReady.ShouldBeTrue();

            try {
                _factory.SeedTestingData(inMemoryDbContext);
            }
            catch (Exception e) {
                e.ShouldBeNull();
            }

            var sut = new UpdateProductRequest.Handler(_mockMapper.Object, inMemoryDbContext);

            //Act
            UpdateProductResponse result = await sut.Handle(new UpdateProductRequest { ProductId = Guid.Empty }, CancellationToken.None);

            //Assert
            result.ProductUpdated.ShouldBe(false);
            result.ProductDescription.ShouldBeNull();
            result.ProductUpdateMessage.ShouldBe("Product not found");
            _mockMapper.Verify(mapper => mapper.Map <UpdateProductResponse>(It.IsAny <UpdateProductRequest>()), Times.Never);


            //Act
            result = await sut.Handle(new UpdateProductRequest { ProductId = Guid.Parse("1BBDFAD4-B8CD-472A-881B-08D890B3E94F"), Description = "Old description" }, CancellationToken.None);

            //Assert
            result.ProductUpdated.ShouldBe(false);
            result.ProductDescription.ShouldBeNull();
            result.ProductUpdateMessage.ShouldBe("Product already up to date");
            _mockMapper.Verify(mapper => mapper.Map <UpdateProductResponse>(It.IsAny <UpdateProductRequest>()), Times.Never);
            Product dbResult = await inMemoryDbContext.Products.FindAsync(Guid.Parse("1BBDFAD4-B8CD-472A-881B-08D890B3E94F"));

            dbResult.Description.ShouldBe("Old description");

            //Act
            _ = await sut.Handle(new UpdateProductRequest { ProductId = Guid.Parse("1BBDFAD4-B8CD-472A-881B-08D890B3E94F"), Description = "New description" }, CancellationToken.None);

            //Assert
            dbResult = await inMemoryDbContext.Products.FindAsync(Guid.Parse("1BBDFAD4-B8CD-472A-881B-08D890B3E94F"));

            dbResult.Description.ShouldBe("New description");
            _mockMapper.Verify(mapper => mapper.Map <UpdateProductResponse>(It.IsAny <Product>()), Times.Once);

            await inMemoryDbContext.Database.EnsureDeletedAsync();

            await inMemoryDbContext.DisposeAsync();
        }
Пример #3
0
        public async Task Should_Get_All_Existing_Items_Paginated_Test()           //paginated products from in-memory-db or combined
        //Arrange
        {
            AlzaDbContext inMemoryDbContext = _factory.Create();

            bool isDbReady = await inMemoryDbContext.Database.EnsureCreatedAsync();

            isDbReady.ShouldBeTrue();

            try {
                _factory.SeedTestingData(inMemoryDbContext);
            }
            catch (Exception e) {
                e.ShouldBeNull();
            }

            var sut = new GetProductsPaginatedRequest.Handler(_mapper, inMemoryDbContext);

            //Act
            var result = await sut.Handle(new GetProductsPaginatedRequest { PageNumber = 1 }, CancellationToken.None);

            //Assert
            result.ShouldNotBeEmpty();
            result.Count().ShouldBeEquivalentTo(10);             //default page size


            //Act
            result = await sut.Handle(new GetProductsPaginatedRequest { PageNumber = 1, PageSize = 20 }, CancellationToken.None);

            //Assert
            result.ShouldNotBeEmpty();
            result.Count().ShouldBeEquivalentTo(20);             //set page size


            //Act
            result = await sut.Handle(new GetProductsPaginatedRequest { PageNumber = 1, PageSize = 1 }, CancellationToken.None);             // ordering by id

            var first = result.FirstOrDefault();

            //Assert
            result.ShouldNotBeEmpty();
            result.Count().ShouldBeEquivalentTo(1);             //set page size and number


            //Act
            result = await sut.Handle(new GetProductsPaginatedRequest { PageNumber = 2, PageSize = 1 }, CancellationToken.None);             // ordering by id

            var second = result.FirstOrDefault();

            //Assert
            result.ShouldNotBeEmpty();
            result.Count().ShouldBeEquivalentTo(1);                  //set page size and number
            first.ProductName.ShouldNotBeSameAs(second.ProductName); //different pages


            //Act
            result = await sut.Handle(new GetProductsPaginatedRequest { PageNumber = 1, PageSize = 1 }, CancellationToken.None);             // ordering by id

            var firstAgain = result.FirstOrDefault();

            //Assert
            result.ShouldNotBeEmpty();
            result.Count().ShouldBeEquivalentTo(1);                         //set page size and number
            first.ProductName.ShouldBeEquivalentTo(firstAgain.ProductName); //same page


            //Act
            result = await sut.Handle(new GetProductsPaginatedRequest { PageNumber = 1, PageSize = 1, PageOrderKey = product => product.Description }, CancellationToken.None);             // ordering by description

            //Assert
            result.ShouldNotBeEmpty();
            result.FirstOrDefault().ProductName.ShouldBe("TV"); //ordering by description
            result.Count().ShouldBeEquivalentTo(1);             //set page size and number

            await inMemoryDbContext.Database.EnsureDeletedAsync();

            await inMemoryDbContext.DisposeAsync();
        }