public async Task DeleteCategoryAsync_IdPresent_RequestedProductDeleted()
        {
            var mockUserContext            = new Mock <IUserContext>();
            var mockProductContext         = new Mock <IProductCatalogueContext>();
            List <DbCategory> categoryList = new List <DbCategory>()
            {
                new DbCategory()
                {
                    Id = 1, IsDeleted = true
                },
                new DbCategory()
                {
                    Id = 2
                }
            };

            mockProductContext.Setup(c => c.Categories).ReturnsEntitySet(categoryList);
            mockProductContext.Setup(c => c.Products).ReturnsEntitySet(new List <CatalogueProduct>());
            var service = new ProductCategoryService(mockProductContext.Object, mockUserContext.Object);

            await service.DeleteCategoryAsync(1);

            var categories = await service.GetCategoriesAsync(0, categoryList.Count);

            Assert.DoesNotContain(categories, h => h.Id == 1);
        }
        public async Task GetCategoriesAsync_TwoElementsRequested_ReturnsCollectionWithTwoElements()
        {
            var mockUserContext    = new Mock <IUserContext>();
            var mockProductContext = new Mock <IProductCatalogueContext>();
            List <CatalogueProduct> productList = new List <CatalogueProduct>()
            {
                new CatalogueProduct()
                {
                    Id = 1, CategoryId = 1
                },
                new CatalogueProduct()
                {
                    Id = 2, CategoryId = 2
                }
            };
            List <DbCategory> categoryList = new List <DbCategory>()
            {
                new DbCategory()
                {
                    Id = 1
                },
                new DbCategory()
                {
                    Id = 2
                }
            };

            mockProductContext.Setup(c => c.Products).ReturnsEntitySet(productList);
            mockProductContext.Setup(c => c.Categories).ReturnsEntitySet(categoryList);
            var service = new ProductCategoryService(mockProductContext.Object, mockUserContext.Object);

            var list = await service.GetCategoriesAsync(0, 2);

            Assert.Equal(2, list.Count);
        }
        public async Task GetCategoriesAsync_IntStartAndAmount_ListProductCategoryListItems(
            [Frozen] Mock <IProductCatalogueContext> context,
            ProductCategoryService service,
            IFixture fixture)
        {
            // arrange
            var dbCategories = fixture.CreateMany <DbProductCategory>(2).OrderBy(s => s.Id).ToList();

            var dbProducts = fixture.CreateMany <CatalogueProduct>(5).ToList();

            dbProducts[0].CategoryId = dbCategories[0].Id;
            dbProducts[1].CategoryId = dbCategories[0].Id;
            dbProducts[2].CategoryId = dbCategories[1].Id;
            dbProducts[3].CategoryId = dbCategories[1].Id;
            dbProducts[4].CategoryId = dbCategories[1].Id;

            int start = 0, amount = 2;

            context.Setup(s => s.Categories).ReturnsEntitySet(dbCategories);

            context.Setup(s => s.Products).ReturnsEntitySet(dbProducts);

            // act
            var productCategoryListItems = await service.GetCategoriesAsync(start, amount);

            // assert
            Assert.Equal(2, productCategoryListItems.Count);

            Assert.Collection(
                productCategoryListItems,
                item => Assert.Equal(2, productCategoryListItems[0].ProductCount),
                item => Assert.Equal(3, productCategoryListItems[1].ProductCount));
        }
        public async Task GetCategoriesAsync_EmptyCollectionRequested_ReturnsEmptyCollection()
        {
            var mockUserContext    = new Mock <IUserContext>();
            var mockProductContext = new Mock <IProductCatalogueContext>();

            mockProductContext.Setup(c => c.Products).ReturnsEntitySet(new List <CatalogueProduct>());
            mockProductContext.Setup(c => c.Categories).ReturnsEntitySet(new List <DbCategory>());
            var service = new ProductCategoryService(mockProductContext.Object, mockUserContext.Object);

            var list = await service.GetCategoriesAsync(0, 0);

            Assert.Empty(list);
        }
        public async Task GetCategoriesAsync_CreqteFiveElement_ReturnFiveElements(
            [Frozen] Mock <IProductCatalogueContext> context,
            ProductCategoryService service,
            IFixture fixture)
        {
            var categories = fixture.CreateMany <DbProductCategory>(5).ToList();
            var products   = fixture.CreateMany <CatalogueProduct>(5).ToList();

            context.Setup(x => x.Products).ReturnsEntitySet(products);
            context.Setup(x => x.Categories).ReturnsEntitySet(categories);
            var result = await service.GetCategoriesAsync(0, 5);

            result.Count.Should().Be(5);
        }
        public async Task CreateCategoryAsync_AddOneElement(
            [Frozen] Mock <IProductCatalogueContext> context,
            ProductCategoryService service,
            IFixture fixture)
        {
            var categories     = fixture.CreateMany <DbProductCategory>(5).ToList();
            var products       = fixture.CreateMany <CatalogueProduct>(5).ToList();
            var updateCategory = fixture.Create <UpdateProductCategoryRequest>();

            context.Setup(x => x.Products).ReturnsEntitySet(products);
            context.Setup(x => x.Categories).ReturnsEntitySet(categories);

            await service.CreateCategoryAsync(updateCategory);

            var result = await service.GetCategoriesAsync(0, 6);

            result.Count.Should().Be(6);
        }
        public async Task DeleteCategoryAsync_OneElementDelete(
            [Frozen] Mock <IProductCatalogueContext> context,
            ProductCategoryService service,
            IFixture fixture)
        {
            var categories = fixture.CreateMany <DbProductCategory>(5).ToList();
            var products   = fixture.CreateMany <CatalogueProduct>(5).ToList();

            categories[0].IsDeleted = true;
            context.Setup(x => x.Products).ReturnsEntitySet(products);
            context.Setup(x => x.Categories).ReturnsEntitySet(categories);

            await service.DeleteCategoryAsync(categories[0].Id);

            var result = await service.GetCategoriesAsync(0, 6);

            result.Count.Should().Be(4);
        }