source: trunk/test/integration/gscf/SampleTests.groovy @ 812

Last change on this file since 812 was 812, checked in by keesvb, 10 years ago

added Sample tests for deleting samples via parent sampling event and event group, corrected some small errors, updated BootStrap? to find out why Sample.parentEvent gets serialized into the db, still no luck...

  • Property svn:keywords set to Date Author Rev
File size: 8.7 KB
Line 
1package gscf
2
3import dbnp.studycapturing.Study
4import dbnp.studycapturing.Template
5import grails.test.GrailsUnitTestCase
6import dbnp.studycapturing.SamplingEvent
7import dbnp.studycapturing.Sample
8import dbnp.studycapturing.TemplateFieldType
9import dbnp.studycapturing.Subject
10import dbnp.studycapturing.EventGroup
11
12/**
13 * Test the creation of a Sample and its TemplateEntity functionality on data model level
14 *
15 * @author keesvb
16 * @since 20100511
17 * @package dbnp.studycapturing
18 *
19 * Revision information:
20 * $Rev: 812 $
21 * $Author: keesvb $
22 * $Date: 2010-08-16 16:04:00 +0000 (ma, 16 aug 2010) $
23 */
24
25class SampleTests extends StudyTests {
26
27        // This test extends StudyTests, so that we have a test study to assign as a parent study
28
29        final String testSampleName = "Test sample @XYZ!"
30        final String testSampleTemplateName = "Human blood sample"
31
32        final String testSamplingEventName = "Test sampling event"
33        final String testSamplingEventTemplateName = "Blood extraction"
34        final long testSamplingEventTime = 34534534L
35        final long testSamplingEventDuration = 1000L
36        final String testEventGroupName = "Test Group"
37
38
39        protected void setUp() {
40                super.setUp()
41
42                // Retrieve the study that should have been created in StudyTests
43                def study = Study.findByTitle(testStudyName)
44                assert study
45
46                // Look up sampling event template
47                def samplingEventTemplate = Template.findByName(testSamplingEventTemplateName)
48                assert samplingEventTemplate
49
50                // Look up sample template
51                def sampleTemplate = Template.findByName(testSampleTemplateName)
52                assert sampleTemplate
53
54                // Create parent sampling event
55                def samplingEvent = new SamplingEvent(
56                        startTime: testSamplingEventTime,
57                        duration: testSamplingEventDuration,
58                        template: samplingEventTemplate,
59                        sampleTemplate: sampleTemplate
60                )
61
62                if (!samplingEvent.validate()) {
63                        samplingEvent.errors.each { println it}
64                }
65                // The SamplingEvent should not validate at this point because it doesn't have a parent study
66                assert !samplingEvent.validate()
67
68                study.addToSamplingEvents(samplingEvent)
69                // It should do fine now
70                assert samplingEvent.validate()
71                assert samplingEvent.save(flush:true)
72
73                // Create sample with the retrieved study as parent
74                def sample = new Sample(
75                    name: testSampleName,
76                    template: sampleTemplate,
77                    parentEvent: samplingEvent
78                )
79
80                // At this point, the sample should not validate, because it doesn't have a parent study assigned
81                assert !sample.validate()
82
83                // Add the sample to the retrieved parent study
84                study.addToSamples(sample)
85                assert study.samples.find { it.name == sample.name}
86
87                // Now, the sample should validate
88                if (!sample.validate()) {
89                        sample.errors.each { println it}
90                }
91                assert sample.validate()
92
93                // Make sure the sample is saved to the database
94                assert sample.save(flush: true)
95
96        }
97
98        void testSave() {
99
100                // Try to retrieve the sample and make sure it's the same
101                def sampleDB = Sample.findByName(testSampleName)
102                assert sampleDB
103                assert sampleDB.name.equals(testSampleName)
104                assert sampleDB.template.name.equals(testSampleTemplateName)
105                assert sampleDB.parentEvent
106                assert sampleDB.parentEvent.startTime.equals(testSamplingEventTime)
107
108                println "The sample has parentEvent ${sampleDB.parentEvent.encodeAsHTML()}"
109                // A sample without a name should not be saveable
110                sampleDB.name = null
111                assert !sampleDB.validate()
112
113                // A sample without a parent SamplingEvent should not be saveable
114                sampleDB.name = testSampleName
115                sampleDB.parentEvent = null
116                assert !sampleDB.validate()
117        }
118
119        void testDelete() {
120                def sampleDB = Sample.findByName(testSampleName)
121                sampleDB.delete()
122                try {
123                        sampleDB.save()
124                        assert false // The save should not succeed since the sample is referenced by a study
125                }
126                catch(org.springframework.dao.InvalidDataAccessApiUsageException e) {
127                        sampleDB.discard()
128                        assert true // OK, correct exception (at least for the in-mem db, for PostgreSQL it's probably a different one...)
129                }
130
131                // Now, delete the sample from the study samples collection, and then the delete action should be cascaded to the sample itself
132                def study = Study.findByTitle(testStudyName)
133                assert study
134                study.removeFromSamples sampleDB
135
136                // Make sure the sample doesn't exist anymore at this point
137                assert !Sample.findByName(testSampleName)
138                assert Sample.count() == 0
139                assert study.samples.size() == 0
140        }
141
142        void testDeleteViaParentSubject() {
143
144                def sampleDB = Sample.findByName(testSampleName)
145                assert sampleDB
146
147                // Retrieve the parent study
148                def study = Study.findByTitle(testStudyName)
149                assert study
150
151                def subject = SubjectTests.createSubject(study)
152                assert subject
153
154                sampleDB.parentSubject = subject
155                assert sampleDB.validate()
156                assert sampleDB.save()
157
158                // Use the deleteSubject method
159                def msg = study.deleteSubject(subject)
160                println msg
161                assert study.save()
162
163                assert !study.subjects.contains(subject)
164
165                assert !Subject.findByName(subject.name)
166                assert !Sample.findByName(testSampleName)
167
168                assert Subject.count() == 0
169                assert Sample.count() == 0
170
171        }
172
173        void testDeleteViaParentSamplingEvent() {
174
175                def sampleDB = Sample.findByName(testSampleName)
176                assert sampleDB
177
178                // Retrieve the parent study
179                def study = Study.findByTitle(testStudyName)
180                assert study
181
182                def event = sampleDB.parentEvent
183                assert event
184
185                // Use the deleteSamplingEvent method
186                def msg = study.deleteSamplingEvent(event)
187                println msg
188                assert study.save()
189
190                assert !study.samplingEvents.contains(event)
191
192                assert !SamplingEvent.findByStartTime(testSamplingEventTime)
193                assert !Sample.findByName(testSampleName)
194
195                assert SamplingEvent.count() == 0
196                assert Sample.count() == 0
197
198        }
199
200        void testDeleteViaParentEventGroup() {
201
202                def sampleDB = Sample.findByName(testSampleName)
203                assert sampleDB
204
205                // Retrieve the parent study
206                def study = Study.findByTitle(testStudyName)
207                assert study
208
209                // Retrieve the sample's sampling event
210                def event = sampleDB.parentEvent
211                assert event
212
213                // Create a subject and add it at the sample's parent
214                def subject = SubjectTests.createSubject(study)
215                assert subject
216                sampleDB.parentSubject = subject
217                assert sampleDB.validate()
218                assert sampleDB.save()
219
220                // Create an event group in this study with the sample's sampling event
221                def group = new EventGroup(
222                    name: testEventGroupName
223                )
224                study.addToEventGroups(group)
225                group.addToSubjects(subject)
226                group.addToSamplingEvents(event)
227                assert study.eventGroups.find { it.name == group.name}
228                assert group.validate()
229                assert study.save()
230
231                // Use the deleteSamplingEvent method
232                def msg = study.deleteEventGroup(group)
233                println msg
234                assert study.save()
235
236                assert !study.eventGroups.contains(group)
237                assert !EventGroup.findByName(testEventGroupName)
238                assert !Sample.findByName(testSampleName)
239
240                assert EventGroup.count() == 0
241                assert Sample.count() == 0
242
243        }
244
245        void testStudyRelation() {
246                // Retrieve the parent study
247                def study = Study.findByTitle(testStudyName)
248                assert study
249
250                // Test giveSampleTemplates
251                def templates = study.giveSampleTemplates()
252                assert templates
253                assert templates.size() == 1
254                assert templates.asList().first().name == testSampleTemplateName
255
256                // Test if the sample is in the samples collection
257                assert study.samples
258                assert study.samples.size() == 1
259                assert study.samples.first().name == testSampleName
260        }
261
262
263        void testParentStudy() {
264                def sample = Sample.findByName(testSampleName)
265                assert sample
266
267                assert sample.parent
268                assert sample.parent.code == testStudyCode
269        }
270
271        void testSampleUniqueNameConstraint() {
272                def sample = Sample.findByName(testSampleName)
273                assert sample
274
275                def study = sample.parent
276                assert study
277
278                def sample2 = new Sample(
279                    name: testSampleName,
280                    template: sample.template,
281                    parentEvent: sample.parentEvent
282                )
283
284                // Add the sample to the retrieved parent study
285                study.addToSamples(sample2)
286
287                // At this point, the sample should not validate or save, because there is already a sample with that name in the study
288                assert !sample2.validate()
289                assert !sample2.save(flush:true)
290
291        }
292
293        void testFindViaSamplingEvent() {
294                // Try to retrieve the sampling event by using the time...
295                // (should be also the parent study but that's not yet implemented)
296                def samplingEventDB = SamplingEvent.findByStartTime(testSamplingEventTime)
297                assert samplingEventDB
298
299                def samples = samplingEventDB.getSamples()
300                assert samples
301                assert samples.size() == 1
302                assert samples.first().name == testSampleName
303        }
304
305        void testDomainFields() {
306                def sample = Sample.findByName(testSampleName)
307                assert sample
308
309                // Make sure the domain fields exist
310                assert sample.fieldExists('name')
311                assert sample.fieldExists('material')
312
313                // Make sure they are domain fields
314                assert sample.isDomainField('name')
315                assert sample.isDomainField('material')
316
317                // Make sure that they have the right type
318                assert sample.giveFieldType('name') == TemplateFieldType.STRING
319                assert sample.giveFieldType('material') == TemplateFieldType.ONTOLOGYTERM
320
321        }
322
323        protected void tearDown() {
324                super.tearDown()
325        }
326
327}
Note: See TracBrowser for help on using the repository browser.