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 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" });
        }
        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 async Task UpdateFailItemTestAsync()
        {
            //Given
            Account testAccount;
            var     repository = new EfCoreAsyncAccountRepository(_databaseFactory);

            using (var efCoreAsyncUnitOfWork = new EfCoreAsyncUnitOfWork <SharedLibraryContext>(_databaseFactory))
            {
                testAccount = await repository.SaveAsync(AccountEntityHelper.CreateEfTestAccount()).ConfigureAwait(true);

                await efCoreAsyncUnitOfWork.CommitAsync().ConfigureAwait(false);
            }

            using (var efCoreAsyncUnitOfWork = new EfCoreAsyncUnitOfWork <SharedLibraryContext>(_databaseFactory))
            {
                //When
                testAccount.CompanyName = null;
                await repository.SaveAsync(testAccount).ConfigureAwait(true);

                // ReSharper disable once AccessToDisposedClosure
                AsyncTestDelegate testDelegate = async() => await efCoreAsyncUnitOfWork.CommitAsync().ConfigureAwait(true);

                Assert.ThrowsAsync <DbUpdateException>(testDelegate);
            }

            //Then
            //GetAsync fresh database factory
            var finalDatabaseFactory = new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var finalRepo            = new EfCoreAsyncAccountRepository(finalDatabaseFactory);
            var itemToCheck          = await finalRepo.GetAllAsync().ConfigureAwait(true);

            Assert.AreEqual(testAccount.CompanyName, itemToCheck.FirstOrDefault()?.CompanyName, "The compAnyAsync name was updated when it should not have been");
        }
        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" });
            }
        }
        public void DeleteNonExistingItemTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            var repository = new EfCoreAccountRepository(databseFactory);

            //When
            var testAccount = AccountEntityHelper.CreateEfTestAccount();
        }
        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 GetByIdExceptionTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var repository = new EfCoreAccountRepository(databseFactory);

            //When
            var shouldBeNull = repository.GetById(-1);

            //Then
            Assert.IsNull(shouldBeNull, "The item was found when it should have never existed.");
        }
        public void GetItemsViaStoredProcedureWithNoParameterButNoAccountsExistNotParameterisedTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            //When
            var repository = new EfCoreAccountRepository(databseFactory);
            var items      = repository.ExecuteQuery <Account>("exec GetAccounts").ToList();

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

            //When
            TestDelegate testDelegate = () => repository.ExecuteQueryAsAction("exec DeleteAllAccountsFake");

            //Then
            Assert.Throws <SqlException>(testDelegate);
        }
        public void GetManyWithWhereClauseButNoneMatchSuccessTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            var repository = new EfCoreAccountRepository(databseFactory);

            //When
            var shouldBeEmpty = repository.GetMany(x => false);

            //Then
            Assert.IsFalse(shouldBeEmpty.Any(), "No item should have been found.");
        }
        public void ExecuteStoredProcedureWithIncorrectQueryTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            //When
            var repository = new EfCoreAccountRepository(databseFactory);
            // ReSharper disable once ReturnValueOfPureMethodIsNotUsed
            TestDelegate testDelegate = () => repository.ExecuteQuery("exec GetAccountsFake").ToList();

            //Then
            Assert.Throws <SqlException>(testDelegate, "The expected exception was not thrown.");
        }
        public void GetNoneSuccessTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            var repository = new EfCoreAccountRepository(databseFactory);

            //When
            var hasNone = repository.None(x => false);

            //Then
            Assert.IsTrue(hasNone, "No items should have been found.");
        }
        public void GetAnyButNoneExistTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            var repository = new EfCoreAccountRepository(databseFactory);

            //When
            var hasAny = repository.Any(x => false);

            //Then
            Assert.IsFalse(hasAny, "No items should have been found.");
        }
        public void GetAutocompleteItemsButNoneExist()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");

            var repository = new EfCoreAccountRepository(databseFactory);

            //When
            var shouldBeEmpty = repository.GetAutoCompleteItems(x => false, 1);

            //Then
            Assert.IsFalse(shouldBeEmpty.Any(), "No item should have been found.");
        }
        public void AddListOfItemsFailsValidationTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var repository  = new EfCoreAccountRepository(databseFactory);
            var listOfItems = AccountEntityHelper.CreateEfCoreTestAccounts(3);

            listOfItems[1].CompanyName = null;

            //When
            TestDelegate testDelegate = () => repository.AddRange(listOfItems);

            //Then
            Assert.Throws <DbUpdateException>(testDelegate);
        }
        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 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);
            }
        }
        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 AddItemFailsValidationTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
            var repository  = new EfCoreAccountRepository(databseFactory);
            var testAccount = AccountEntityHelper.CreateEfTestAccount();

            testAccount.CompanyName = null;
            repository.Save(testAccount);

            using (var efCoreUnitOfWork = new EfCoreUnitOfWork <SharedLibraryContext>(databseFactory))
            {
                //When
                // ReSharper disable once AccessToDisposedClosure
                TestDelegate testDelegate = () => efCoreUnitOfWork.Commit();

                Assert.Throws <DbUpdateException>(testDelegate);
            }
        }
        public void GetItemsViaStoredProcedureWithNoParameterNotParameterisedTest()
        {
            //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
                var items = repository.ExecuteQuery <Account>("exec GetAccounts").ToList();

                //Then
                EqualityHelper.AssertListsAreEqual(items.OrderBy(x => x.CompanyName).ToList(), listOfItems.OrderBy(x => x.CompanyName).ToList(), new[] { "AccountID", "LastModified", "LastModifiedBy", "Contacts" });
            }
        }
        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.");
            }
        }
        public void GetNoneButSomeExistTest()
        {
            //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[2].CompanyName = "TestReferenceOtherValue";
                repository.AddRange(listOfItems);
                efCoreUnitOfWork.Commit();

                //When
                var hasNone = repository.None(x => x.CompanyName.Contains("TestReference"));

                //Then
                Assert.IsFalse(hasNone, "None were found when there should have been some.");
            }
        }
        public void GetManyWithWhereClauseSuccessTest()
        {
            //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 items = repository.GetMany(x => x.CompanyName.Contains(reference));

                //Then
                EqualityHelper.AssertListsAreEqual(items.OrderBy(x => x.CompanyName).ToList(), listOfItems.OrderBy(x => x.CompanyName).Take(2).ToList(), new[] { "AccountID", "LastModified", "LastModifiedBy", "Contacts" });
            }
        }
        public void GetAutoCompleteItemsTest()
        {
            //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 item = repository.GetAutoCompleteItems(x => x.CompanyName.Contains(reference), 1);

                //Then
                EqualityHelper.PropertyValuesAreEqual(item.First(), listOfItems[0], new[] { "AccountID", "LastModified", "LastModifiedBy", "Contacts" });
            }
        }
        public void GetWithWhereClauseSuccessTest()
        {
            //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[2].CompanyName = "TestReferenceOtherValue";
                repository.AddRange(listOfItems);
                efCoreUnitOfWork.Commit();

                //When
                var items = repository.Get(x => x.CompanyName.Contains("TestReference"));

                //Then
                EqualityHelper.PropertyValuesAreEqual(items, listOfItems[2],
                                                      new[] { "AccountID", "LastModified", "LastModifiedBy", "Contacts" });
            }
        }
        public void DeleteItemSuccessTest()
        {
            //Given
            var databseFactory =
                new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");


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

                //When
                repository.Delete(testAccount);
                efCoreUnitOfWork.Commit();

                //Then
                var retrievedAccount = repository.GetById(testAccount.AccountId);
                Assert.IsNull(retrievedAccount, "The account was not deleted.");
            }
        }
        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 Init()
 {
     _databaseFactory = new EfCoreDatabaseFactoryBase <SharedLibraryContext>("SharedLibraryContext");
     _transaction     = _databaseFactory.Get().Database.BeginTransaction();
 }