public void CanRetryDeleteUsingNewSession() { //setup - trigger a failure when deleting... Guid customerOneId = DbFixture.InsertCustomer(); var c = Nh.CurrentSession.Get <Customer>(customerOneId); Nh.CurrentSession.Delete(c); //cause transaction to fail Nh.CurrentSession.Save(Om.CreateInvalidCustomer()); CommitSessionExpectingException(); //run test... using (ISession newSession = Nh.CreateSession()) { newSession.Delete(c); newSession.Flush(); } Assert.That(DbFixture.CustomerExists(c), Is.False); }
public static void VerifyIsEquivalentToObjectInDatabase <T>(T entity, EquivalenceComparer comparer) where T : class { using (var session = Nh.CreateSession()) { var claimFreshFromDb = session.Get <T>(Nh.GetIdOf(entity)); AssertAreEquivalent(entity, claimFreshFromDb, comparer); } }
protected void PersistUsingNewSession(params object[] entities) { using (ISession s = Nh.CreateSession()) { foreach (object entity in entities) { s.SaveOrUpdate(entity); } s.BeginTransaction().Commit(); } }
public static Guid Insert(Customer customer) { foreach (var salesRepresentative in customer.CustomerRepresentatives) { if (salesRepresentative.IsNew) { Insert(salesRepresentative); } } return(Nh.InsertIntoDb(customer)); }
/// <summary> /// Use this to verify that the mapping file has mapped an association as mandatory (ie not-null='true') /// </summary> /// <remarks> /// <example> /// This verifies that the association from claim to claimant is mandatory ie claim must have /// a reference to claimant before it can be persisted: /// <c>VerifyInsertThrowsBecausePropertyReferencesUnsavedInstance(claim, x => x.Claimant);</c> /// </example> /// </remarks> /// <typeparam name="T"></typeparam> /// <typeparam name="PropertyT">the type of the property that is being tested</typeparam> /// <param name="objectUnderTest"></param> /// <param name="propertyReferencingUnsavedInstance">expression that references the property that is being tested as a mandatory association</param> public static void VerifyInsertThrowsBecausePropertyReferencesUnsavedInstance <T, PropertyT> (T objectUnderTest, Expression <Func <T, PropertyT> > propertyReferencingUnsavedInstance) { string expectedMsg = String.Format("not-null property references a null or transient value"); var ex = Assert.Throws <PropertyValueException>(() => Nh.InsertIntoDb(objectUnderTest)); Assert.That(ex.Message, Is.StringContaining(expectedMsg)); string propertyName = ReflectionUtil.GetProperty(propertyReferencingUnsavedInstance).Name; Assert.That(ex.Message, Is.StringContaining(propertyName)); }
public void FlushWillPersistChangesMadeAfterCallingSaveAsASeperateUpdate() { Customer customer = Om.CreateCustomer(); Nh.CurrentSession.Save(customer); customer.Name = "New name"; Nh.FlushSessionToDbAndClear(); Assert.That(customer.Name, Is.EqualTo(Nh.GetFromDb <Customer>(customer.Id).Name)); Assert.That(customer.ConcurrencyId, Is.EqualTo(2)); }
public void CanAttachAssociatedObjectToDifferentSessions() { //note: although this is possible would highly recommend against doing this //given Customer customer = Om.CreateCustomer(); Order order1 = Om.CreateOrderWith(customer); Order order2 = Om.CreateOrderWith(customer); Nh.CommitInSameTempSession(session => { DbFixture.Insert(customer); DbFixture.Insert(order1); DbFixture.Insert(order2); }); var order1FromDb = Nh.GetFromDb <Order>(order1.Id); var order2FromDb = Nh.GetFromDb <Order>(order2.Id); NHibernateUtil.Initialize(order1FromDb.Customer); Assert.That(order1FromDb.Customer, Is.SameAs(order2FromDb.Customer), "clarifying assumptions"); //evict all our object we have just loaded Nh.CurrentSession.Clear(); //run test... //attach first order to first session ISession firstSession = Nh.CreateSession(); firstSession.Lock(order1FromDb, LockMode.None); Assert.That(firstSession.ExistsInSession <Order>(order1FromDb.Id), Is.True, "order not attached"); Assert.That(firstSession.ExistsInSession <Customer>(order1FromDb.Customer.Id), Is.False, "customer not expected to attached"); //attach customer to second session ISession secondSession = Nh.CreateSession(); secondSession.Lock(order1FromDb.Customer, LockMode.None); Assert.That(secondSession.ExistsInSession <Customer>(order1FromDb.Customer.Id), Is.True, "customer not attached"); //attach second order to third session ISession thirdSession = Nh.CreateSession(); thirdSession.Lock(order2FromDb, LockMode.None); Assert.That(thirdSession.ExistsInSession <Order>(order2FromDb.Id), Is.True, "order not attached"); Assert.That(secondSession.ExistsInSession <Customer>(order1FromDb.Customer.Id), Is.True, "customer no longer attached to first session"); }
public void NH(string filename) { var testvects = JsonConvert.DeserializeObject <TestVector[]>(File.ReadAllText($"..\\..\\..\\{filename}")); foreach (var vect in testvects) { Nh nh = new Nh(FromHexString(vect.Input.KeyHex)); byte[] hash = nh.ComputeHash(FromHexString(vect.Input.MessageHex)); hash.Should().BeEquivalentTo(FromHexString(vect.HashHex)); } }
public void CanTestWhenAssociationIsLazyLoaded() { //given Customer customer = Om.CreateCustomerWithOneRep(); DbFixture.Insert(customer); //when var loadedCustomer = Nh.GetFromDb <Customer>(customer.Id); //then NhAssert.IsLazyLoaded(loadedCustomer.CustomerRepresentatives); }
public void DisposingOfTransactionWillRollbackTransactionIfNotCommitted() { Customer c = Om.CreateCustomer(); using (Nh.CurrentSession.BeginTransaction()) { Nh.CurrentSession.SaveOrUpdate(c); Nh.FlushSessionToDbAndClear(); } Assert.That(Nh.CurrentSession.Get <Customer>(c.Id), Is.Null, "Customer not found in database"); }
public void CanCountChildEntitiesWithoutLoadingThenIntoSession() { var customer = Nh.CurrentSession.Get <Customer>(_customersInDb[0].Id); Address address = customer.AddressAt(0); using (ISession newSession = Nh.CreateSession()) { newSession.Get <Customer>(customer.Id); Assert.That(newSession.Contains(address), Is.False, "Not testing yet just clarifying assumptions"); const string querySring = "select count(*) from Address as a where a.Customer.Id=:custId"; newSession.CreateQuery(querySring).SetGuid("custId", customer.Id).UniqueResult(); Assert.That(newSession.Contains(address), Is.False, "child object not loaded"); } }
public void FlushWillReachNewObjectsAttachedToAPersistenceInstanceAfterSave() { Customer c = Om.CreateCustomer(); Nh.CurrentSession.SaveOrUpdate(c); //note that customer already associated with session Address a = Om.CreateAddress(); c.AddAddress(a); Nh.FlushSessionToDbAndClear(); var loadedCustomer = Nh.CurrentSession.Get <Customer>(c.Id); Assert.That(loadedCustomer.Addresses.Count, Is.EqualTo(1)); }
public void NHibernateCriteriaSpikesSetup() { Customer c1 = Om.CreateCustomerWithOneAddress(); Customer c2 = Om.CreateCustomerWithOneAddress(); c2.AddAddress(Om.CreateAddress("ME5 8HU")); Customer c3 = Om.CreateCustomer(); c3.Name = "Mervyn Ramos"; Customer c4 = Om.CreateCustomer(); c4.ShortCode = "009"; _customersInDb = new List <Customer>(); _customersInDb.AddRange(new[] { c1, c2, c3, c4 }); Nh.InsertIntoDb(_customersInDb); }
public void CanEvictObjectsContainedWithinAnotherSession() { Guid customerId = DbFixture.InsertCustomer(); var customer = Nh.CurrentSession.Get <Customer>(customerId); using (ISession newSession = Nh.CreateSession()) { newSession.Get <Customer>(customerId); //not testing yet just clarifying assumptions Assert.That(Nh.CurrentSession.Contains(customer), Is.True, "entity in session"); EvictObjectsInCurrentSessionFoundWithin(newSession); } Assert.That(Nh.CurrentSession.Contains(customer), Is.False, "entity has been evicted from session"); }
public void FlushInsideATransactionWillNotBeCommittedUnlessTransactionCommitted() { Customer c = Om.CreateCustomer(); using (ITransaction transaction = Nh.CurrentSession.BeginTransaction()) { Nh.CurrentSession.SaveOrUpdate(c); Nh.FlushSessionToDbAndClear(); Assert.That(Nh.CurrentSession.Get <Customer>(c.Id), Is.Not.Null, "Customer saved in database"); transaction.Rollback(); } Nh.CurrentSession.Clear(); Assert.That(Nh.CurrentSession.Get <Customer>(c.Id), Is.Null, "Customer not found in database"); }
protected void CommitSessionExpectingException() { bool exceptionThrow = false; try { Nh.CurrentSession.BeginTransaction().Commit(); } catch (Exception) { exceptionThrow = true; } finally { Nh.DisposeCurrentSession(); } if (!exceptionThrow) { Assert.Fail("Committing session expected to throw"); } }
public static bool AddressExits(Guid addressID) { return(Nh.ExistsInDb <Address>(addressID)); }
protected virtual void CleanupNHibernateSession() { CurrentSessionContext.Unbind(NHibernateTestContext.CurrentContext.SessionFactory); Nh.DisposeCurrentSession(); }
public static Guid Insert(EntityBase entity) { return(Nh.InsertIntoDb(entity)); }
public static bool CustomerExists(Customer c) { return(Nh.ExistsInDb <Customer>(c.Id)); }