Skip to content

Commit a68a7e0

Browse files
committed
Add tests for AutoTimestampEventListener.insertOverwrite
1 parent 8dddc27 commit a68a7e0

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

grails-datastore-gorm-test/src/test/groovy/org/grails/datastore/gorm/CustomAutoTimestampSpec.groovy

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.grails.datastore.gorm
22

33
import grails.gorm.annotation.AutoTimestamp
4+
import org.grails.datastore.gorm.events.AutoTimestampEventListener
5+
46
import static grails.gorm.annotation.AutoTimestamp.EventType.*;
57
import grails.gorm.tests.GormDatastoreSpec
68
import grails.persistence.Entity
@@ -30,6 +32,65 @@ class CustomAutoTimestampSpec extends GormDatastoreSpec {
3032
r.modified != null && previousModified < r.modified
3133
previousCreated == r.created
3234
}
35+
36+
void "Test when the auto timestamp properties are already set, they are overwritten"() {
37+
when:"An entity is persisted"
38+
def r = new RecordCustom(name: "Test")
39+
def now = new Date()
40+
r.created = new Date()
41+
r.modified = r.created
42+
r.save(flush:true, failOnError:true)
43+
session.clear()
44+
r = RecordCustom.get(r.id)
45+
46+
then:"the custom lastUpdated and dateCreated are set"
47+
now < r.modified
48+
now < r.created
49+
50+
when:"An entity is modified"
51+
Date previousCreated = r.created
52+
Date previousModified = r.modified
53+
r.name = "Test 2"
54+
r.save(flush:true)
55+
session.clear()
56+
r = RecordCustom.get(r.id)
57+
58+
then:"the custom lastUpdated property is updated and dateCreated is not"
59+
r.modified != null && previousModified < r.modified
60+
previousCreated == r.created
61+
}
62+
63+
void "Test when the auto timestamp properties are already set, they are not overwritten if config is set"() {
64+
when:"An entity is persisted and insertOverwrite is false"
65+
AutoTimestampEventListener autoTimestampEventListener =
66+
RecordCustom.gormPersistentEntity.mappingContext.eventListeners.find { it.class == AutoTimestampEventListener}
67+
autoTimestampEventListener.insertOverwrite = false
68+
69+
def r = new RecordCustom(name: "Test")
70+
def now = new Date()
71+
r.created = new Date()
72+
r.modified = r.created
73+
r.save(flush:true, failOnError:true)
74+
session.clear()
75+
r = RecordCustom.get(r.id)
76+
77+
then:"the custom lastUpdated and dateCreated are not overwritten"
78+
now == r.modified
79+
now == r.created
80+
81+
when:"An entity is modified"
82+
Date previousCreated = r.created
83+
Date previousModified = r.modified
84+
r.name = "Test 2"
85+
r.save(flush:true)
86+
session.clear()
87+
r = RecordCustom.get(r.id)
88+
89+
then:"the custom lastUpdated property is updated and dateCreated is not"
90+
r.modified != null && previousModified < r.modified
91+
previousCreated == r.created
92+
}
93+
3394
@Override
3495
List getDomainClasses() {
3596
[RecordCustom]

0 commit comments

Comments
 (0)