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

Last change on this file since 737 was 737, checked in by keesvb, 12 years ago

added a test that confirms the cascaded deletion of a Sample in case it is removed from a Study

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