在这里搔痒,不确定它是代码还是测试。
我们需要跟踪对象的状态更改以及这些更改的历史记录。有时候只显示最新状态,有些则需要整个历史记录列表。
最初我们认为'小菜一碟'遵循文档(grails.org/doc/2.2.x/ref/Database%20Mapping/sort.html )和这家伙发现了什么(stackoverflow.com/questions/12482995/sort-a-collection-in-grails-by-date 并且是金色的。
来到单元测试时间,我们创建了测试以制作两种状态并拉出最新状态。宾果它有效。然后,麻烦;随机间隔,测试失败。所以我们似乎发现what this guy found关于排序不起作用。也许我错过了一些明显的东西,希望一双新眼睛可以看到它。
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())
}
}
根据to this blog和Beckwith先生(www.infoq.com/),GORM,多对一/多人以及搜索的性能/扩展似乎存在问题,我感到有点不安。演示/ GORM-性能)
但是我继续回到文档并查看我的代码并认为它是正确的。所以我现在在循环参考。任何帮助将不胜感激。
答案 0 :(得分:1)
来自同事 - 可能有几个解决方案,但这将是我的方法。不要使用hasMany集合来获取第一个值,因为您无法确定顺序(Grails中可能存在此错误)。而是使用单个查询来获取值。尝试将getCurrentStatus()方法替换为:
String getCurrentStatus() {
StatusHistory.createCriteria().get() {
eq(‘mainObjects’, this)
maxResults(1)
order(‘statusDate, ‘asc’)
projections {
property(‘strStatus’)
}
}
}
这将仅从StatusHistory获取当前MainObject的最早strStatus。它比检索所有历史记录更好,只有选择第一个历史记录。
这似乎已经解决了问题