Exemplo n.º 1
0
        public void GetAllSuccessTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository  = new AccountRepository(databseFactory);
                var listOfItems = AccountEntityHelper.CreateTestAccounts(3);
                listOfItems[0].CompanyName = "1";
                listOfItems[1].CompanyName = "2";
                listOfItems[2].CompanyName = "3";

                repository.AddRange(listOfItems);
                unitOfWork.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" });
            }
        }
Exemplo n.º 2
0
        public void UpdateFailItemTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            Account testAccount;
            var     repository = new AccountRepository(databseFactory);

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                testAccount = repository.Save(AccountEntityHelper.CreateTestAccount());
                unitOfWork.Commit();

                //When
                testAccount.CompanyName = null;
                repository.Save(testAccount);
                // ReSharper disable once AccessToDisposedClosure
                TestDelegate testDelegate = () => unitOfWork.Commit();
                Assert.Throws <DbEntityValidationException>(testDelegate);
            }

            //Then
            //Get fresh database factory
            var finalDatabaseFactory = new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var finalRepo            = new AccountRepository(finalDatabaseFactory);
            var itemToCheck          = finalRepo.GetById(testAccount.AccountID);

            Assert.AreNotEqual(testAccount.CompanyName, itemToCheck.CompanyName, "The company name was updated when it should not have been");
        }
Exemplo n.º 3
0
        public void GetItemsViaStoredProcedureWithParameterNotParameterisedTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository  = new AccountRepository(databseFactory);
                var reference   = "TestReference";
                var listOfItems = GetItemsWithTwoItemsContainingTestReference(reference);
                repository.AddRange(listOfItems);
                unitOfWork.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" });
            }
        }
Exemplo n.º 4
0
        public void UpdateSuccessItemTest()
        {
            //Given
            var     databseFactory = new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            Account testAccount;
            var     repository = new AccountRepository(databseFactory);

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                testAccount = repository.Save(AccountEntityHelper.CreateTestAccount());
                unitOfWork.Commit();

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

            //Then
            var finalDatabaseFactory = new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var finalRepo            = new AccountRepository(finalDatabaseFactory);
            var itemToCheck          = finalRepo.GetById(testAccount.AccountID);

            EqualityHelper.PropertyValuesAreEqual(itemToCheck, testAccount, new[] { "LastModified", "Contacts" });
        }
Exemplo n.º 5
0
        public void DatabaseFactoryCreateTest()
        {
            //Given
            var databaseFactory = new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            //When
            var databaseContext = databaseFactory.Get();

            //Then
            Assert.IsNotNull(databaseContext?.Database, "The database has not been returned from the factory");
        }
Exemplo n.º 6
0
        public void GetByIdExceptionTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var repository = new AccountRepository(databseFactory);

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

            //Then
            Assert.IsNull(shouldBeNull, "The item was found when it should have never existed.");
        }
Exemplo n.º 7
0
        public void GetItemsViaStoredProcedureWithNoParameterButNoAccountsExistNotParameterisedTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

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

            //Then
            Assert.AreEqual(0, items.Count, "Some items were returned where none should have been");
        }
Exemplo n.º 8
0
        public void GetAllButNoneExistTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var repository = new AccountRepository(databseFactory);

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

            //Then
            Assert.AreEqual(0, allItems.Count(), "Some items were returned where none should have been");
        }
Exemplo n.º 9
0
        public void ExecuteStoredProcedureAsActionWithBrokenSyntaxTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var repository = new AccountRepository(databseFactory);

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

            //Then
            Assert.Throws <SqlException>(testDelegate);
        }
Exemplo n.º 10
0
        public void GetManyWithWhereClauseButNoneMatchSuccessTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            var repository = new AccountRepository(databseFactory);

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

            //Then
            Assert.IsFalse(shouldBeEmpty.Any(), "No item should have been found.");
        }
Exemplo n.º 11
0
        public void ExecuteStoredProcedureWithIncorrectQueryTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

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

            //Then
            Assert.Throws <SqlException>(testDelegate, "The expected exception was not thrown.");
        }
Exemplo n.º 12
0
        public void GetNoneSuccessTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            var repository = new AccountRepository(databseFactory);

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

            //Then
            Assert.IsTrue(hasNone, "No items should have been found.");
        }
Exemplo n.º 13
0
        public void GetAnyButNoneExistTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            var repository = new AccountRepository(databseFactory);

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

            //Then
            Assert.IsFalse(hasAny, "No items should have been found.");
        }
Exemplo n.º 14
0
        public void GetAutocompleteItemsButNoneExist()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            var repository = new AccountRepository(databseFactory);

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

            //Then
            Assert.IsFalse(shouldBeEmpty.Any(), "No item should have been found.");
        }
Exemplo n.º 15
0
        public void DeleteNonExistingItemTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            var repository = new AccountRepository(databseFactory);

            //When
            var          testAccount  = AccountEntityHelper.CreateTestAccount();
            TestDelegate testDelegate = () => repository.Delete(testAccount);

            //Then
            Assert.Throws <InvalidOperationException>(testDelegate);
        }
Exemplo n.º 16
0
        public void AddListOfItemsFailsValidationTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var repository  = new AccountRepository(databseFactory);
            var listOfItems = AccountEntityHelper.CreateTestAccounts(3);

            listOfItems[1].CompanyName = null;

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

            //Then
            Assert.Throws <InvalidOperationException>(testDelegate);
        }
Exemplo n.º 17
0
        public void UnitOfWorkShouldRollbackItemsTest()
        {
            //Given
            var databaseFactory = new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databaseFactory))
            {
                var database          = databaseFactory.Get();
                var countBeforeInsert = database.Accounts.Count();
                database.Accounts.Add(AccountEntityHelper.CreateTestAccount());

                //When
                unitOfWork.Dispose();

                //Then
                var countAfterInsert = database.Accounts.Count();
                Assert.AreEqual(countBeforeInsert, countAfterInsert, "Item was inserted.");
            }
        }
Exemplo n.º 18
0
        public void RetrieveItemByIdTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var repository = new AccountRepository(databseFactory);

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var addedAccount = repository.Save(AccountEntityHelper.CreateTestAccount());
                unitOfWork.Commit();

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

                //Then
                EqualityHelper.PropertyValuesAreEqual(retrievedItem, addedAccount);
            }
        }
Exemplo n.º 19
0
        public void AddItemTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var repository = new AccountRepository(databseFactory);

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                //When
                var testAccount  = AccountEntityHelper.CreateTestAccount();
                var addedAccount = repository.Save(testAccount);
                unitOfWork.Commit();

                //Then
                Assert.AreNotEqual(addedAccount.AccountID, 0, "The account ID was not updated.");
                EqualityHelper.PropertyValuesAreEqual(addedAccount, testAccount, new[] { "AccountID" });
            }
        }
Exemplo n.º 20
0
        public void UnitOfWorkShouldSaveItemsTest()
        {
            //Given
            var databaseFactory = new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            //When
            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databaseFactory))
            {
                var database          = databaseFactory.Get();
                var countBeforeInsert = database.Accounts.Count();
                var account           = database.Accounts.Add(AccountEntityHelper.CreateTestAccount());
                unitOfWork.Commit();
                var countAfterInsert = database.Accounts.Count();

                //Then
                Assert.IsNotNull(account);
                Assert.IsTrue(countBeforeInsert < countAfterInsert, "Item was not inserted.");
            }
        }
Exemplo n.º 21
0
        public void AddItemFailsValidationTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var repository  = new AccountRepository(databseFactory);
            var testAccount = AccountEntityHelper.CreateTestAccount();

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

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                //When
                // ReSharper disable once AccessToDisposedClosure
                TestDelegate testDelegate = () => unitOfWork.Commit();

                Assert.Throws <DbEntityValidationException>(testDelegate);
            }
        }
Exemplo n.º 22
0
        public void GetItemsViaStoredProcedureWithNoParameterNotParameterisedTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository  = new AccountRepository(databseFactory);
                var listOfItems = AccountEntityHelper.CreateTestAccounts(3);
                repository.AddRange(listOfItems);
                unitOfWork.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" });
            }
        }
Exemplo n.º 23
0
        public void ExecuteStoredProcedureAsActionTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository  = new AccountRepository(databseFactory);
                var listOfItems = AccountEntityHelper.CreateTestAccounts(3);
                repository.AddRange(listOfItems);
                unitOfWork.Commit();

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

                //Then
                Assert.AreEqual(0, repository.GetAll().Count(), "There are still items in the database.");
            }
        }
Exemplo n.º 24
0
        public void AddListOfItemsTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");
            var repository    = new AccountRepository(databseFactory);
            var listOfItems   = AccountEntityHelper.CreateTestAccounts(3);
            var originalItems = repository.GetAll();

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                //When
                repository.AddRange(listOfItems);

                //Then
                var allNewItems = repository.GetAll();
                var itemsAdded  = allNewItems.Except(originalItems).ToList();
                EqualityHelper.AssertListsAreEqual(itemsAdded, listOfItems,
                                                   new[] { "AccountID", "LastModified", "Contacts" });
            }
        }
Exemplo n.º 25
0
        public void GetNoneButSomeExistTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository  = new AccountRepository(databseFactory);
                var listOfItems = AccountEntityHelper.CreateTestAccounts(3);
                listOfItems[2].CompanyName = "TestReferenceOtherValue";
                repository.AddRange(listOfItems);
                unitOfWork.Commit();

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

                //Then
                Assert.IsFalse(hasNone, "None were found when there should have been some.");
            }
        }
Exemplo n.º 26
0
        public void GetManyWithWhereClauseSuccessTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository  = new AccountRepository(databseFactory);
                var reference   = "TestReference";
                var listOfItems = GetItemsWithTwoItemsContainingTestReference(reference);
                repository.AddRange(listOfItems);
                unitOfWork.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" });
            }
        }
Exemplo n.º 27
0
        public void GetAutoCompleteItemsTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository  = new AccountRepository(databseFactory);
                var reference   = "TestReference";
                var listOfItems = GetItemsWithTwoItemsContainingTestReference(reference);
                repository.AddRange(listOfItems);
                unitOfWork.Commit();

                //When
                var item = repository.GetAutoCompleteItems(x => x.CompanyName.Contains(reference), 1);

                //Then
                EqualityHelper.PropertyValuesAreEqual(item.First(), listOfItems[0], new[] { "AccountID", "LastModified", "LastModifiedBy", "Contacts" });
            }
        }
Exemplo n.º 28
0
        public void GetWithWhereClauseSuccessTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository  = new AccountRepository(databseFactory);
                var listOfItems = AccountEntityHelper.CreateTestAccounts(3);
                listOfItems[2].CompanyName = "TestReferenceOtherValue";
                repository.AddRange(listOfItems);
                unitOfWork.Commit();

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

                //Then
                EqualityHelper.PropertyValuesAreEqual(items, listOfItems[2],
                                                      new[] { "AccountID", "LastModified", "LastModifiedBy", "Contacts" });
            }
        }
Exemplo n.º 29
0
        public void DeleteItemSuccessTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");


            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                //Given
                var repository  = new AccountRepository(databseFactory);
                var testAccount = repository.Save(AccountEntityHelper.CreateTestAccount());
                unitOfWork.Commit();

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

                //Then
                var retrievedAccount = repository.GetById(testAccount.AccountID);
                Assert.IsNull(retrievedAccount, "The account was not deleted.");
            }
        }
Exemplo n.º 30
0
        public void DeleteWithWhereClauseSuccessTest()
        {
            //Given
            var databseFactory =
                new DatabaseFactoryBase <SharedCommonDatabaseContext>("SharedCommonDatabaseContext");

            using (var unitOfWork = new UnitOfWork <SharedCommonDatabaseContext>(databseFactory))
            {
                var repository    = new AccountRepository(databseFactory);
                var listOfItems   = AccountEntityHelper.CreateTestAccounts(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));
                unitOfWork.Commit();

                //Then
                Assert.AreEqual(0, repository.GetAll().Except(allNewItems).Count(), "The items have not been deleted.");
            }
        }