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

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

added deleteSubject method to Study, updated Study and Sample integration tests, disabled unused ontology tests

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