|
1 | 1 | package org.grails.datastore.gorm |
2 | 2 |
|
3 | 3 | import grails.gorm.annotation.AutoTimestamp |
| 4 | +import org.grails.datastore.gorm.events.AutoTimestampEventListener |
| 5 | + |
4 | 6 | import static grails.gorm.annotation.AutoTimestamp.EventType.*; |
5 | 7 | import grails.gorm.tests.GormDatastoreSpec |
6 | 8 | import grails.persistence.Entity |
@@ -30,6 +32,65 @@ class CustomAutoTimestampSpec extends GormDatastoreSpec { |
30 | 32 | r.modified != null && previousModified < r.modified |
31 | 33 | previousCreated == r.created |
32 | 34 | } |
| 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 | + |
33 | 94 | @Override |
34 | 95 | List getDomainClasses() { |
35 | 96 | [RecordCustom] |
|
0 commit comments