2

测试 NHibernate 映射类的最佳方法是什么?

假设我想测试以下地图:

public QuoteMap()
{
    this.Table("QUOTE");
    this.Id(x => x.Id).Column("QUOTE_ID").GeneratedBy.Sequence("SEQ_QUOTE_ID");
    this.Map(x => x.IsDeleted).Column("IS_DELETED");
    this.References(x => x.Proposal).Column("PROPOSAL_ID");
}

其中Proposaltype 映射到另一个表。

QUOTE 表如下所示:

CREATE TABLE "QUOTE"
  (
    "QUOTE_ID"    NUMBER(18,0) NOT NULL,
    "PROPOSAL_ID" NUMBER(18,0) NOT NULL ENABLE,
    "IS_DELETED"  NUMBER(1,0) DEFAULT 0 NOT NULL ENABLE,

    CONSTRAINT "PK_QUOTE" PRIMARY KEY ("QUOTE_ID"),
    CONSTRAINT "FK_QUOTE_PROPOSAL" FOREIGN KEY ("PROPOSAL_ID") REFERENCES
        "PROPOSAL" ("PROPOSAL_ID") ENABLE
  )

选项1:持久性规范

new PersistenceSpecification<Quote>(session, new CustomEqualityComparer())
    .CheckProperty(c => c.TenantId, 1)
    .CheckProperty(c => c.IsDeleted, false)
    .CheckReference(
        c => c.Proposal,
        new Proposal
            {
                Id = 1,
                IsDeleted = false,
                TenantId = 1,
                VersionNumber = 1,
                OutletId = 1,
                StatusId = "TST"
        })
    .VerifyTheMappings();
transaction.Commit();

...此测试将失败,但出现以下异常:

NHibernate.Exceptions.GenericADOException: 无法插入:[Quote#18][SQL: INSERT INTO QUOTE (IS_DELETED, PROPOSAL_ID, QUOTE_ID) VALUES (?, ?, ?)] ---> Oracle.DataAccess.Client.OracleException: ORA -02291: 违反完整性约束 (PROPOSALOWN.FK_QUOTE_PROPOSAL) - 未找到父键

...因为它依赖于提案记录的Id = 1存在。另一个问题是,如果您恰好映射PropertyAColumnB并且PropertyBColumnA的测试将通过并且不会指出您的错误。

选项 2:原始 SQL 到 INSERT,NHibernate 到 SELECT 现在这将是理想的:您通过发出原始 SQL 语句来插入,例如

INSERT INTO QUOTE (QUOTE_ID, PROPOSAL_ID, IS_DELETED) 
  SELECT SEQ_QUOTE_ID.NextVal, 1, 0 from dual;

然后您使用 Nhibernate 阅读并检查值。问题再次是依赖于 PROPOSAL 记录的存在。插入此测试的提案记录?当然!但是,Proposal 表有另一组 FOREIGN KEYS,因此您可能最终将 ROWS 插入到几十个表中,只是为了测试您的映射……不喜欢!

当然有更好、更简单的方法来测试 NHibernate Maps。你能推荐一个吗?

4

1 回答 1

0

您应该CheckReference用于提案:

.CheckReference(
        c => c.Proposal,
        new Proposal
            {
                IsDeleted = false,
                TenantId = 1,
                VersionNumber = 1,
                OutletId = 1,
                StatusId = "TST"
        })
于 2012-11-22T16:52:44.610 回答