public virtual void TeatCleanup() { CleanupNHibernateSession(); TestConnectionProvider.CloseDatabase(); EquivalenceComparer.ClearGlobalFilter(); Clock.ResetToSystemClock(); }
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); } }
public void CanInsertCustomer() { //given //0.drop the physical database if already exists AND 1.create the physical database var connectionInfo = new DbConnectionInfo { DatabaseName = "MyFirstNHibernateTest" }; DbMediaBuilder mediaBuilder = DbMediaBuilder.For(DatabaseEngine.MsSql2005, connectionInfo); mediaBuilder.CreateDatabaseMedia(); //2.configure nhibernate IDictionary <string, string> sqlServerConfigs = NhCommonConfigurations.MsSqlServer(connectionInfo); var configuration = new Configuration { Properties = sqlServerConfigs }; configuration.AddAssembly(typeof(Customer).Assembly); //3.create the database schema new SchemaExport(configuration).Execute(true, true, false); //4.setup session factory ISessionFactory sessionFactory = configuration.BuildSessionFactory(); //5.Create a new customer var customer = new Customer("Brian Allen"); //when //6.open a session ISession session = sessionFactory.OpenSession(); //7.make customer a persistent instance (using the session) session.Save(customer); //8.open transaction using (ITransaction tx = session.BeginTransaction()) { //9.flush the session to the database // ?? //10.commit transaction tx.Commit(); } //11.open a new session using (ISession newSession = sessionFactory.OpenSession()) { //12.retrieve persistent customer as stored in the database var customerFromDb = newSession.Get <Customer>(customer.Id); //13.recursively compare all the 'relevant' properties of: // - the customer constructed in the test // - the customer as retrieved from the database EquivalenceComparer comparer = EquivalenceComparer.For <Customer>(); IEnumerable <string> propertiesNotEqual = comparer.PropertiesNotEqual(customer, customerFromDb); Assert.That(propertiesNotEqual, Is.Empty); } }
public virtual void TestInitialize() { EquivalenceComparer.GlobalFilter = EquivalenceComparer.GlobalFilter.Union(NhAssert.PropertiesToIgnoreForDbComparison); _comparer = CreateComparer(); TestConnectionProvider.CloseDatabase(); //just in case //ensure Nh by default is using the same session as the one started by our tests Nh.CurrentSession = CreateNHibernateSession(); }
public void UsingLinqToParseTextFileIntoNestedObjectGraph() { var lines = new[] { "#Titile,Price,Authors", "c# 3 in depth,39.99,John Skeet;C Crowhurst", "Linq in Action,20.00,John Smith;C Crowhurst" }; var query = from line in lines where !line.StartsWith("#") let lineParts = line.Split(',') select new { Title = lineParts[0], Price = Decimal.Parse(lineParts[1]), Authors = (from authorFullName in lineParts[2].Split(';') let authorNameParts = authorFullName.Split(' ') select new { FirstName = authorNameParts[0], LastName = authorNameParts[1] }).ToArray() }; var expected = new[] { new { Title = "c# 3 in depth", Price = 39.99m, Authors = new[] { new { FirstName = "John", LastName = "Skeet" }, new { FirstName = "C", LastName = "Crowhurst" } } }, new { Title = "Linq in Action", Price = 20.00m, Authors = new[] { new { FirstName = "John", LastName = "Smith" }, new { FirstName = "C", LastName = "Crowhurst" } } } }; var actual = query.ToList(); EquivalenceComparer comparer = EquivalenceComparer.For <object>(); Assert.That(comparer.PropertiesNotEqual(actual[0], expected[0]), Is.Empty); Assert.That(comparer.PropertiesNotEqual(actual[1], expected[1]), Is.Empty); }
public static void AssertAreEquivalent <T>(T o1, T o2, EquivalenceComparer comparer) where T : class { if (o1 == null) { Assert.That(o2, Is.Null, "o2 expected to be null"); } if (o2 == null) { Assert.That(o1, Is.Null, "o1 expected to be null"); } Assert.That(o1, Is.Not.SameAs(o2), "only makes sense to compare two different object references"); Assert.That(comparer.PropertiesNotEqual(o1, o2, typeof(T)), Is.Empty); }
private void AssertAreEquivalent <T>(T o, T deserialized) { EquivalenceComparer comparer = EquivalenceComparer.For <T>(); Assert.That(comparer.Equals(o, deserialized), Is.True); }
protected virtual EquivalenceComparer CreateComparer() { return(EquivalenceComparer.For <T>()); }
protected void AssertAreEquivalent <TOther>(TOther o1, TOther o2) where TOther : class { NhAssert.AssertAreEquivalent(o1, o2, EquivalenceComparer.For <TOther>()); }
protected void VerifyIsEquivalentToObjectInDatabase <TOther>(TOther entity) where TOther : class { NhAssert.VerifyIsEquivalentToObjectInDatabase(entity, EquivalenceComparer.For <TOther>()); }