Override Grails dateCreated and lastUpdated for test data only?
Asked Answered
J

2

8

I have several Grails 2.1 domain classes that include dateCreated and lastUpdated fields that GORM manages automatically, eg:

class Person {
    Date dateCreated
    Date lastUpdated
    String name
}

I want Grails to automatically fill in these fields at runtime, but I also want to create some test data where I can manually define the values of these dates. The trouble is that Grails automatically sets the values if these fields with an interceptor even when I have specifically set them.

I have seen this SO question which describes how to allow changes to dateCreated, but I need to change lastUpdated as well. Is this possible?

Josh answered 22/11, 2012 at 15:53 Comment(4)
Did you try using that other method? I assume it didn't work?Multifid
@tim_yates, the method linked in the question allows me to set the dateCreated field manually but not the lastUpdated field.Josh
Did you tested? It seems to disable all timestamps, so lastUpdated is included.Raised
@DanVinton What error do you get? Or is the lastUpdated field just set as per usual?Multifid
J
16

Whoops, my mistake, the approach in the other question does work, but the entity in question was separately being saved somewhere else. It also seems that you need an explicit flush to make things work:

def withAutoTimestampSuppression(entity, closure) {
    toggleAutoTimestamp(entity, false)
    def result = closure()
    toggleAutoTimestamp(entity, true)
    result
}

def toggleAutoTimestamp(target, enabled) {
    def applicationContext = (ServletContextHolder.getServletContext()                                                           
                              .getAttribute(ApplicationAttributes.APPLICATION_CONTEXT))

    def closureInterceptor = applicationContext.getBean("eventTriggeringInterceptor")
    def datastore = closureInterceptor.datastores.values().iterator().next()
    def interceptor = datastore.getEventTriggeringInterceptor()

    def listener = interceptor.findEventListener(target)
    listener.shouldTimestamp = enabled
    null
}

def createTestPerson() {

    def luke = new Person(name: "Luke Skywalker")
    withAutoTimestampSuppression(luke) {

        def lastWeek = new Date().minus(7)
        luke.dateCreated = lastWeek
        luke.lastUpdated = lastWeek
        luke.save(failOnError: true, flush: true)
    }
}
Josh answered 22/11, 2012 at 17:8 Comment(0)
H
3

If it is an integration test you can use an hql update statement to manually set lastUpdated.

Herdic answered 22/5, 2014 at 21:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.