public static async Task <Product> CreateValidProduct()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new ProductRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new ProductService(repository, unitOfWork);

            var newBrand = await BrandHelpers.CreateValidBrand();

            var command = new product.Commands.Create
            {
                Name    = "New product",
                Id      = null,
                BrandId = newBrand.Id
            };


            // Create product
            var productController = new ProductController(service);
            await productController.Post(command);


            return(await repository.GetProductFullAsync(command.Id));
        }
Example #2
0
        public async static Task <Store> CreateValidStore()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new StoreRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new StoreService(repository, unitOfWork);

            var command = new store.Commands.Create();

            command.Name = "New store";
            command.Id   = null;

            // Create Store
            var storeController = new StoreController(service);
            var storeId         = await storeController.Post(command);

            // Update store name
            var updateCommand = new store.Commands.SetStoreName();

            updateCommand.Id   = storeId.Value.Id;
            updateCommand.Name = "Test market";

            await storeController.Put(updateCommand);

            return(await repository.GetStoreFullAsync(command.Id));
        }
        public void UpdateSuccessItemTest()
        {
            //Given
            var     databseFactory = new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            Account testAccount;
            var     repository = new EfCoreAccountRepository(databseFactory);

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                testAccount = repository.Save(AccountEntityHelper.CreateEfTestAccount());
                efCoreUnitOfWork.Commit();

                //When
                testAccount.CompanyName = "Updated account";
                repository.Save(testAccount);
                efCoreUnitOfWork.Commit();
            }

            //Then
            var finalDatabaseFactory = new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var finalRepo            = new EfCoreAccountRepository(finalDatabaseFactory);
            var itemToCheck          = finalRepo.GetById(testAccount.AccountId);

            EqualityHelper.PropertyValuesAreEqual(itemToCheck, testAccount, new[] { "LastModified", "Contact" });
        }
Example #4
0
        public void Modify_Post()
        {
            InitializeInMemoryDbContext(options =>
            {
                using (var uowManager = new EfCoreUnitOfWorkManager())
                {
                    var repository = new PostRepository(new BloggingDbContext(options));

                    repository.Count().ShouldBe(2);

                    var uow = new EfCoreUnitOfWork(
                        ((IRepositoryWithDbContext)repository).GetDbContext()
                        );

                    uowManager.Register(uow);

                    var post = repository.All().First();

                    post.ShouldNotBeNull();

                    var postId = post.Id;

                    post.Title = "已修改";

                    repository.Modify(post);

                    uowManager.Commit();

                    repository.Single(s => s.Id.Equals(postId))
                    .Title
                    .ShouldBe("已修改");
                }
            });
        }
        public void UpdateFailItemTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            Account testAccount;
            var     repository = new EfCoreAccountRepository(databseFactory);

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                testAccount = repository.Save(AccountEntityHelper.CreateEfTestAccount());
                efCoreUnitOfWork.Commit();
            }

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                //When
                testAccount.CompanyName = null;
                repository.Save(testAccount);
                // ReSharper disable once AccessToDisposedClosure
                TestDelegate testDelegate = () => efCoreUnitOfWork.Commit();
                Assert.Throws <DbUpdateException>(testDelegate);
            }

            //Then
            //Get fresh database factory
            var finalDatabaseFactory = new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var finalRepo            = new EfCoreAccountRepository(finalDatabaseFactory);
            var itemToCheck          = finalRepo.GetAll();

            Assert.AreEqual(testAccount.CompanyName, itemToCheck.FirstOrDefault()?.CompanyName, "The company name was updated when it should not have been");
        }
        public void GetAllSuccessTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                var repository  = new EfCoreAccountRepository(databseFactory);
                var listOfItems = AccountEntityHelper.CreateEfCoreTestAccounts(3);
                listOfItems[0].CompanyName = "1";
                listOfItems[1].CompanyName = "2";
                listOfItems[2].CompanyName = "3";

                repository.AddRange(listOfItems);
                efCoreUnitOfWork.Commit();

                //When
                var allItems = repository.GetAll();

                //Then
                EqualityHelper.AssertListsAreEqual(allItems.OrderBy(x => x.CompanyName).ToList(), listOfItems.OrderBy(x => x.CompanyName).ToList(),
                                                   new[] { "AccountID", "LastModified", "LastModifiedBy", "Contacts" });
            }
        }
        public void GetItemsViaStoredProcedureWithParameterNotParameterisedTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                var repository  = new EfCoreAccountRepository(databseFactory);
                var reference   = "TestReference";
                var listOfItems = GetItemsWithTwoItemsContainingTestReference(reference);
                repository.AddRange(listOfItems);
                efCoreUnitOfWork.Commit();

                //When
                var filter = new SqlParameter("@CompanyName", SqlDbType.VarChar)
                {
                    Value = $"%{reference}%"
                };
                var items = repository.ExecuteQuery <Account>("exec GetAccounts @CompanyName", filter).ToList();

                //Then
                EqualityHelper.AssertListsAreEqual(items.OrderBy(x => x.CompanyName).ToList(), listOfItems.OrderBy(x => x.CompanyName).Take(2).ToList(), new[] { "AccountID", "LastModified", "LastModifiedBy", "Contacts" });
            }
        }
Example #8
0
        public void Create_New_Blogs()
        {
            UseInMemoryDbContext(options =>
            {
                using (var uowManager = new EfCoreUnitOfWorkManager())
                {
                    var repository = new EfCoreRepositoryBase <BloggingDbContext, Blog>(new BloggingDbContext(options));

                    var uow = new EfCoreUnitOfWork(
                        ((IRepositoryWithDbContext)repository).GetDbContext()
                        );

                    uowManager.Register(uow);

                    var blog = new Blog
                    {
                        Name         = "R's blog",
                        Url          = "https://www.cnblogs.com/rajesh",
                        CreationTime = DateTime.Now
                    };

                    var entity = repository.Create(blog);

                    uowManager.Commit();

                    entity.Id.ShouldBeGreaterThan(0);
                }
            });
        }
Example #9
0
        public static async Task <Store> CreateValidStore()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new StoreRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new StoreService(repository, unitOfWork);

            var newLocation = await LocationHelpers.CreateValidLocation();

            var command = new store.Commands.Create
            {
                Name       = "New store",
                Id         = null,
                LocationId = newLocation.Id
            };


            // Create Store
            var storeController = new StoreController(service);
            await storeController.Post(command);


            return(await repository.GetAsync(command.Id));
        }
Example #10
0
        public DbTests()
        {
            var options = new DbContextOptionsBuilder <EfCoreContext>()
                          .UseInMemoryDatabase("MOP.Data.Tests").Options;

            var context = new EfCoreContext(options);

            SeedAsync(context).GetAwaiter().GetResult();
            _unitOfWork = new EfCoreUnitOfWork(context);
        }
Example #11
0
        public static async Task UpdateCityName(CityId id, string name)
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new CityRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new CityService(repository, unitOfWork);
            var cityController   = new CityController(service);

            var updateCommand = new Commands.SetCityName {
                Id = id, Name = name
            };

            await cityController.Put(updateCommand);
        }
Example #12
0
        public async static Task RemoveStore(StoreId id)
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new StoreRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new StoreService(repository, unitOfWork);
            var storeController  = new StoreController(service);

            var updateCommand = new store.Commands.DeleteStore();

            updateCommand.Id = id;

            await storeController.DeleteStore(updateCommand);
        }
Example #13
0
        public async static Task UpdateStore(PurchaseTransactionId id, Store store)
        {
            var connectionString      = ConnectivityService.GetConnectionString("TEMP");
            var context               = new SplurgeStopDbContext(connectionString);
            var repository            = new PurchaseTransactionRepository(context);
            var unitOfWork            = new EfCoreUnitOfWork(context);
            var service               = new PurchaseTransactionService(repository, unitOfWork);
            var transactionController = new PurchaseTransactionController(service);

            var updateCommand = new transaction.Commands.SetPurchaseTransactionStore();

            updateCommand.Id      = id;
            updateCommand.StoreId = store.Id;

            await transactionController.Put(updateCommand);
        }
        public static async Task RemoveProduct(ProductId id)
        {
            var connectionString  = ConnectivityService.GetConnectionString("TEMP");
            var context           = new SplurgeStopDbContext(connectionString);
            var repository        = new ProductRepository(context);
            var unitOfWork        = new EfCoreUnitOfWork(context);
            var service           = new ProductService(repository, unitOfWork);
            var productController = new ProductController(service);

            var updateCommand = new product.Commands.DeleteProduct
            {
                Id = id
            };

            await productController.DeleteProduct(updateCommand);
        }
        public static async Task RemoveCountry(CountryId id)
        {
            var connectionString  = ConnectivityService.GetConnectionString("TEMP");
            var context           = new SplurgeStopDbContext(connectionString);
            var repository        = new CountryRepository(context);
            var unitOfWork        = new EfCoreUnitOfWork(context);
            var service           = new CountryService(repository, unitOfWork);
            var countryController = new CountryController(service);

            var updateCommand = new Commands.DeleteCountry
            {
                Id = id
            };

            await countryController.DeleteCountry(updateCommand);
        }
        public static async Task <dynamic> CreateInvalidBrand()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new BrandRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new BrandService(repository, unitOfWork);

            var command = new Commands.Create {
                Id = null
            };

            var brandController = new BrandController(service);

            return(await brandController.Post(command));
        }
        public static async Task <Brand> CreateValidBrand()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new BrandRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new BrandService(repository, unitOfWork);

            var command = new Commands.Create {
                Name = "Levi's", Id = null
            };

            var brandController = new BrandController(service);
            var brand           = await brandController.Post(command);

            return(await repository.GetAsync(brand.Value.Id));
        }
Example #18
0
        public async static Task <dynamic> CreateInvalidStore()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new StoreRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new StoreService(repository, unitOfWork);

            var command = new store.Commands.Create();

            command.Id = null;

            // Create Store
            var storeController = new StoreController(service);

            return(await storeController.Post(command));
        }
Example #19
0
        public static async Task UpdatePurchaseDate(PurchaseTransactionId id, DateTime date)
        {
            var connectionString      = ConnectivityService.GetConnectionString("TEMP");
            var context               = new SplurgeStopDbContext(connectionString);
            var repository            = new PurchaseTransactionRepository(context);
            var unitOfWork            = new EfCoreUnitOfWork(context);
            var service               = new PurchaseTransactionService(repository, unitOfWork);
            var transactionController = new PurchaseTransactionController(service);

            var updateCommand = new Commands.SetPurchaseTransactionDate
            {
                Id = id,
                TransactionDate = date
            };

            await transactionController.Put(updateCommand);
        }
        public static async Task UpdateProductType(ProductId id, ProductType productType)
        {
            var connectionString  = ConnectivityService.GetConnectionString("TEMP");
            var context           = new SplurgeStopDbContext(connectionString);
            var repository        = new ProductRepository(context);
            var unitOfWork        = new EfCoreUnitOfWork(context);
            var service           = new ProductService(repository, unitOfWork);
            var productController = new ProductController(service);

            var updateCommand = new product.Commands.ChangeProductType
            {
                Id            = id,
                ProductTypeId = productType.Id
            };

            await productController.Put(updateCommand);
        }
Example #21
0
        public static async Task UpdateSizeAmount(size.SizeId id, string amount)
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new SizeRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new size.SizeService(repository, unitOfWork);
            var sizeController   = new SizeController(service);

            var updateCommand = new size.Commands.SetSizeAmount
            {
                Id     = id,
                Amount = amount
            };

            await sizeController.Put(updateCommand);
        }
Example #22
0
        public static async Task UpdateStoreLocation(StoreId id, Location location)
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new StoreRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new StoreService(repository, unitOfWork);
            var storeController  = new StoreController(service);

            var updateCommand = new store.Commands.ChangeLocation
            {
                Id       = id,
                Location = location
            };

            await storeController.Put(updateCommand);
        }
        public static async Task UpdateLocationCountry(LocationId id, Country country)
        {
            var connectionString   = ConnectivityService.GetConnectionString("TEMP");
            var context            = new SplurgeStopDbContext(connectionString);
            var repository         = new LocationRepository(context);
            var unitOfWork         = new EfCoreUnitOfWork(context);
            var service            = new LocationService(repository, unitOfWork);
            var locationController = new LocationController(service);

            var updateCommand = new Commands.ChangeCountry
            {
                Id      = id,
                Country = country
            };

            await locationController.Put(updateCommand);
        }
        public static async Task UpdateProductName(ProductId id, string name, BrandId brandId)
        {
            var connectionString  = ConnectivityService.GetConnectionString("TEMP");
            var context           = new SplurgeStopDbContext(connectionString);
            var repository        = new ProductRepository(context);
            var unitOfWork        = new EfCoreUnitOfWork(context);
            var service           = new ProductService(repository, unitOfWork);
            var productController = new ProductController(service);

            var updateCommand = new product.Commands.UpdateProduct
            {
                Id      = id,
                Name    = name,
                BrandId = brandId
            };

            await productController.Put(updateCommand);
        }
Example #25
0
        public static async Task <PurchaseTransactionId> CreateFullValidPurchaseTransaction()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new PurchaseTransactionRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new PurchaseTransactionService(repository, unitOfWork);

            var command = new Commands.CreateFull
            {
                Id = null,
                TransactionDate = DateTime.Now
            };

            // Add Store
            var store = await StoreHelpers.CreateValidStore();

            command.StoreId = store.Id;

            // Get Product
            var prod = await ProductHelpers.CreateValidProduct();

            // Add one LineItem
            var newLineItem = new LineItemStripped
            {
                Booking                = Booking.Credit,
                Price                  = "1.23",
                CurrencyCode           = "EUR",
                CurrencySymbol         = "€",
                CurrencySymbolPosition = CurrencySymbolPosition.End,
                Notes                  = "New notes",
                ProductId              = prod.Id
            };

            command.LineItems = new List <LineItemStripped>
            {
                newLineItem
            };

            var transactionController = new PurchaseTransactionController(service);
            await transactionController.Post(command);

            return(command.Id);
        }
Example #26
0
        public static async Task <ProductType> CreateValidProductType()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new ProductTypeRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new ProductTypeService(repository, unitOfWork);

            var command = new Commands.Create
            {
                Name = "trousers",
                Id   = null
            };

            var productTypeController = new ProductTypeController(service);
            var productType           = await productTypeController.Post(command);

            return(await repository.GetAsync(productType.Value.Id));
        }
Example #27
0
        public static async Task <PurchaseTransactionId> CreateValidPurchaseTransaction(decimal price     = 1.00m,
                                                                                        LineItem lineItem = null)
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new PurchaseTransactionRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new PurchaseTransactionService(repository, unitOfWork);

            var command = new Commands.CreateFull();

            command.Id = null;
            command.TransactionDate = DateTime.Now;

            // Add Store
            var store = await StoreHelpers.CreateValidStore();

            command.StoreId = store.Id;

            // Get Product
            var prod = await ProductHelpers.CreateValidProduct();

            // Add one LineItem
            command.LineItems = new List <LineItemStripped>
            {
                new LineItemStripped
                {
                    Price                  = price.ToString(CultureInfo.InvariantCulture),
                    CurrencyCode           = "EUR",
                    CurrencySymbol         = "€",
                    CurrencySymbolPosition = CurrencySymbolPosition.End,
                    Booking                = Booking.Credit,
                    Notes                  = lineItem?.Notes,
                    ProductId              = prod.Id
                }
            };

            // Create PurchaseTransaction
            var transactionController = new PurchaseTransactionController(service);
            await transactionController.Post(command);

            return(command.Id);
        }
        public static async Task <Country> CreateValidCountry()
        {
            var connectionString = ConnectivityService.GetConnectionString("TEMP");
            var context          = new SplurgeStopDbContext(connectionString);
            var repository       = new CountryRepository(context);
            var unitOfWork       = new EfCoreUnitOfWork(context);
            var service          = new CountryService(repository, unitOfWork);

            var command = new Commands.Create
            {
                Name = "Rapture",
                Id   = null
            };

            var countryController = new CountryController(service);
            var country           = await countryController.Post(command);

            return(await repository.GetAsync(country.Value.Id));
        }
        public void AddItemTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var repository = new EfCoreAccountRepository(databseFactory);

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                //When
                var testAccount  = AccountEntityHelper.CreateEfTestAccount();
                var addedAccount = repository.Save(testAccount);
                efCoreUnitOfWork.Commit();

                //Then
                Assert.AreNotEqual(addedAccount.AccountId, 0, "The account ID was not updated.");
                EqualityHelper.PropertyValuesAreEqual(addedAccount, testAccount, new[] { "AccountID" });
            }
        }
        public void RetrieveItemByIdTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var repository = new EfCoreAccountRepository(databseFactory);

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                var addedAccount = repository.Save(AccountEntityHelper.CreateEfTestAccount());
                efCoreUnitOfWork.Commit();

                //When
                var retrievedItem = repository.GetById(addedAccount.AccountId);

                //Then
                EqualityHelper.PropertyValuesAreEqual(retrievedItem, addedAccount);
            }
        }