Search code examples
unit-testingc#-4.0fluent-nhibernatefluent-nhibernate-mappinghas-one

Fluent nHibernate Unit Test HasOne mapping


I have situation where I DO require one-to-one relationship thus the usage of HasOne. Yes I do want to have separate table for Patrons and Members. Following are my classes and their corresponding mapping classes.

public class Member
{
    public virtual string ID { get; set; }
    public virtual bool IsRegistered { get; set; }
    public virtual Patron Patron { get; set; }
    public virtual string Reference1 { get; set; }
    public virtual string Reference2 { get; set; }        
}

public class Patron
{
    public virtual string ID { get; set; }
    public virtual string FirstName { get; set; }
    public virtual string LastName { get; set; }
    public virtual string Address { get; set; }
    public virtual int Telephone1 { get; set; }
    public virtual int Telephone2 { get; set; }
    public virtual int Age { get; set; }
    public virtual string Occupation { get; set; }
    public virtual string Gender { get; set; }
    public virtual string Room { get; set; }
}

public class PatronMap : ClassMap<Patron>
{
    public PatronMap()
    {
        Id(x => x.ID);
        Map(x => x.FirstName);
        Map(x => x.LastName);
        Map(x => x.Gender);
        Map(x => x.Age);
        Map(x => x.Address);
        Map(x => x.Occupation);
        Map(x => x.Telephone1);
        Map(x => x.Telephone2);
        Map(x => x.Room);
        Table("Patrons");
    }
}

public class MemberMap : ClassMap<Member>
{
    public MemberMap()
    {
        Id(x => x.ID);
        HasOne(x => x.Patron).PropertyRef(x => x.ID).Constrained();
        Map(x => x.Reference1);
        Map(x => x.Reference2);
        Map(x => x.IsRegistered);
        Table("Members");
    }
 }

I also have a little test, as below, to see if the things are mapped correctly or not.

    [TestMethod]
    public void MemberMap_Create_Success()
    {
        new PersistenceSpecification<Member>( Database.Session, new CustomEqualityComparer() )
        .CheckProperty(x => x.ID, "1")
        //I'm not quite sure about the following mapping check. How do I do HasOne check here? :-(
        .CheckReference(x => x.Patron, new Patron()
        {
            ID = "2",
            FirstName = "Foo",
            LastName = "Bar",
            Gender = "M",
            Age = 59,
            Address = "City, Coutnry",
            Telephone1 = 0123456789,
            Telephone2 = 0987654321,
            Occupation = "Learning Fluent nHibernate",
            Room = "Somewhere"
        })
        .CheckProperty(x => x.Reference1, "Ref1")
        .CheckProperty(x => x.Reference2, "Ref2")
        .CheckProperty(x => x.IsRegistered, true)
        .VerifyTheMappings();
    }

I'm not sure how to test HasOne mapping property in this test. Any help is appreciated. Thanks in advance.


Solution

  • Following is what I did to implement HasOne mapping and unit test. Maps are as followed:

    public class PatronMap : ClassMap<Patron>
    {
        public PatronMap()
        {
            Id(x => x.ID);
            Map(x => x.FirstName);
            Map(x => x.LastName);
            Map(x => x.Gender);
            Map(x => x.Age);
            Map(x => x.Address);
            Map(x => x.Occupation);
            Map(x => x.Telephone1);
            Map(x => x.Telephone2);
            Map(x => x.AshramRoom);
            HasOne(x => x.Member).ForeignKey();
            Table("Patrons");
        }
    }
    
    public class MemberMap : ClassMap<Member>
    {
        public MemberMap()
        {
            Id(x => x.ID);
            Map(x => x.IsRegistered);
            Map(x => x.Reference1);
            Map(x => x.Reference2);
            References(x => x.Patron)
                .Column("PatronID")
                .Unique()
                .UniqueKey("IDX_UniquePatronID");
            Table("Members");
        }
    }
    

    Unit test is as thus:

    public void MemberMap_Create_Success()
    {
        new PersistenceSpecification<Member>( Database.Session, new CustomEqualityComparer() )
        .CheckProperty(x => x.ID, "1")
        .CheckReference(x => x.Patron, new Patron()
        {
            ID = "2",
            FirstName = "Abc",
            LastName = "Xyz",
            Gender = "M",
            Age = 99,
            Address = "Address",
            Telephone1 = 0000000001,
            Telephone2 = 1000000000,
            Occupation = "Occupation",
            AshramRoom = "Room"
        })
        .CheckProperty(x => x.Reference1, "Ref1")
        .CheckProperty(x => x.Reference2, "Ref2")
        .CheckProperty(x => x.IsRegistered, true)
        .VerifyTheMappings();
    }
    

    Hope this helps someone. :-)