GithubHelp home page GithubHelp logo

ngbrown / nhibernate.persistancetesting Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 1.0 48 KB

Quickly test your NHibernate mappings using the PersistenceSpecification class

License: BSD 3-Clause "New" or "Revised" License

C# 100.00%
nhibernate testing

nhibernate.persistancetesting's Introduction

NHibernate.PersistenceTesting

Build status NuGet

Code based on the testing framework in Fluent NHibernate and adopted to work with vanilla NHibernate. Documentation adapted from Fluent NHibernate wiki.

Downloads

The latest release of NHibernate.PersistenceTesting is available on NuGet.

Usage

You can quickly test your mappings using the PersistenceSpecification<T> class. Let's start with a simple Employee mapping:

public class Employee
{
    public virtual int Id { get; private set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
}

public class EmployeeMap : ClassMapping<Employee>
{
    public EmployeeMap()
    {
        Id(x => x.Id);
        Property(x => x.FirstName);
        Property(x => x.LastName);
    }
}

To test this mapping:

using NHibernate.PersistenceTesting;

[Test]
public void CanCorrectlyMapEmployee()
{
    new PersistenceSpecification<Employee>(session)
        .CheckProperty(c => c.Id, 1)
        .CheckProperty(c => c.FirstName, "John")
        .CheckProperty(c => c.LastName, "Doe")
        .VerifyTheMappings();
}

This test will:

  • create an Employee instance
  • insert the Employee into the database
  • retrieve the record into a new Employee instance
  • verify the retrieved Employee matches the original

Testing references

Let's consider an Employee mapping that includes a reference to a Store:

public class EmployeeMap : ClassMapping<Employee>
{
    public EmployeeMap()
    {
        Id(x => x.Id);
        Property(x => x.FirstName);
        Property(x => x.LastName);
        ManyToOne(x => x.Store);
    }
}

When verifying mappings, the Object.Equals() method is used to compare the retrieved values to the inserted values. This works as expected for primitive types and types that override Object.Equals(). For all other types, such as our Store class, the default implementation of Object.Equals() checks that both values point to the same instance of the object. We will always have different instances because the test flushes and clears the session cache after inserting the record. Instead, we want to verify the correct Store was loaded by comparing the Store's Id (and possibly other properties).

The PersistenceSpecification constructor allows us to pass in an IEqualityComparer to customize how each value is compared.

[Test]
public void CanCorrectlyMapEmployee()
{
    new PersistenceSpecification<Employee>(session, new CustomEqualityComparer())
        .CheckProperty(c => c.Id, 1)
        .CheckProperty(c => c.FirstName, "John")
        .CheckProperty(c => c.LastName, "Doe")
        .CheckReference(c => c.Store, new Store() {Name = "MyStore"})
        .VerifyTheMappings();
}

public class CustomEqualityComparer: IEqualityComparer
{
    public bool Equals(object x, object y)
    {
        if (x == null || y == null)
        {
            return false;
        }
        if (x is Store && y is Store)
        {
            return ((Store) x).Id == ((Store) y).Id;
        }
        return x.Equals(y);
    }

    public int GetHashCode(object obj)
    {
        throw new NotImplementedException();
    }
}

Note that, in order for CheckReferences to work, you either need to pass an already persistence entity, or have cascading on the entities you're testing. Otherwise, the test will fail complaining "object references an unsaved transient instance".

Obviously, the sample provided here won't work because the Store object is transient so here you'll need Cascading to make the test pass (which is omitted for brevity).

Testing lists

CheckList saves each element individually while CheckComponentList relies on cascading or the user saving.

Contributors

For list of contributors, see Fluent NHibernate.

NHibernate.PersistenceTesting is © 2017 Nathan Brown and contributors and © 2008-2015 James Gregory and contributors under the BSD license

Fluent NHibernate is © 2008-2015 James Gregory and contributors under the BSD license

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.