|
1 | 1 | package grails.logical.delete |
2 | 2 |
|
3 | 3 | import app.Person |
| 4 | +import spock.lang.Narrative |
4 | 5 | import spock.lang.Specification |
| 6 | +import spock.lang.Title |
5 | 7 |
|
6 | 8 | import grails.gorm.transactions.Rollback |
7 | 9 | import grails.logical.delete.test.PersonTestData |
8 | 10 | import grails.testing.mixin.integration.Integration |
9 | 11 |
|
10 | | -/** |
11 | | - * This test suite focuses on how the deleted field in a LogicalDelete |
12 | | - * implementation gets changed from overridden delete() operations. |
13 | | - */ |
14 | 12 | @Integration |
| 13 | +@Title('Verify LogicalDelete operations') |
| 14 | +@Narrative('This specification focuses on the behavior of the delete and undelete methods in the LogicalDelete implementation.') |
15 | 15 | class LogicalDeleteSpec extends Specification implements PersonTestData { |
16 | 16 |
|
17 | | - /******************* delete tests - (w/ get) ***********************************/ |
18 | | - |
19 | 17 | @Rollback |
20 | | - void 'test logical delete - get'() { |
21 | | - when: |
| 18 | + void 'get(id) after logical delete works'() { |
| 19 | + |
| 20 | + when: 'getting a non-deleted person' |
22 | 21 | def p = Person.get(1) |
23 | 22 |
|
24 | | - then: |
| 23 | + then: 'person is not marked as deleted' |
25 | 24 | !p.deleted |
26 | 25 |
|
27 | | - when: |
| 26 | + when: 'deleting the person logically' |
28 | 27 | p.delete(flush: true) |
29 | 28 | p = Person.withDeleted { Person.get(1) } as Person |
30 | 29 |
|
31 | | - then: |
| 30 | + then: 'person is marked as deleted' |
32 | 31 | p.deleted |
33 | 32 | } |
34 | 33 |
|
35 | 34 | @Rollback |
36 | | - void 'test logical hard delete - get'() { |
37 | | - when: |
| 35 | + void 'get(id) after hard delete works'() { |
| 36 | + |
| 37 | + given: 'three persons in the database' |
| 38 | + assert Person.count() == 3 |
| 39 | + |
| 40 | + when: 'getting a non-deleted person' |
38 | 41 | def p = Person.get(1) |
39 | 42 |
|
40 | | - then: |
| 43 | + then: 'the person is not marked as deleted' |
41 | 44 | !p.deleted |
42 | 45 |
|
43 | | - when: |
| 46 | + when: 'deleting the person hard' |
44 | 47 | p.delete(flush: true, hard: true) |
| 48 | + def count = Person.withDeleted { Person.count() } |
45 | 49 |
|
46 | | - then: |
47 | | - Person.count() == 2 // 2 left after one hard deleted |
| 50 | + then: 'the person is removed from the database' |
| 51 | + count == 2 |
48 | 52 | } |
49 | 53 |
|
50 | | - /******************* delete tests - (w/ load) ***********************************/ |
51 | | - |
52 | 54 | @Rollback |
53 | | - void 'test logical delete - load'() { |
54 | | - when: |
| 55 | + void 'load(id) after logical delete works'() { |
| 56 | + |
| 57 | + when: 'loading a non-deleted person' |
55 | 58 | def p = Person.load(1) |
56 | 59 |
|
57 | | - then: |
| 60 | + then: 'the person is not marked as deleted' |
58 | 61 | !p.deleted |
59 | 62 |
|
60 | | - when: |
| 63 | + when: 'deleting the person logically' |
61 | 64 | p.delete(flush: true) |
62 | 65 | p = Person.withDeleted { Person.load(1) } as Person |
63 | 66 |
|
64 | | - then: |
| 67 | + then: 'person is marked as deleted' |
65 | 68 | p.deleted |
66 | 69 | } |
67 | 70 |
|
68 | 71 | @Rollback |
69 | | - void 'test logical hard delete - load'() { |
70 | | - when: |
| 72 | + void 'load(id) after hard delete works'() { |
| 73 | + |
| 74 | + given: 'three persons in the database' |
| 75 | + assert Person.count() == 3 |
| 76 | + |
| 77 | + when: 'loading a non-deleted person' |
71 | 78 | def p = Person.load(1) |
72 | 79 |
|
73 | | - then: |
| 80 | + then: 'the person is not marked as deleted' |
74 | 81 | !p.deleted |
75 | 82 |
|
76 | | - when: |
| 83 | + when: 'deleting the person hard' |
77 | 84 | p.delete(flush: true, hard: true) |
| 85 | + def count = Person.withDeleted { Person.count() } |
78 | 86 |
|
79 | | - then: |
80 | | - Person.count() == 2 // 2 left after one hard deleted |
| 87 | + then: 'the person is removed from the database' |
| 88 | + count == 2 |
81 | 89 | } |
82 | 90 |
|
83 | | - /******************* delete tests - (w/ proxy) ***********************************/ |
84 | | - |
85 | 91 | @Rollback |
86 | | - void 'test logical delete - proxy'() { |
87 | | - when: |
| 92 | + void 'proxy(id) after logical delete works'() { |
| 93 | + |
| 94 | + when: 'getting a proxy for a non-deleted person' |
88 | 95 | def p = Person.proxy(1) |
89 | 96 |
|
90 | | - then: |
| 97 | + then: 'the person is not marked as deleted' |
91 | 98 | !p.deleted |
92 | 99 |
|
93 | | - when: |
| 100 | + when: 'deleting the person logically' |
94 | 101 | p.delete(flush: true) |
95 | 102 | p = Person.withDeleted { Person.proxy(1) } as Person |
96 | 103 |
|
97 | | - then: |
| 104 | + then: 'person is marked as deleted' |
98 | 105 | p.deleted |
99 | 106 | } |
100 | 107 |
|
101 | 108 | @Rollback |
102 | | - void 'test logical hard delete - proxy'() { |
103 | | - when: |
| 109 | + void 'proxy(id) after hard delete works'() { |
| 110 | + |
| 111 | + given: 'three persons in the database' |
| 112 | + assert Person.count() == 3 |
| 113 | + |
| 114 | + when: 'getting a proxy for a non-deleted person' |
104 | 115 | def p = Person.proxy(1) |
105 | 116 |
|
106 | | - then: |
| 117 | + then: 'the person is not marked as deleted' |
107 | 118 | !p.deleted |
108 | 119 |
|
109 | | - when: |
| 120 | + when: 'deleting the person hard' |
110 | 121 | p.delete(flush: true, hard: true) |
| 122 | + def count = Person.withDeleted { Person.count() } |
111 | 123 |
|
112 | | - then: |
113 | | - Person.count() == 2 // 2 left after one hard deleted |
| 124 | + then: 'the person is removed from the database' |
| 125 | + count == 2 // 2 left after one hard deleted |
114 | 126 | } |
115 | 127 |
|
116 | | - /******************* delete tests - (w/ read) ***********************************/ |
117 | | - |
118 | 128 | @Rollback |
119 | | - void 'test logical delete - read'() { |
120 | | - when: |
| 129 | + void 'read(id) after logical delete works'() { |
| 130 | + |
| 131 | + when: 'reading a non-deleted person' |
121 | 132 | def p = Person.read(1) |
122 | 133 |
|
123 | | - then: |
| 134 | + then: 'person is not marked as deleted' |
124 | 135 | !p.deleted |
125 | 136 |
|
126 | | - when: |
| 137 | + when: 'deleting the person logically' |
127 | 138 | p.delete(flush: true) |
128 | 139 | p = Person.withDeleted { Person.read(1) } as Person |
129 | 140 |
|
130 | | - then: |
| 141 | + then: 'person is marked as deleted' |
131 | 142 | p.deleted |
132 | 143 | } |
133 | 144 |
|
134 | 145 | @Rollback |
135 | | - void 'test logical hard delete - read'() { |
136 | | - when: |
| 146 | + void 'read(id) after hard delete works'() { |
| 147 | + |
| 148 | + given: 'three persons in the database' |
| 149 | + assert Person.count() == 3 |
| 150 | + |
| 151 | + when: 'reading a non-deleted person' |
137 | 152 | def p = Person.read(1) |
138 | 153 |
|
139 | | - then: |
| 154 | + then: 'person is not marked as deleted' |
140 | 155 | !p.deleted |
141 | 156 |
|
142 | | - when: |
| 157 | + when: 'deleting the person hard' |
143 | 158 | p.delete(flush: true, hard: true) |
| 159 | + def count = Person.withDeleted { Person.count() } |
144 | 160 |
|
145 | | - then: |
146 | | - Person.count() == 2 // 2 left after one hard deleted |
| 161 | + then: 'the person is removed from the database' |
| 162 | + count == 2 |
147 | 163 | } |
148 | 164 |
|
149 | | - /******************* undelete tests ***********************************/ |
150 | | - |
151 | 165 | @Rollback |
152 | | - void 'test logical undelete'() { |
153 | | - when: |
| 166 | + void 'undelete() works'() { |
| 167 | + |
| 168 | + when: 'getting a non-deleted person' |
154 | 169 | def p = Person.get(1) |
| 170 | + |
| 171 | + and: 'deleting the person logically' |
155 | 172 | p.delete(flush: true) |
156 | 173 | p = Person.withDeleted { Person.get(1) } as Person |
157 | 174 |
|
158 | | - then: |
| 175 | + then: 'person is marked as deleted' |
159 | 176 | p.deleted |
160 | 177 |
|
161 | | - when: |
| 178 | + when: 'undeleting the person' |
162 | 179 | p.undelete(flush: true) |
163 | 180 | p = Person.get(1) |
164 | 181 |
|
165 | | - then: |
| 182 | + then: 'person is no longer marked as deleted' |
166 | 183 | !p.deleted |
167 | 184 | } |
168 | 185 | } |
0 commit comments