1 | package gscf |
---|
2 | |
---|
3 | import dbnp.studycapturing.* |
---|
4 | import org.dbnp.gdt.Template |
---|
5 | import org.dbnp.gdt.TemplateFieldType |
---|
6 | |
---|
7 | /** |
---|
8 | * Test the creation of a Sample and its TemplateEntity functionality on data model level |
---|
9 | * |
---|
10 | * @author keesvb |
---|
11 | * @since 20100511 |
---|
12 | * @package dbnp.studycapturing |
---|
13 | * |
---|
14 | * Revision information: |
---|
15 | * $Rev: 1588 $ |
---|
16 | * $Author: s.h.sikkema@gmail.com $ |
---|
17 | * $Date: 2011-03-04 11:30:52 +0000 (vr, 04 mrt 2011) $ |
---|
18 | */ |
---|
19 | |
---|
20 | class SampleTests extends StudyTests { |
---|
21 | |
---|
22 | // This test extends StudyTests, so that we have a test study to assign as a parent study |
---|
23 | |
---|
24 | final String testSampleName = "Test sample @XYZ!" |
---|
25 | final String testSampleTemplateName = "Human blood sample" |
---|
26 | |
---|
27 | final String testSamplingEventName = "Test sampling event" |
---|
28 | final String testSamplingEventTemplateName = "Blood extraction" |
---|
29 | final long testSamplingEventTime = 34534534L |
---|
30 | final long testSamplingEventDuration = 1000L |
---|
31 | final String testEventGroupName = "Test Group" |
---|
32 | |
---|
33 | protected void setUp() { |
---|
34 | super.setUp() |
---|
35 | |
---|
36 | // Retrieve the study that should have been created in StudyTests |
---|
37 | def study = Study.findByTitle(testStudyName) |
---|
38 | assert study |
---|
39 | |
---|
40 | // Look up sample template |
---|
41 | def sampleTemplate = Template.findByName(testSampleTemplateName) |
---|
42 | assert sampleTemplate |
---|
43 | |
---|
44 | // Create sample with the retrieved study as parent |
---|
45 | def sample = new Sample( |
---|
46 | name: testSampleName, |
---|
47 | template: sampleTemplate |
---|
48 | ) |
---|
49 | |
---|
50 | // At this point, the sample should not validate, because it doesn't have a parent study assigned |
---|
51 | assert !sample.validate() |
---|
52 | |
---|
53 | // Add the sample to the retrieved parent study |
---|
54 | study.addToSamples(sample) |
---|
55 | assert study.samples.find { it.name == sample.name} |
---|
56 | |
---|
57 | // Now, the sample should validate |
---|
58 | if (!sample.validate()) { |
---|
59 | sample.errors.each { println it} |
---|
60 | } |
---|
61 | assert sample.validate() |
---|
62 | } |
---|
63 | |
---|
64 | private void addParentSamplingEvent() { |
---|
65 | |
---|
66 | // Retrieve the sample |
---|
67 | def sampleDB = Sample.findByName(testSampleName) |
---|
68 | assert sampleDB |
---|
69 | |
---|
70 | // Retrieve the study that should have been created in StudyTests |
---|
71 | def study = Study.findByTitle(testStudyName) |
---|
72 | assert study |
---|
73 | |
---|
74 | // Look up sampling event template |
---|
75 | def samplingEventTemplate = Template.findByName(testSamplingEventTemplateName) |
---|
76 | assert samplingEventTemplate |
---|
77 | |
---|
78 | // Create parent sampling event |
---|
79 | def samplingEvent = new SamplingEvent( |
---|
80 | startTime: testSamplingEventTime, |
---|
81 | duration: testSamplingEventDuration, |
---|
82 | template: samplingEventTemplate, |
---|
83 | sampleTemplate: Template.findByName(testSampleTemplateName) |
---|
84 | ) |
---|
85 | |
---|
86 | // The SamplingEvent should not validate at this point because it doesn't have a parent study |
---|
87 | assert !samplingEvent.validate() |
---|
88 | |
---|
89 | study.addToSamplingEvents(samplingEvent) |
---|
90 | // It should do fine now |
---|
91 | assert samplingEvent.validate() |
---|
92 | assert samplingEvent.save(flush:true) |
---|
93 | |
---|
94 | // Add sample to the sampling event |
---|
95 | samplingEvent.addToSamples(sampleDB) |
---|
96 | |
---|
97 | // Make sure the sampling event is really the parent event of the sample |
---|
98 | assert sampleDB.parentEvent |
---|
99 | assert sampleDB.parentEvent == samplingEvent |
---|
100 | assert sampleDB.parentEvent.startTime.equals(testSamplingEventTime) |
---|
101 | |
---|
102 | } |
---|
103 | |
---|
104 | private void addParentSubject() { |
---|
105 | |
---|
106 | def sampleDB = Sample.findByName(testSampleName) |
---|
107 | assert sampleDB |
---|
108 | |
---|
109 | // Retrieve the parent study |
---|
110 | def study = Study.findByTitle(testStudyName) |
---|
111 | assert study |
---|
112 | |
---|
113 | def subject = SubjectTests.createSubject(study) |
---|
114 | assert subject |
---|
115 | |
---|
116 | sampleDB.parentSubject = subject |
---|
117 | assert sampleDB.validate() |
---|
118 | assert sampleDB.save() |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | // Commented out by Siemen: functionality to be tested not implemented yet. See |
---|
123 | // comment in Study's contraints section |
---|
124 | // /** |
---|
125 | // * Test whether a study which has orphan (without parent subject/event) samples cannot be published |
---|
126 | // */ |
---|
127 | // void testStudyPublish() { |
---|
128 | // def sampleDB = Sample.findByName(testSampleName) |
---|
129 | // assert sampleDB |
---|
130 | // |
---|
131 | // // Retrieve the parent study |
---|
132 | // def study = Study.findByTitle(testStudyName) |
---|
133 | // assert study |
---|
134 | // |
---|
135 | // // Make sure the study validates at this point |
---|
136 | // assert study.validate() |
---|
137 | // |
---|
138 | // // Try to publish the study, should fail as it has a sample without a parent sampling event |
---|
139 | // study.published = true |
---|
140 | // assert !study.validate() |
---|
141 | // |
---|
142 | // // Add parent sampling event |
---|
143 | // addParentSamplingEvent() |
---|
144 | // |
---|
145 | // // Add parent subject |
---|
146 | // addParentSubject() |
---|
147 | // |
---|
148 | // // Now the study should validate |
---|
149 | // assert study.validate() |
---|
150 | // } |
---|
151 | |
---|
152 | void testSave() { |
---|
153 | |
---|
154 | // Try to retrieve the sample and make sure it's the same |
---|
155 | def sampleDB = Sample.findByName(testSampleName) |
---|
156 | assert sampleDB |
---|
157 | assert sampleDB.name.equals(testSampleName) |
---|
158 | assert sampleDB.template.name.equals(testSampleTemplateName) |
---|
159 | |
---|
160 | println "The sample has parentEvent ${sampleDB.parentEvent.encodeAsHTML()}" |
---|
161 | // A sample without a name should not be saveable |
---|
162 | sampleDB.name = null |
---|
163 | assert !sampleDB.validate() |
---|
164 | |
---|
165 | } |
---|
166 | |
---|
167 | void testDelete() { |
---|
168 | def sampleDB = Sample.findByName(testSampleName) |
---|
169 | assert sampleDB |
---|
170 | sampleDB.delete() |
---|
171 | try { |
---|
172 | sampleDB.save() |
---|
173 | assert false // The save should not succeed since the sample is referenced by a study |
---|
174 | } |
---|
175 | catch(org.springframework.dao.InvalidDataAccessApiUsageException e) { |
---|
176 | sampleDB.discard() |
---|
177 | assert true // OK, correct exception (at least for the in-mem db, for PostgreSQL it's probably a different one...) |
---|
178 | } |
---|
179 | |
---|
180 | // Now, delete the sample from the study samples collection, and then the delete action should be cascaded to the sample itself |
---|
181 | def study = Study.findByTitle(testStudyName) |
---|
182 | assert study |
---|
183 | study.removeFromSamples sampleDB |
---|
184 | |
---|
185 | // Make sure the sample doesn't exist anymore at this point |
---|
186 | assert !Sample.findByName(testSampleName) |
---|
187 | assert Sample.count() == 0 |
---|
188 | assert study.samples.size() == 0 |
---|
189 | } |
---|
190 | |
---|
191 | void testDeleteViaParentSubject() { |
---|
192 | |
---|
193 | def sampleDB = Sample.findByName(testSampleName) |
---|
194 | assert sampleDB |
---|
195 | |
---|
196 | // Retrieve the parent study |
---|
197 | def study = Study.findByTitle(testStudyName) |
---|
198 | assert study |
---|
199 | |
---|
200 | // Add parent subject |
---|
201 | addParentSubject() |
---|
202 | Subject subject = sampleDB.parentSubject |
---|
203 | |
---|
204 | // Use the deleteSubject method |
---|
205 | def msg = study.deleteSubject(subject) |
---|
206 | println msg |
---|
207 | assert study.save() |
---|
208 | |
---|
209 | assert !study.subjects.contains(subject) |
---|
210 | |
---|
211 | assert !Subject.findByName(subject.name) |
---|
212 | assert !Sample.findByName(testSampleName) |
---|
213 | |
---|
214 | assert Subject.count() == 0 |
---|
215 | assert Sample.count() == 0 |
---|
216 | |
---|
217 | } |
---|
218 | |
---|
219 | void testDeleteViaParentSamplingEvent() { |
---|
220 | |
---|
221 | def sampleDB = Sample.findByName(testSampleName) |
---|
222 | assert sampleDB |
---|
223 | |
---|
224 | // Retrieve the parent study |
---|
225 | def study = Study.findByTitle(testStudyName) |
---|
226 | assert study |
---|
227 | |
---|
228 | // Add parent sampling event |
---|
229 | addParentSamplingEvent() |
---|
230 | |
---|
231 | def event = sampleDB.parentEvent |
---|
232 | assert event |
---|
233 | |
---|
234 | // Use the deleteSamplingEvent method |
---|
235 | def msg = study.deleteSamplingEvent(event) |
---|
236 | println msg |
---|
237 | assert study.save() |
---|
238 | |
---|
239 | assert !study.samplingEvents.contains(event) |
---|
240 | |
---|
241 | assert !SamplingEvent.findByStartTime(testSamplingEventTime) |
---|
242 | assert !Sample.findByName(testSampleName) |
---|
243 | |
---|
244 | assert SamplingEvent.count() == 0 |
---|
245 | assert Sample.count() == 0 |
---|
246 | |
---|
247 | } |
---|
248 | |
---|
249 | void testDeleteViaParentEventGroup() { |
---|
250 | |
---|
251 | def sampleDB = Sample.findByName(testSampleName) |
---|
252 | assert sampleDB |
---|
253 | |
---|
254 | // Retrieve the parent study |
---|
255 | def study = Study.findByTitle(testStudyName) |
---|
256 | assert study |
---|
257 | |
---|
258 | // Retrieve the sample's sampling event |
---|
259 | addParentSamplingEvent() |
---|
260 | def event = sampleDB.parentEvent |
---|
261 | assert event |
---|
262 | |
---|
263 | // Add parent subject |
---|
264 | addParentSubject() |
---|
265 | Subject subject = sampleDB.parentSubject |
---|
266 | |
---|
267 | // Create an event group in this study with the sample's sampling event and subject |
---|
268 | def group = new EventGroup( |
---|
269 | name: testEventGroupName |
---|
270 | ) |
---|
271 | study.addToEventGroups(group) |
---|
272 | assert group.validate() |
---|
273 | |
---|
274 | group.addToSubjects(subject) |
---|
275 | group.addToSamplingEvents(event) |
---|
276 | |
---|
277 | assert study.eventGroups.find { it.name == group.name} |
---|
278 | assert group.validate() |
---|
279 | assert study.save() |
---|
280 | |
---|
281 | // Use the deleteEventGroup method |
---|
282 | def msg = study.deleteEventGroup(group) |
---|
283 | println msg |
---|
284 | assert Sample.count() == 0 // trigger (if any) e.ObjectDeletedException: deleted object would be re-saved by cascade (remove deleted object from associations) |
---|
285 | if (!study.validate()) { |
---|
286 | study.errors.each { println it} |
---|
287 | } |
---|
288 | assert study.validate() |
---|
289 | |
---|
290 | assert study.save() |
---|
291 | |
---|
292 | assert !study.eventGroups.contains(group) |
---|
293 | assert !EventGroup.findByName(testEventGroupName) |
---|
294 | assert !Sample.findByName(testSampleName) |
---|
295 | |
---|
296 | assert EventGroup.count() == 0 |
---|
297 | assert Sample.count() == 0 |
---|
298 | |
---|
299 | } |
---|
300 | |
---|
301 | void testStudyRelation() { |
---|
302 | // Retrieve the parent study |
---|
303 | def study = Study.findByTitle(testStudyName) |
---|
304 | assert study |
---|
305 | |
---|
306 | // Test giveSampleTemplates |
---|
307 | def templates = study.giveSampleTemplates() |
---|
308 | assert templates |
---|
309 | assert templates.size() == 1 |
---|
310 | assert templates.asList().first().name == testSampleTemplateName |
---|
311 | |
---|
312 | // Test if the sample is in the samples collection |
---|
313 | assert study.samples |
---|
314 | assert study.samples.size() == 1 |
---|
315 | assert study.samples.first().name == testSampleName |
---|
316 | } |
---|
317 | |
---|
318 | |
---|
319 | void testParentStudy() { |
---|
320 | def sample = Sample.findByName(testSampleName) |
---|
321 | assert sample |
---|
322 | |
---|
323 | assert sample.parent |
---|
324 | assert sample.parent.code == testStudyCode |
---|
325 | } |
---|
326 | |
---|
327 | void testSampleUniqueNameConstraint() { |
---|
328 | def sample = Sample.findByName(testSampleName) |
---|
329 | assert sample |
---|
330 | |
---|
331 | def study = sample.parent |
---|
332 | assert study |
---|
333 | |
---|
334 | def sample2 = new Sample( |
---|
335 | name: testSampleName, |
---|
336 | template: sample.template, |
---|
337 | parentEvent: sample.parentEvent |
---|
338 | ) |
---|
339 | |
---|
340 | // Add the sample to the retrieved parent study |
---|
341 | study.addToSamples(sample2) |
---|
342 | |
---|
343 | // At this point, the sample should not validate or save, because there is already a sample with that name in the study |
---|
344 | assert !sample2.validate() |
---|
345 | assert !sample2.save(flush:true) |
---|
346 | |
---|
347 | } |
---|
348 | |
---|
349 | /** |
---|
350 | * Test whether it's indeed not possible to add two yet-to-be-saved samples with the same name to a yet-to-be-saved study |
---|
351 | */ |
---|
352 | void testSampleUniqueNameConstraintAtValidate() { |
---|
353 | def sample = Sample.findByName(testSampleName) |
---|
354 | assert sample |
---|
355 | |
---|
356 | def study = sample.parent |
---|
357 | assert study |
---|
358 | |
---|
359 | def sample1 = new Sample( |
---|
360 | name: testSampleName + "-double", |
---|
361 | template: sample.template, |
---|
362 | parentEvent: sample.parentEvent |
---|
363 | ) |
---|
364 | |
---|
365 | def sample2 = new Sample( |
---|
366 | name: testSampleName + "-double", |
---|
367 | template: sample.template, |
---|
368 | parentEvent: sample.parentEvent |
---|
369 | ) |
---|
370 | |
---|
371 | // Add the sample to the retrieved parent study |
---|
372 | study.addToSamples(sample1) |
---|
373 | study.addToSamples(sample2) |
---|
374 | |
---|
375 | // At this point, the sample should not validate or save, because there is already a sample with that name in the study |
---|
376 | assert !sample1.validate() |
---|
377 | assert !sample1.save(flush:true) |
---|
378 | |
---|
379 | assert !sample2.validate() |
---|
380 | assert !sample2.save(flush:true) |
---|
381 | } |
---|
382 | |
---|
383 | void testFindViaSamplingEvent() { |
---|
384 | |
---|
385 | // Add parent sampling event |
---|
386 | addParentSamplingEvent() |
---|
387 | |
---|
388 | // Try to retrieve the sampling event by using the time... |
---|
389 | // (should be also the parent study but that's not yet implemented) |
---|
390 | def samplingEventDB = SamplingEvent.findByStartTime(testSamplingEventTime) |
---|
391 | assert samplingEventDB |
---|
392 | |
---|
393 | def samples = samplingEventDB.getSamples() |
---|
394 | assert samples |
---|
395 | assert samples.size() == 1 |
---|
396 | assert samples.every {it.name == testSampleName} |
---|
397 | } |
---|
398 | |
---|
399 | void testDomainFields() { |
---|
400 | def sample = Sample.findByName(testSampleName) |
---|
401 | assert sample |
---|
402 | |
---|
403 | // Make sure the domain fields exist |
---|
404 | assert sample.fieldExists('name') |
---|
405 | assert sample.fieldExists('material') |
---|
406 | |
---|
407 | // Make sure they are domain fields |
---|
408 | assert sample.isDomainField('name') |
---|
409 | assert sample.isDomainField('material') |
---|
410 | |
---|
411 | // Make sure that they have the right type |
---|
412 | assert sample.giveFieldType('name') == TemplateFieldType.STRING |
---|
413 | assert sample.giveFieldType('material') == TemplateFieldType.ONTOLOGYTERM |
---|
414 | |
---|
415 | } |
---|
416 | |
---|
417 | protected void tearDown() { |
---|
418 | super.tearDown() |
---|
419 | } |
---|
420 | |
---|
421 | } |
---|