5

I'm trying to get a feel for how the rest of the community tests their Fluent Nhibernate mappings. So let's say I have the following mappings:

    public UserHeaderMap()
    {
        Table("USER_HEADER");
        Id(x => x.Id, "USER_ID");
        Map(x => x.LoginName, "LOGIN_NAME");
        Map(x => x.UserPassword, "USER_PASSWORD");
        Map(x => x.UserEmail, "USER_EMAIL");
        Map(x => x.UserLanguage, "USER_LANGUAGE");
        Map(x => x.UserEnabled, "USER_ENABLED");

        HasManyToMany(x => x.Groups)
            .Table("USER_GROUP_COMPOSITE")
            .ParentKeyColumn("USER_ID")
            .ChildKeyColumn("GROUP_ID")
            .Cascade.All()
            .Inverse();
    }

public class GroupHeaderMap : ClassMap<GroupHeader>
{
    public GroupHeaderMap()
    {
        Table("GROUP_HEADER");
        Id(x => x.Id, "GROUP_ID");
        Map(x => x.Name, "GROUP_NAME");
        Map(x => x.Description, "GROUP_DESCRIPTION");

        HasManyToMany(x => x.Users)
            .Table("USER_GROUP_COMPOSITE")
            .ParentKeyColumn("GROUP_ID")
            .ChildKeyColumn("USER_ID");
    }
}

What all unit tests would you write for these? Would you use PersistenceSpecification class to unit test these?

Edit:

I want to use SqlLite but what if I'm not generating my schema from my mappings? Can I still load my schema into SqlLite somehow? Also I'm wondering if testing SqlLite is really enough. Our product needs to run on at least MS SQL and Oracle. Will testing only on a SqlLite database meet my requirements? Also do you usually test each and every entity that you have mapped (Constructors, Properties, etc)?

Cole W
  • 15,123
  • 6
  • 51
  • 85

1 Answers1

13

Fluent nhibernate has build-in testing methods. With them you can do the following

[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

This will check property mappings.

Also I would suggest to use SqlLite and test real SQL queries in memory to verify cascading rules.

Sly
  • 15,046
  • 12
  • 60
  • 89
  • I've added additional questions that came to mind when you mentioned this above. – Cole W Apr 06 '11 at 13:45
  • 3
    The only thing I would add is that UNIT testing your Fluent NH mappings is not enough. There are still a million things that can go wrong when you touch the actual db. You should always backup these tests with real integration tests. – Shane Courtrille Apr 06 '11 at 14:39
  • The above URL seems to have died, but I found the content here instead: https://github.com/jagregory/fluent-nhibernate/wiki/Persistence-specification-testing – rohancragg Jun 12 '12 at 13:43
  • What is session ? How to prepare session object ? – Siva Sankaran Dec 16 '21 at 10:24