How to create a test for Fluent nHibernate?

I am creating a set of conventions for Fluent nHibernate . I create several conventions, such as a primary key, a foreign key, and a many-to-many table.

I would like to be able to test these conventions using an in-memory database to make sure that I have encoded these conventions correctly.

I am currently setting up nHibernate using an in-memory SQlite database as follows:

Configuration configuration = null; var factory = Fluently.Configure() .Database(SQLiteConfiguration.Standard.InMemory().ShowSql()) .Mappings(m => { m.FluentMappings.AddFromAssemblyOf<DataLayer>(); m.FluentMappings.Conventions.AddFromAssemblyOf<DataLayer>(); }) .ExposeConfiguration((c) => configuration = c) .BuildSessionFactory(); var session = factory.OpenSession(); var export = new SchemaExport(configuration); export.Execute(true, true, false, session.Connection, null); 

I am creating a test case, but I do not know how to test the naming convention. How can I run some unit tests in conventions using a Visual Studio test project?

+4
source share
1 answer

What I did is not testing the database, but just checking the generated mapping. So, for example, I have an agreement that says that all foreign keys are written as ID, and I test it as (I use xunit, not mstest, but hopefully you can get the concept ...):

  [Fact] public void AddDefaultConventions_ShouldNameMappingToForeinKeyCorrectly() { var configuration = Fluently.Configure() .Database(MsSqlConfiguration.MsSql2008) .Mappings(m => { m.FluentMappings.Conventions.Add(new CustomForeignKeyConvention()); m.FluentMappings.Add<TestClassMap>(); m.FluentMappings.Add<TestClass2Map>(); }) .BuildConfiguration(); var typeMapping = configuration.GetClassMapping(typeof(TestClass2)); var property = typeMapping.GetProperty("Parent"); Assert.Equal("ParentID", property.ColumnIterator.First().Text); } private class TestClass { public virtual int ID { get; set; } } private class TestClass2 { public virtual int ID { get; set; } public virtual TestClass Parent { get; set; } } private class TestClassMap : ClassMap<TestClass> { public TestClassMap() { Id(x => x.ID); } } private class TestClass2Map : ClassMap<TestClass2> { public TestClass2Map() { Id(x => x.ID); References(x => x.Parent); } } 

Btw. it would not be too difficult to test against the database, just try to select something from TestClass2 and make sure that the exception is not thrown ... but I think the way I showed is simpler, and I believe that when FluentNhibernate can generate the right one NHibernate mapping, NHibernate can generate the right queries for me.

0
source

All Articles