public void Retrieve_WithVAlidEntityID_ReturnsAValidEntity()
        {
            var options = ConnectionOptionHelper.Sqlite();
            //Arrange

            var category = new Category
            {
                Name        = "Shoes",
                Description = "Shoes Department"
            };

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                context.Categories.Add(category);
                context.SaveChanges();
            }

            using (var context = new ECommerceDbContext(options))
            {
                var sut = new CategoryRepository(context);
                // Act
                var actual = sut.Retrieve(category.ID);
                //Assert
                Assert.NotNull(actual);
                Assert.Equal(category.Name, actual.Name);
                Assert.Equal(category.Description, actual.Description);
            }
        }
        public void Delete_WithValidEntityID_ShouldRemoveRecordFromDatabase()
        {
            var options = ConnectionOptionHelper.Sqlite();
            //Arrange
            var category = new Category
            {
                Name        = "Bag",
                Description = "Bag Department"
            };

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                var sut = new CategoryRepository(context);
                context.Categories.Add(category);
                context.SaveChanges();
                //Act
                sut.Delete(category.ID);
                context.SaveChanges();
                var actual = context.Categories.Find(category.ID);
                //Assert
                Assert.Null(actual);
            }
        }
        public void Update_WithValidEntity_ShouldUpdateDatabaseRecord()
        {
            var options     = ConnectionOptionHelper.Sqlite();
            var oldCategory = new Category
            {
                Name        = "shoes",
                Description = "Shoes Department"
            };

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                //Arrange
                context.Categories.Add(oldCategory);
                context.SaveChanges();
            }
            var newName        = "Bag";
            var newDescription = "Bag Department";

            using (var context = new ECommerceDbContext(options))
            {
                var sut = new CategoryRepository(context);
                //Act
                var record = context.Categories.Find(oldCategory.ID);
                record.Name        = newName;
                record.Description = newDescription;
                sut.Update(record.ID, record);
                var newCategory = context.Categories.Find(record.ID);
                //Assert
                Assert.Equal(newCategory.Name, newName);
                Assert.Equal(newCategory.Description, newDescription);
            }
        }
        public void Create_WithValidEntity_ShouldCreateDatabaseRecord()
        {
            var options  = ConnectionOptionHelper.Sqlite();
            var category = new Category
            {
                Name        = "Bag",
                Description = "Bag Department"
            };

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                var sut = new CategoryRepository(context);
                //Act
                sut.Create(category);
            }

            using (var context = new ECommerceDbContext(options))
            {
                //Assert
                Assert.True(category.ID != 0);

                var entity = context.Categories.Find(category.ID);
                Assert.NotNull(entity);
            }
        }
        public void Create_WithValidEntity_ShouldCreateDatabaseRecord()
        {
            var options = ConnectionOptionHelper.Sqlite();
            // Arrange
            var supplier = new Supplier
            {
                Name        = "Shoes Supplier",
                Description = "This is a Shoes Supplier",
                IsActive    = true
            };

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                var sut = new SupplierRepository(context);

                // Act
                sut.Create(supplier);
            }
            using (var context = new ECommerceDbContext(options))
            {
                // Assert
                var actual = context.Suppliers.Find(supplier.ID);

                Assert.NotNull(actual);
                Assert.Equal(supplier.Name, actual.Name);
                Assert.Equal(supplier.Description, actual.Description);
            }
        }
        public void Retrieve_WithVAlidEntityID_ReturnsAValidEntity()
        {
            var options = ConnectionOptionHelper.Sqlite();
            // Arrange
            var supplier = new Supplier
            {
                Name        = "Bag Supplier",
                Description = "This is a Bag Supplier"
            };

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                context.Suppliers.Add(supplier);
                context.SaveChanges();
            }
            using (var context = new ECommerceDbContext(options))
            {
                var sut = new SupplierRepository(context);
                // Act
                var actual = sut.Retrieve(supplier.ID);
                //Assert
                Assert.NotNull(actual);
                Assert.Equal(supplier.Name, actual.Name);
                Assert.Equal(supplier.Description, actual.Description);
            }
        }
        public void Delete_WithValidEntityID_ShouldRemoveRecordFromDatabase()
        {
            var options = ConnectionOptionHelper.Sqlite();
            //Arrange
            var supplier = new Supplier
            {
                Name        = "Shirt Supplier",
                Description = "This is a Shirt Supplier",
                IsActive    = true
            };

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                var sut = new SupplierRepository(context);
                context.Suppliers.Add(supplier);
                context.SaveChanges();
                //Act
                sut.Delete(supplier.ID);
                context.SaveChanges();
                var actual = context.Suppliers.Find(supplier.ID);
                //Assert
                Assert.Null(actual);
            }
        }
        public void Create_WithNotExistingCategoryID_ShouldThrowException()
        {
            var options = ConnectionOptionHelper.Sqlite();
            //Arrange

            Product product;

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                product = new Product
                {
                    Name        = "Sling Bag",
                    Description = "This is a Sling Bag",
                    Price       = 199,
                    //CategoryID = 100,
                    ImageUrl = "slingbag.jpg"
                };

                var sut = new ProductRepository(context);

                //Act &Assert
                Assert.Throws <DbUpdateException>(() => sut.Create(product));
            }
        }
        public void Update_WithValidEntity_ShouldUpdateDatabaseRecord()
        {
            var options = ConnectionOptionHelper.Sqlite();
            //Arrange
            Product oldProduct;

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                //Arrange
                var category = new Category
                {
                    Name        = "Bag",
                    Description = "Bag Department"
                };
                context.Categories.Add(category);
                oldProduct = new Product
                {
                    Name        = "Sling Bag",
                    Description = "This is a Sling Bag",
                    Price       = 199,
                    //CategoryID = category.ID,
                    ImageUrl = "slingbag.jpg"
                };
                context.Products.Add(oldProduct);
                context.SaveChanges();
            }
            var newName        = "Rubbes Shoes";
            var newDescription = "This is a Rubber Shoes";
            var newPrice       = 500;
            var newImageUrl    = "shoes.jpg";

            using (var context = new ECommerceDbContext(options))
            {
                var sut    = new ProductRepository(context);
                var record = context.Products.Find(oldProduct.ID);
                //Act
                record.Name        = newName;
                record.Description = newDescription;
                record.Price       = newPrice;
                record.ImageUrl    = newImageUrl;
                sut.Update(record.ID, record);
                var newProduct = context.Products.Find(record.ID);
                //Assert
                Assert.Equal(newName, newProduct.Name);
                Assert.Equal(newDescription, newProduct.Description);
                Assert.Equal(newPrice, newProduct.Price);
                Assert.Equal(newImageUrl, newProduct.ImageUrl);
            }
        }
        public void Retrieve_WithSkipAndCount_ReturnsTheCorrectPage()
        {
            var options = ConnectionOptionHelper.Sqlite();

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                var category = new Category
                {
                    Name        = "Bag",
                    Description = "Bag Department"
                };
                context.Categories.Add(category);
                // Arrange
                for (int i = 1; i <= 25; i++)
                {
                    context.Products.Add(new Product
                    {
                        Name        = string.Format("Product {0}", i),
                        Description = string.Format("Description {0}", i),
                        Price       = i,
                        //CategoryID = category.ID,
                        ImageUrl = string.Format("image{0}.jpg", i)
                    });
                }
                context.SaveChanges();
            }
            using (var context = new ECommerceDbContext(options))
            {
                var sut = new ProductRepository(context);

                // Act & Assert
                var list = sut.Retrieve(5, 5);
                Assert.True(list.Count() == 5);

                list = sut.Retrieve(0, 5);
                Assert.True(list.Count() == 5);

                list = sut.Retrieve(10, 5);
                Assert.True(list.Count() == 5);

                list = sut.Retrieve(15, 5);
                Assert.True(list.Count() == 5);

                list = sut.Retrieve(20, 5);
                Assert.True(list.Count() == 5);
            }
        }
        public void Retrieve_WithNonexistingEntityID_ReturnsNull()
        {
            var options = ConnectionOptionHelper.Sqlite();

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                //Arrange
                var sut = new ProductRepository(context);
                //Act
                var actual = sut.Retrieve(-1);
                //Assert
                Assert.Null(actual);
            }
        }
        public void Retrieve_WithSkipAndCount_ReturnsTheCorrectPage()
        {
            var options = ConnectionOptionHelper.Sqlite();

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                // Arrange
                for (int i = 1; i <= 25; i++)
                {
                    context.Suppliers.Add(new Supplier
                    {
                        Name        = string.Format("Supplier {0}", i),
                        Description = string.Format("Description {0}", i),
                        IsActive    = true
                    });
                }
                context.SaveChanges();
            }

            using (var context = new ECommerceDbContext(options))
            {
                var sut = new SupplierRepository(context);

                // Act & Assert
                var list = sut.Retrieve(5, 5);
                Assert.True(list.Count() == 5);

                list = sut.Retrieve(0, 5);
                Assert.True(list.Count() == 5);

                list = sut.Retrieve(10, 5);
                Assert.True(list.Count() == 5);

                list = sut.Retrieve(15, 5);
                Assert.True(list.Count() == 5);

                list = sut.Retrieve(20, 5);
                Assert.True(list.Count() == 5);
            }
        }
        public void Retrieve_WithVAlidEntityID_ReturnsAValidEntity()
        {
            var options = ConnectionOptionHelper.Sqlite();
            //Arrange
            Product product;

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                var category = new Category
                {
                    Name        = "Bag",
                    Description = "Bag Department"
                };
                var categoryRepo = new CategoryRepository(context);
                context.Categories.Add(category);
                product = new Product
                {
                    Name        = "Sling Bag",
                    Description = "This is a Sling Bag",
                    Price       = 199,
                    //CategoryID = category.ID,
                    ImageUrl = "slingbag.jpg"
                };
                context.Products.Add(product);
                context.SaveChanges();
            }

            using (var context = new ECommerceDbContext(options))
            {
                var sut = new ProductRepository(context);
                //Act
                var retrievedProduct = sut.Retrieve(product.ID);
                //Assert
                Assert.NotNull(retrievedProduct);
                Assert.Equal(retrievedProduct.Name, product.Name);
                Assert.Equal(retrievedProduct.Description, product.Description);
                Assert.Equal(retrievedProduct.Price, product.Price);
                //Assert.Equal(retrievedProduct.CategoryID, product.CategoryID);
            }
        }
        public void Create_WithValidEntity_ShouldCreateDatabaseRecord()
        {
            var options = ConnectionOptionHelper.Sqlite();
            //Arrange

            Product product;

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                var category = new Category
                {
                    Name        = "Bag",
                    Description = "Bag Department"
                };
                context.Categories.Add(category);
                context.SaveChanges();
                product = new Product
                {
                    Name        = "Sling Bag",
                    Description = "This is a Sling Bag",
                    Price       = 199,
                    //CategoryID = category.ID,
                    ImageUrl = "slingbag.jpg"
                };
            }
            using (var context = new ECommerceDbContext(options))
            {
                var sut = new ProductRepository(context);

                //Act
                sut.Create(product);
                Assert.True(product.ID != 0);
                var actual = context.Products.Find(product.ID);
                //Assert
                Assert.NotNull(actual);
            }
        }
        public void Delete_WithExistingProduct_ShouldThrowAnException()
        {
            var options = ConnectionOptionHelper.Sqlite();
            //Arrange
            //Arrange
            var category = new Category
            {
                Name        = "Bag",
                Description = "Bag Department"
            };
            Product product;

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                var sut = new CategoryRepository(context);
                context.Categories.Add(category);

                product = new Product
                {
                    Name        = "Sling Bag",
                    Description = "This is a Sling Bag",
                    Price       = 199,
                    //CategoryID = category.ID,
                    ImageUrl = "slingbag.jpg"
                };
                context.Products.Add(product);
                context.SaveChanges();
            }
            using (var context = new ECommerceDbContext(options))
            {
                var sut = new CategoryRepository(context);
                //Act &Assert
                Assert.Throws <DbUpdateException>(() => sut.Delete(product.ID));
            }
        }
        public void Update_WithValidEntity_ShouldUpdateDatabaseRecord()
        {
            var options     = ConnectionOptionHelper.Sqlite();
            var oldSupplier = new Supplier
            {
                Name        = "Shirt Supplier",
                Description = "This is a Shirt Supplier",
                IsActive    = true
            };

            using (var context = new ECommerceDbContext(options))
            {
                context.Database.OpenConnection();
                context.Database.EnsureCreated();
                //Arrange
                context.Suppliers.Add(oldSupplier);
                context.SaveChanges();
            }

            var newName        = "Shoes Supplier";
            var newDescription = "This is a Shoes Supplier";

            using (var context = new ECommerceDbContext(options))
            {
                var sut = new SupplierRepository(context);
                //Act
                var record = context.Suppliers.Find(oldSupplier.ID);
                record.Name        = newName;
                record.Description = newDescription;
                sut.Update(record.ID, record);
                var newSupplier = context.Suppliers.Find(record.ID);
                //Assert
                Assert.Equal(newSupplier.Name, newName);
                Assert.Equal(newSupplier.Description, newDescription);
            }
        }