Unit testing Grails在子集合中获取最新信息-排序不起作用?

Unit testing Grails在子集合中获取最新信息-排序不起作用?,unit-testing,sorting,gorm,Unit Testing,Sorting,Gorm,这里有个麻烦,不确定是代码还是测试 我们需要跟踪对象的状态更改以及这些更改的历史记录。有时只应显示最新状态,有时则需要显示整个历史记录列表 最初,我们认为“小菜一碟”遵循文档(grails.org/doc/2.2.x/ref/Database%20Mapping/sort.html) )这家伙发现了什么(stackoverflow.com/questions/12482995/sort-a-collection-in-grails-by-date) )而且是金色的 来到单元测试时间,我们创建了一

这里有个麻烦,不确定是代码还是测试

我们需要跟踪对象的状态更改以及这些更改的历史记录。有时只应显示最新状态,有时则需要显示整个历史记录列表

最初,我们认为“小菜一碟”遵循文档(grails.org/doc/2.2.x/ref/Database%20Mapping/sort.html) )这家伙发现了什么(stackoverflow.com/questions/12482995/sort-a-collection-in-grails-by-date) )而且是金色的

来到单元测试时间,我们创建了一个测试来创建两个状态并获取最新的状态。答对了,它起作用了。然后,麻烦;每隔一段时间,测试就会失败。所以我们似乎发现排序不起作用。也许我错过了一些明显的东西,希望有一双新的眼睛能看到

class mainObject {
    static hasMany = [ statusHistory : StatusHistory]

    static mapping = {
           sort id: 'asc'
           statusHistory sort:"statusDate"
    }


 String getCurrentStatus(){
     if (!this.statusHistory){
         return""
     }else{
         this.statusHistory.sort{it.sstatusDate}
         return statusHistory.status.first()
     }
 }
}


class statusHistory {

     static mapping = {
        sort statusDate: "asc"
     }

 static belongsTo = [ mainObjects : MainObject]

 Date statusDate
 String strStatus
 String notes

 String toString(){
    if (statusDate ==null){
        return "${strStatus}" + " - No Date"
    }else{
        return "${strStatus}" +" - "+ "${statusDate.getDateString()}"
    }
 }
}
单元测试

@TestMixin(GrailsUnitTestMixin)
class MainObjectTests {

    def util = new UnitTestUtil()
    def mockMainObj

    @Before
    void setUp {

          mockMainObj = util.initMockMainObj()
          mockForConstraintsTests(MainObject, [mockMainObj])
     }

    void testgetCurrentStatus(){
        assertEquals("", mockMainObj.getCurrentStatus())

        def mockObjStatus1 = util.initMockStatus(mockMainObj, new SimpleDateFormat(dd/MM/yyyy hh:mm:ss).parse("01/12/2008 15:00:00"), "First Status")
        mockDomain (StatusHistory, [mockObjStatus1])
        mockForConstaintsTests (StatusHistory, [mockObjStatus1])
        mockObjStatus1.save()
        assertEquals(1, mockMainObj.statusHistory.size())
        assertEquals("First Status", mockMainObj.getCurrentStatus())

        def mockObjStatus2 = util.initMockStatus(mockMainObj, new Date(), "Latest Status")
        mockDomain (StatusHistory, [mockObjStatus2])
        mockForConstaintsTests (StatusHistory, [mockObjStatus2])
        mockObjStatus2.save()
        assertEquals(2, mockMainObj.statusHistory.size())
        assertEquals("Latest Status", mockMainObj.getCurrentStatus())

    }
 }
我感到有点不安的是,GORM、多对一/多以及搜索的性能/扩展似乎存在问题,根据和Beckwith先生(www.infoq.com/presentations/GORM performance)


但我一直在回顾文档,查看我的代码,并认为这是正确的。所以我现在处于循环引用中。非常感谢您的任何帮助。

来自同事的帮助-可能有几种解决方案,但这将是我的方法。不要使用hasMany集合获取第一个值,因为您无法确定顺序(Grails中可能存在此错误)。而是使用单个查询来获取值。尝试用以下方法替换getCurrentStatus()方法:

String getCurrentStatus() {
            StatusHistory.createCriteria().get() {
                            eq(‘mainObjects’, this)

                            maxResults(1)
                            order(‘statusDate, ‘asc’)

                            projections {
                                            property(‘strStatus’)
                            }
            }
}
这将从StatusHistory中仅获取当前Main对象最早的strStatus。它的性能比检索所有历史记录并仅选择第一个历史记录要好

这似乎解决了问题