如何为 Fluent nHibernate 约定创建测试?

发布于 2024-09-12 07:03:02 字数 886 浏览 5 评论 0原文

我正在为 Fluent nHibernate 创建一套一组约定。我创建了一些约定,例如主键、外键和多对多表。

我希望能够使用内存数据库测试这些约定,看看我是否正确编码了这些约定。

目前,我正在使用 SQlite 内存数据库设置 nHibernate,如下所示:

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);

我创建一个测试用例,但我不知道测试命名约定。如何使用 Visual Studio 测试项目实现一些约定的单元测试?

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

I would like to be able to test out these conventions with an in memory database to see if I coded these conventions correctly.

Currently, I am setting up nHibernate using an SQlite in-memory database like this:

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 create a test case, but I don't know test a naming convention. How can I achieve some unit tests on a conventions using a Visual Studio test project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清君侧 2024-09-19 07:03:02

我所做的不是针对数据库进行测试,而是只是测试生成的映射。例如,我有一个约定,规定所有外键都像 ID 一样编写,并且我对其进行测试(我使用 xunit 而不是 mstest,但希望您能理解这个概念......):

    [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);
        }
    }

顺便说一句。对数据库进行测试不会太困难,只需尝试从 TestClass2 中选择一些内容并确保不会引发异常...但我认为我展示的方法更简单,并且我相信 FluentNhibernate 可以生成正确的 NHibernate映射,NHibernate 可以为我生成正确的查询。

What I did is not testing against the database, but just test the generated mapping. So for example I have a convention that says that all foreign keys are written like ID and I test it like (I use xunit and 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 wouldn't be too difficult to test against a DB, just try to select something from TestClass2 and make sure no exception is thrown... but I think the way I showed is easier and I trust that when FluentNhibernate can generate the correct NHibernate mapping, NHibernate can generate the correct queries for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文