nhibernate查询逻辑测试

时间:2011-08-03 11:54:26

标签: nhibernate

我正在尝试使用nhibernate对我们的oracle数据库进行设置。 我的目标之一是测试查询逻辑。我开始使用sqlite,但遇到了多对一关系的问题,没有引入异物。认为这是一个sqlite问题我设置了一个新的oracle数据库来测试,但我得到了相同的症状。

当我对填充的数据库运行查询时,它可以正常工作并返回正确延迟加载外部对象的结果,因为它们在代码中被引用。

查询逻辑测试(包含在mstest程序集中)返回空值而不是外部对象。

两个查询之间的差异:

1. The test query creates,saves and flushes the data just before 
   calling the query.
2. The test configuration includes steps needed to export the mapped schema 
   into test database, uses a different default schema and connection string.
3. The test query happens in a mstest unit test assembly.

我已确认:

1. That running query against real database works even when run from the unit test       
   assembly.
2. That the test data is being save in the test database schema.

3. That the foreign key reference is created in the test database.

4. That the primary keys are being created in both tables used in query.

我怀疑在将数据保存到测试数据库时错过了一步,所以我将共享该代码,如果需要,我可以添加配置代码。

        var session = DbSession.Load(ConnModes.UnitTest);

        var uil = new List<UserInfo>();
        uil.Add(new UserInfo { Id = -1, FullName = "Fred Flintstone", LoginName = "fredflint" });
        uil.Add(new UserInfo { Id = -1, FullName = "Barney Ruble", LoginName = "barnrubl" });
        uil.Add(new UserInfo { Id = -1, FullName = "Bam Bam", LoginName = "bambam" });

        foreach (var f in uil)
        {
            session.Save(f);
        }

        var rightNow = DateTime.Now;
        var x = new List<FacilityRouteFormula>();
        x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula1", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow.AddMinutes(5) });
        x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula2", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow.AddMinutes(3) });
        x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula3", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow.AddMinutes(4) });
        x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula4", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow.AddDays(1) });
        x.Add(new FacilityRouteFormula { Id = -1, TrackingFacilityId = 2, Formula = "a formula5", ComponentGroupName = "Comp 0", UserId = 1, CreationDate = rightNow });

        foreach (var f in x)
        {
            session.Save(f);
        }

        session.Flush();

        var uidata2 = (from uid in session.Query<UserInfo>() select uid).ToList();
        var data = (from uid in session.Query<FacilityRouteFormula>() select uid).ToList();

        var q = (from frf in session.Query<FacilityRouteFormula>()
                 where frf.ComponentGroupName == "Comp 0" &&
                 frf.TrackingFacilityId == 2
                 orderby frf.CreationDate descending
                 select frf).ToList();

        Assert.IsNotNull(q[0].User);
        Assert.AreEqual("fredflint", q[0].User.LoginName);

以下是针对“真实”数据库的查询。

        var s = DbSession.Load(ConnModes.Dev );

        var q = (from frf in s.Query<FacilityRouteFormula>()
                 where frf.ComponentGroupName == "Comp 0" &&
                 frf.TrackingFacilityId == 2
                 orderby frf.CreationDate descending
                 select frf).ToList();

按要求映射文件:

FacilityRouteFormula:

<id name="Id"  column="N_FACILITY_ROUTE_FORMULA_ID" type="long"  unsaved-value="-1">
  <generator class="native" >
    <param name="sequence">SEQ_FACILITY_ROUTE_FORMULA</param>
  </generator>
</id>

<property  name="TrackingFacilityId" column="N_TRACKING_FACILITY_ID" type="long" />
<property  name ="ComponentGroupName" column="C_COMPONENT_GROUP_NAME" type="string"/>
<property name="CreationDate" column="D_CREATION_DATE"  type="DateTime"/>
<property name="UserId" column="N_USER_ID" type="int" not-null="true" />
<property name="Formula" column="C_FORMULA" type="string" not-null="true" />

<many-to-one insert="false" update="false" lazy="false" name="User" fetch="select">
  <column name="N_USER_ID" sql-type="NUMBER" not-null="true" />
</many-to-one>

的UserInfo:

<id name="Id"  column="N_USER_ID" type="int" unsaved-value="-1"  >
  <generator class="native" >
    <param name="sequence">SEQ_USER_ID</param>
  </generator>
</id>

<property name="FullName" column="C_USER_FULL_NAME" type="string"  not-null="true" />
<property name="LoginName" column="C_LOGIN_NAME" type="string"  not-null="true"  />

1 个答案:

答案 0 :(得分:1)

我认为您需要清除这样的缓存。

session.Save(f);
session.Flush();
session.Evict(f);