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 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 AddListOfItemsTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var repository    = new EfCoreAccountRepository(databseFactory);
            var listOfItems   = AccountEntityHelper.CreateEfCoreTestAccounts(3);
            var originalItems = repository.GetAll();

            //When
            repository.AddRange(listOfItems);

            //Then
            var allNewItems = repository.GetAll();
            var itemsAdded  = allNewItems.Except(originalItems).ToList();

            EqualityHelper.AssertListsAreEqual(itemsAdded, listOfItems,
                                               new[] { "AccountID", "LastModified", "Contacts" });
        }
        public void GetAllButNoneExistTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var repository = new EfCoreAccountRepository(databseFactory);

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

            //Then
            Assert.AreEqual(0, allItems.Count(), "Some items were returned where none should have been");
        }
        public void DeleteWithWhereClauseSuccessTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                var repository    = new EfCoreAccountRepository(databseFactory);
                var listOfItems   = AccountEntityHelper.CreateEfCoreTestAccounts(3);
                var originalItems = repository.GetAll();
                repository.AddRange(listOfItems);
                var allNewItems = repository.GetAll().ToList();
                var itemsAdded  = allNewItems.Except(originalItems).ToList();

                //When
                var idsToDelete = itemsAdded.Select(x => x.AccountId);
                repository.Delete(x => idsToDelete.Contains(x.AccountId));
                efCoreUnitOfWork.Commit();

                //Then
                Assert.AreEqual(0, repository.GetAll().Except(allNewItems).Count(), "The items have not been deleted.");
            }
        }
        public void ExecuteStoredProcedureAsActionTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                var repository  = new EfCoreAccountRepository(databseFactory);
                var listOfItems = AccountEntityHelper.CreateEfCoreTestAccounts(3);
                repository.AddRange(listOfItems);
                efCoreUnitOfWork.Commit();

                //When
                repository.ExecuteQueryAsAction("exec DeleteAllAccounts");

                //Then
                Assert.AreEqual(0, repository.GetAll().Count(), "There are still items in the database.");
            }
        }