Unit Tests for Fluent Nhibernate Mappings

I'm trying to understand how other communities test 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 could you write for them? Could you use the PersistenceSpecification class for unit test?

Edit:

I want to use SqlLite, but what if I do not create my schema from my mappings? Can I still load my schema into SqlLite? I am also wondering if SqlLite testing is enough. Our product should work at least on MS SQL and Oracle. Will testing only in the SqlLite database meet my requirements? Also, do you usually check every object that you have mapped (constructors, properties, etc.)?

+4
source share
1 answer

Free nhibernate has built-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 be

  • create an instance of Employee
  • insert Employee into the database
  • return record to new instance of Employee
  • verify that the received employee matches the original

This will check property mappings.

I would also suggest using SqlLite and testing real SQL queries in memory to check for cascading rules.

+10
source

Source: https://habr.com/ru/post/1346843/


All Articles