public async Task <Address> UpdateAddressAsync(int id, string addressString, bool save = true)
        {
            if (string.IsNullOrEmpty(addressString))
            {
                throw new ArgumentNullException($"Address could not be null or empty!");
            }

            if (string.IsNullOrWhiteSpace(addressString))
            {
                throw new ArgumentException($"Address could not be WhiteSpace!");
            }

            var address = await FindAddressByIDAsync(id);

            if (address == null)
            {
                throw new ArgumentException($"Address with ID {id} does not exist!");
            }
            else
            {
                address.Name = addressString;
                try
                {
                    context.Update(address);
                    await context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }
            return(address);
        }
        public async Task <Country> UpdateCountryAsync(int id, string name, bool save = true)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException($"Country name could not be null or empty!");
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentException($"Country name could not be WhiteSpace!");
            }

            var country = await FindCountryByIDAsync(id);

            if (country == null)
            {
                throw new ArgumentException($"Country with ID {id} does not exist!");
            }
            else
            {
                country.Name = name;
                try
                {
                    context.Update(country);
                    await context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }
            return(country);
        }
        public async Task <Warehouse> UpdateWarehouseAsync(
            int id,
            string warehouseName,
            int countryID,
            int cityID,
            int addressID,
            bool save = true)
        {
            if (string.IsNullOrEmpty(warehouseName))
            {
                throw new ArgumentException($"WarehouseName could not be null or empty!");
            }

            if (string.IsNullOrWhiteSpace(warehouseName))
            {
                throw new ArgumentException($"WarehouseName could not be WhiteSpace!");
            }

            if (this.context.Cities.Find(cityID) == null)
            {
                throw new ArgumentException($"City with ID {cityID} does not exist!");
            }

            if (this.context.Countries.Find(countryID) == null)
            {
                throw new ArgumentException($"Country with ID {cityID} does not exist!");
            }

            if (this.context.Addresses.Find(addressID) == null)
            {
                throw new ArgumentException($"Address with ID {cityID} does not exist!");
            }

            var warehouse = await FindWarehouseByIDAsync(id);

            if (warehouse == null)
            {
                throw new ArgumentException($"Warehouse with ID {id} does not exist!");
            }
            else
            {
                warehouse.Name      = warehouseName;
                warehouse.CountryID = countryID;
                warehouse.CityID    = cityID;
                warehouse.AddressID = addressID;
                try
                {
                    context.Update(warehouse);
                    await context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }
            return(warehouse);
        }
        public async Task <Supplier> UpdateSupplierAsync(
            int id,
            string supplierName,
            string uin,
            int countryId,
            int cityId,
            int addressId,
            string userId,
            bool isDeleted,
            bool save = true)
        {
            if (string.IsNullOrEmpty(supplierName))
            {
                throw new ArgumentNullException($"supplierName could not be null or empty!");
            }

            if (string.IsNullOrWhiteSpace(supplierName))
            {
                throw new ArgumentException($"supplierName could not be WhiteSpace!");
            }

            var supplier = await FindSupplierByIDAsync(id);

            if (supplier == null)
            {
                throw new ArgumentException($"Supplier with ID {id} does not exist!");
            }
            else
            {
                supplier.Name        = supplierName;
                supplier.UIN         = uin;
                supplier.CountryID   = countryId;
                supplier.CityID      = cityId;
                supplier.AddressID   = addressId;
                supplier.StoreUserId = userId;
                supplier.IsDeleted   = isDeleted;
                try
                {
                    context.Update(supplier);
                    await context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    throw;
                }
            }

            return(supplier);
        }
        public async Task ReturnNullWhenInvalidWarehouseIDIsPassed(int id)
        {
            //Arrange
            var ReturnNullWhenInvalidWarehouseIDIsPassed = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(ReturnNullWhenInvalidWarehouseIDIsPassed);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                var tmpWarehouse = new Warehouse()
                {
                    Name        = "valid name",
                    WarehouseID = 1011,
                    AddressID   = 1,
                    CityID      = 1,
                    CountryID   = 1
                };
                arrangeContext.Warehouses.Add(tmpWarehouse);
                await arrangeContext.SaveChangesAsync();
            }

            //Act & Assert
            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);
                await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.GetWarehouseByIDAsync(id));
            }
        }
        public async Task GetWarehouseWhenValidWarehouseIDPassed(int id)
        {
            //Arrange
            var GetWarehouseWhenValidWarehouseIDPassed = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(GetWarehouseWhenValidWarehouseIDPassed);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                var tmpWarehouse = new Warehouse()
                {
                    Name        = "valid name" + id.ToString(),
                    WarehouseID = id,
                    AddressID   = 1,
                    CityID      = 1,
                    CountryID   = 1
                };
                arrangeContext.Warehouses.Add(tmpWarehouse);
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var actualWarehouse = await sut.GetWarehouseByIDAsync(id);

                //Assert
                Assert.AreEqual(id, actualWarehouse.WarehouseID);
            }
        }
Exemplo n.º 7
0
        public async Task ThrowsArgumentExceptionWhenWarehouseIDDoesNotExist(int validWarehouseID, int validPurchaseID)
        {
            //Arrange
            var ThrowsArgumentExceptionWhenWarehouseIDDoesNotExist = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(ThrowsArgumentExceptionWhenWarehouseIDDoesNotExist);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                arrangeContext.Purchases.Add(new Purchase()
                {
                    PurchaseID = validPurchaseID
                });
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act & Assert
                await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.AddPurchaseToWarehouseAsync(validWarehouseID, validPurchaseID));
            }
        }
        public async Task ReturnNullWhenInvalidWarehouseNameIsPassed(string validWarehouseName)
        {
            //Arrange
            var ReturnNullWhenInvalidWarehouseNameIsPassed = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(ReturnNullWhenInvalidWarehouseNameIsPassed);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                arrangeContext.Warehouses.Add(new Warehouse()
                {
                    Name = "fakeName"
                });
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var actualWarehouse = await sut.FindWarehouseByNameAsync(validWarehouseName);

                //Assert
                Assert.AreEqual(null, actualWarehouse);
            }
        }
        public async Task FindWarehouseWhenValidWarehouseNamePassed(string validWarehouseName)
        {
            //Arrange
            var FindWarehouseWhenValidWarehouseNamePassed = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(FindWarehouseWhenValidWarehouseNamePassed);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                var tmpWarehouse = new Warehouse()
                {
                    Name        = validWarehouseName,
                    WarehouseID = 1000,
                    AddressID   = 1,
                    CityID      = 1,
                    CountryID   = 1
                };
                arrangeContext.Warehouses.Add(tmpWarehouse);
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var actualWarehouse = await sut.FindWarehouseByNameAsync(validWarehouseName);

                //Assert
                Assert.AreEqual(validWarehouseName, actualWarehouse.Name);
            }
        }
Exemplo n.º 10
0
        public async Task ThrowsArgumentExceptionWhenWarehouseNameIsWhiteSpaseAndAddressIDIsInvalid(string warehouseName, int cityID, int countryID, int addressID, bool toSave)
        {
            //Arrange
            var ThrowsArgumentExceptionWhenWarehouseNameIsWhiteSpaseAndAddressIDIsInvalid = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(ThrowsArgumentExceptionWhenWarehouseNameIsWhiteSpaseAndAddressIDIsInvalid);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                //arrangeContext.Warehouses.Add(new Warehouse() { Name = warehouseName });
                arrangeContext.Countries.Add(new Country()
                {
                    CountryID = countryID
                });
                arrangeContext.Cities.Add(new City()
                {
                    CityID = cityID
                });
                //arrangeContext.Addresses.Add(new Address() { AddressID = addressID });
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act & Assert
                await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.CreateWarehouseAsync(warehouseName, countryID, cityID, addressID, toSave));
            }
        }
Exemplo n.º 11
0
        public async Task GetAllWarehousesWhenNoneAreRegistred()
        {
            //Arrange
            var GetAllWarehousesWhenOneIsRegistred = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(GetAllWarehousesWhenOneIsRegistred);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                var warehousesList = arrangeContext.Warehouses.ToList();
                foreach (var item in warehousesList)
                {
                    arrangeContext.Warehouses.Remove(item);
                }
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var warehousesList = await sut.GetAllWarehousesAsync();

                //Assert
                Assert.AreEqual(0, warehousesList.Count);
            }
        }
        public async Task <Purchase> CreatePurchaseAsync(
            int supplierId,
            int warehouseId,
            DateTime purchaseDate,
            DateTime deliveryDate,
            DateTime deadlineDate,
            bool save = true)
        {
            var newPurchase = new Purchase()
            {
                SupplierID   = supplierId,
                WarehouseID  = warehouseId,
                PurchaseDate = purchaseDate,
                DeliveryDate = deliveryDate,
                DeadlineDate = deadlineDate
            };

            await this.context.Purchases.AddAsync(newPurchase);

            await context.SaveChangesAsync();

            return(newPurchase);
        }
        public async Task UpdateWarehouseCityWhenValidID(string warehouseName, int cityID, int countryID, int addressID, bool toSave)
        {
            //Arrange
            var UpdateWarehouseCityWhenValidID = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(UpdateWarehouseCityWhenValidID);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                arrangeContext.Countries.Add(new Country()
                {
                    CountryID = countryID
                });
                arrangeContext.Cities.Add(new City()
                {
                    CityID = cityID
                });
                arrangeContext.Addresses.Add(new Address()
                {
                    AddressID = addressID
                });
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var actualWarehouse = await sut.CreateWarehouseAsync(warehouseName, countryID, cityID, addressID, toSave);

                Assert.AreNotEqual(null, actualWarehouse);
                Assert.AreEqual(warehouseName, actualWarehouse.Name);
                Assert.AreEqual(countryID, actualWarehouse.CountryID);
                Assert.AreEqual(cityID, actualWarehouse.CityID);
                Assert.AreEqual(addressID, actualWarehouse.AddressID);

                var updatedWarehouse = await sut.UpdateWarehouseAsync(actualWarehouse.WarehouseID, warehouseName, countryID, 1, addressID, toSave);

                //Assert
                Assert.AreEqual(addressID, updatedWarehouse.AddressID);
                Assert.AreEqual(countryID, updatedWarehouse.CountryID);
                Assert.AreEqual(1, updatedWarehouse.CityID);
                Assert.AreEqual(warehouseName, updatedWarehouse.Name);
            }
        }
        public async Task <IdentityRole> CreateRoleAsync(string roleString, bool save = true)
        {
            var Role = await this.context.Roles.FirstOrDefaultAsync(x => x.Name == roleString);

            if (Role == null)
            {
                Role = new IdentityRole {
                    Name = roleString
                };
                context.Roles.Add(Role);
                if (save)
                {
                    await context.SaveChangesAsync();
                }
            }
            return(Role);
        }
        public async Task DeleteWarehouseWhenInvalidID(string warehouseName, int cityID, int countryID, int addressID, bool toSave)
        {
            //Arrange
            var FindWarehouseWhenValidWarehouseIdIsPassed = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(FindWarehouseWhenValidWarehouseIdIsPassed);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                arrangeContext.Countries.Add(new Country()
                {
                    CountryID = countryID
                });
                arrangeContext.Cities.Add(new City()
                {
                    CityID = cityID
                });
                arrangeContext.Addresses.Add(new Address()
                {
                    AddressID = addressID
                });
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var actualWarehouse = await sut.CreateWarehouseAsync(warehouseName, countryID, cityID, addressID, toSave);

                //Assert
                Assert.AreNotEqual(null, actualWarehouse);
                Assert.AreEqual(warehouseName, actualWarehouse.Name);
                Assert.AreEqual(countryID, actualWarehouse.CountryID);
                Assert.AreEqual(cityID, actualWarehouse.CityID);
                Assert.AreEqual(addressID, actualWarehouse.AddressID);

                await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.DeleteWarehouseAsync(actualWarehouse.WarehouseID + 100));
            }
        }
Exemplo n.º 16
0
        public async Task AddPurchaseWhenPurchaseIDAndWarehouseIDExist(int validWarehouseID, int validPurchaseID)
        {
            //Arrange
            var AddPurchaseWhenPurchaseIDAndWarehouseIDExist = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(AddPurchaseWhenPurchaseIDAndWarehouseIDExist);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                arrangeContext.Warehouses.Add(new Warehouse()
                {
                    WarehouseID = validWarehouseID
                });
                arrangeContext.Purchases.Add(new Purchase()
                {
                    PurchaseID = validPurchaseID
                });
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var isAdded = await sut.AddPurchaseToWarehouseAsync(validWarehouseID, validPurchaseID);

                var addedPurchase = context.Warehouses.Find(validWarehouseID).Purchases
                                    .Where(x => x.PurchaseID == validPurchaseID)
                                    .FirstOrDefault();

                //Assert
                Assert.AreEqual(true, isAdded);
                Assert.AreEqual(validPurchaseID, addedPurchase.PurchaseID);
            }
        }
        public async Task GetAllWarehousesWhenOneIsRegistredExistngFilter1Max(string filter)
        {
            //Arrange
            var GetAllWarehousesWhenOneIsRegistred = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(GetAllWarehousesWhenOneIsRegistred);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                var warehousesList = arrangeContext.Warehouses.ToList();
                int cnt            = 0;
                foreach (var item in warehousesList)
                {
                    if (0 != cnt)
                    {
                        arrangeContext.Warehouses.Remove(item);
                    }
                    else
                    {
                        cnt = 1;
                    }
                }
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var warehousesList = await sut.GetAllWarehousesByFilterAsync(1, int.MaxValue, filter);

                //Assert
                Assert.AreEqual(0, warehousesList.Count);
            }
        }
        public async Task UpdateWarehouseWithWhiteSpaceNameWhenInvalidID(string warehouseName, int cityID, int countryID, int addressID, bool toSave)
        {
            //Arrange
            var UpdateWarehouseWithWhiteSpaceNameWhenInvalidID = System.Reflection.MethodBase.GetCurrentMethod().Name;

            var options = Utils.GetOptions(UpdateWarehouseWithWhiteSpaceNameWhenInvalidID);

            Utils.SeedDatabase(options);

            using (var arrangeContext = new StoreSystemDbContext(options))
            {
                arrangeContext.Countries.Add(new Country()
                {
                    CountryID = countryID
                });
                arrangeContext.Cities.Add(new City()
                {
                    CityID = cityID
                });
                arrangeContext.Addresses.Add(new Address()
                {
                    AddressID = addressID
                });
                await arrangeContext.SaveChangesAsync();
            }

            using (var context = new StoreSystemDbContext(options))
            {
                var sut = new WarehouseService(context);

                //Act
                var actualWarehouse = await sut.CreateWarehouseAsync(warehouseName, countryID, cityID, addressID, toSave);

                //Assert
                await Assert.ThrowsExceptionAsync <ArgumentException>(async() => await sut.UpdateWarehouseAsync(-actualWarehouse.WarehouseID, "                ", countryID, cityID, addressID, toSave));
            }
        }