Fluent Nhibernate 映射的单元测试

发布于 2024-10-31 05:47:57 字数 1369 浏览 2 评论 0原文

我正在尝试了解社区的其他成员如何测试他们的 Fluent Nhibernate 映射。假设我有以下映射:

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

您会为这些编写哪些单元测试?您会使用 PersistenceSpecification 类来对这些进行单元测试吗?

编辑:

我想使用 SqlLite,但是如果我不从映射生成架构怎么办?我仍然可以将架构加载到 SqlLite 中吗?我还想知道测试 SqlLite 是否真的足够。我们的产品至少需要在 MS SQL 和 Oracle 上运行。仅在 SqlLite 数据库上进行测试是否可以满足我的要求?另外,您通常是否测试已映射的每个实体(构造函数、属性等)?

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

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

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

发布评论

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

评论(1

笨笨の傻瓜 2024-11-07 05:47:57

Fluent nhibernate 有内置测试方法。使用它们,您可以执行以下操作

[Test]
public void CanCorrectlyMapEmployee()
{
    new PersistenceSpecification<Employee>(session)
        .CheckProperty(c => c.Id, 1)
        .CheckProperty(c => c.FirstName, "John")
        .CheckProperty(c => c.LastName, "Doe")
        .VerifyTheMappings();
}

此测试将

  • 创建一个 Employee 实例
  • ,将 Employee 插入数据库中
  • ,将记录检索到新的 Employee 实例中,
  • 验证检索到的 Employee 是否与原始实例匹配,

这将检查属性映射。

另外,我建议使用 SqlLite 并在内存中测试真实的 SQL 查询来验证级联规则。

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.

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