public void SaveChanges_MustSaveChangesIfBeginChangesWasCalledFirst() { using (var repositoryContext = new RepositoryContext()) { Account expectedAccount = TestEntities.GetTestAccount( 1, Guid.Parse("E51C8347-5F11-40EB-8284-3960E98FEB37"), "RepCont"); repositoryContext.BeginChanges(); try { repositoryContext.AccountRepository.Upsert(expectedAccount); repositoryContext.SaveChanges(); Account actualAccount = repositoryContext.AccountRepository.Get(expectedAccount.AccountId); Assert.NotNull(actualAccount); Assert.True(EntityComparer.AreAccountEqual(expectedAccount, actualAccount)); } finally { if (expectedAccount.AccountId != TestValues.TestId) { repositoryContext.AccountRepository.Delete(expectedAccount.AccountId); } } } }
public void SaveChanges_MustSaveChangesIfRepositoryWasCreatedFirst() { using (var repositoryContext = new RepositoryContext()) { Account expectedAccount = TestEntities.GetTestAccount( 1, Guid.Parse("3DA9A385-A97C-4D63-BD4F-2230BD7B97B2"), "RepCont"); repositoryContext.BeginChanges(); try { repositoryContext.AccountRepository.Upsert(expectedAccount); repositoryContext.SaveChanges(); Account actualAccount = repositoryContext.AccountRepository.Get(expectedAccount.AccountId); Assert.NotNull(actualAccount); Assert.True(EntityComparer.AreAccountEqual(expectedAccount, actualAccount)); } finally { if (expectedAccount.AccountId != TestValues.TestId) { repositoryContext.AccountRepository.Delete(expectedAccount.AccountId); } } } }
public void MakingChanges_MustWorkBothInsideAndOutsideOfTransaction() { using (var repositoryContext = new RepositoryContext()) { Account expectedAccount1 = TestEntities.GetTestAccount( 1, Guid.Parse("DCFEDC8C-8D04-471B-B36B-F106E6BE77DC"), "RepCont"); try { repositoryContext.AccountRepository.Upsert(expectedAccount1); Account actualAccount = repositoryContext.AccountRepository.Get(expectedAccount1.AccountId); Assert.NotNull(actualAccount); Assert.True(EntityComparer.AreAccountEqual(expectedAccount1, actualAccount)); Account expectedAccount2 = TestEntities.GetTestAccount( 1, expectedAccount1?.AccountGuid.Value ?? null, "RepCont"); repositoryContext.BeginChanges(); repositoryContext.AccountRepository.Upsert(expectedAccount2); repositoryContext.SaveChanges(); actualAccount = repositoryContext.AccountRepository.Get(expectedAccount2.AccountId); Assert.NotNull(actualAccount); Assert.True(EntityComparer.AreAccountEqual(expectedAccount2, actualAccount)); } finally { if (expectedAccount1.AccountId != TestValues.TestId) { repositoryContext.AccountRepository.Delete(expectedAccount1.AccountId); } } } }
public void CRUD_Test() { var repositoryContext = new RepositoryContext(); Account expectedAccount = TestEntities.GetTestAccount( 1, Guid.Parse("B2A4DF0B-B0B4-46E6-A308-6DFB79909CD3"), "AccOld"); try { // delete test account TestDataCleaner.DeleteTestAccount(repositoryContext, expectedAccount.AccountId); // create repositoryContext.AccountRepository.Upsert(expectedAccount); // read by id Account actualAccount = repositoryContext.AccountRepository.Get(expectedAccount.AccountId); Assert.IsNotNull(actualAccount); Assert.IsTrue(EntityComparer.AreAccountEqual(expectedAccount, actualAccount)); if (expectedAccount.AccountGuid.HasValue) { // read by guid actualAccount = repositoryContext.AccountRepository.Get(expectedAccount.AccountGuid.Value); Assert.IsNotNull(actualAccount); Assert.IsTrue(EntityComparer.AreAccountEqual(expectedAccount, actualAccount)); } int?actualAccountId; if (expectedAccount.AccountGuid.HasValue) { // read id by guid actualAccountId = repositoryContext.AccountRepository.GetId(expectedAccount.AccountGuid.Value); Assert.AreEqual(expectedAccount.AccountId, actualAccountId); } // read id by account number actualAccountId = repositoryContext.AccountRepository.GetId(expectedAccount.AccountNumber); Assert.AreEqual(expectedAccount.AccountId, actualAccountId); // update Account expectedAccountNew = TestEntities.GetTestAccount(2, expectedAccount.AccountGuid.Value, "AccNew"); repositoryContext.AccountRepository.Upsert(expectedAccountNew); Assert.AreEqual(expectedAccount.AccountId, expectedAccountNew.AccountId); Account actualAccountNew = repositoryContext.AccountRepository.Get(expectedAccountNew.AccountId); Assert.IsNotNull(actualAccountNew); Assert.IsTrue(EntityComparer.AreAccountEqual(expectedAccountNew, actualAccountNew)); // delete repositoryContext.AccountRepository.Delete(expectedAccount.AccountId); // read by id actualAccount = repositoryContext.AccountRepository.Get(expectedAccount.AccountId); Assert.IsNull(actualAccount); if (expectedAccount.AccountGuid.HasValue) { // read by guid actualAccount = repositoryContext.AccountRepository.Get(expectedAccount.AccountGuid.Value); Assert.IsNull(actualAccount); // read id by guid actualAccountId = repositoryContext.AccountRepository.GetId(expectedAccount.AccountGuid.Value); Assert.IsNull(actualAccountId); } // read id by account number actualAccountId = repositoryContext.AccountRepository.GetId(expectedAccount.AccountNumber); Assert.IsNull(actualAccountId); } finally { // delete test account TestDataCleaner.DeleteTestAccount(repositoryContext, expectedAccount.AccountId); repositoryContext.Dispose(); } }