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

Last change on this file since 654 was 654, checked in by keesvb, 11 years ago

updated sample and study tests, added belongsTo to Sample (which gives it its parent), tried to write the unique name constraint of Sample and wrote a test for it

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