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" }); } }
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"); }
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" }); } }
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" }); }
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"); }
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."); }
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"); }
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"); }
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); }
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."); }
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."); }
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."); }
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."); }
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."); }
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); }
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); }
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."); } }
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); } }
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" }); } }
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."); } }
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); } }
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" }); } }
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."); } }
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" }); } }
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."); } }
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" }); } }
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" }); } }
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" }); } }
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."); } }
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."); } }