1 | package gscf |
---|
2 | |
---|
3 | import dbnp.studycapturing.Study |
---|
4 | import dbnp.studycapturing.Template |
---|
5 | import grails.test.GrailsUnitTestCase |
---|
6 | import dbnp.studycapturing.SamplingEvent |
---|
7 | import dbnp.studycapturing.Sample |
---|
8 | import 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: 662 $ |
---|
19 | * $Author: keesvb $ |
---|
20 | * $Date: 2010-07-19 14:28:10 +0000 (ma, 19 jul 2010) $ |
---|
21 | */ |
---|
22 | |
---|
23 | class 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" |
---|
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 testStudyRelation() { |
---|
109 | // Retrieve the parent study |
---|
110 | def study = Study.findByTitle(testStudyName) |
---|
111 | assert study |
---|
112 | |
---|
113 | // Test giveSampleTemplates |
---|
114 | def templates = study.giveSampleTemplates() |
---|
115 | assert templates |
---|
116 | assert templates.size() == 1 |
---|
117 | assert templates.asList().first().name == testSampleTemplateName |
---|
118 | |
---|
119 | // Test if the sample is in the samples collection |
---|
120 | assert study.samples |
---|
121 | assert study.samples.size() == 1 |
---|
122 | assert study.samples.first().name == testSampleName |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | void testParentStudy() { |
---|
127 | def sample = Sample.findByName(testSampleName) |
---|
128 | assert sample |
---|
129 | |
---|
130 | assert sample.parent |
---|
131 | assert sample.parent.code == testStudyCode |
---|
132 | } |
---|
133 | |
---|
134 | void testSampleUniqueNameConstraint() { |
---|
135 | def sample = Sample.findByName(testSampleName) |
---|
136 | assert sample |
---|
137 | |
---|
138 | def study = sample.parent |
---|
139 | assert study |
---|
140 | |
---|
141 | def sample2 = new Sample( |
---|
142 | name: testSampleName, |
---|
143 | template: sampleTemplate, |
---|
144 | parentEvent: samplingEvent |
---|
145 | ) |
---|
146 | |
---|
147 | // Add the sample to the retrieved parent study |
---|
148 | study.addToSamples(sample2) |
---|
149 | |
---|
150 | // At this point, the sample should not validate or save, because there is already a sample with that name in the study |
---|
151 | assert !sample2.validate() |
---|
152 | assert !sample2.save(flush:true) |
---|
153 | |
---|
154 | } |
---|
155 | |
---|
156 | void testFindViaSamplingEvent() { |
---|
157 | // Try to retrieve the sampling event by using the time... |
---|
158 | // (should be also the parent study but that's not yet implemented) |
---|
159 | def samplingEventDB = SamplingEvent.findByStartTime(testSamplingEventTime) |
---|
160 | assert samplingEventDB |
---|
161 | |
---|
162 | def samples = samplingEventDB.getSamples() |
---|
163 | assert samples |
---|
164 | assert samples.size() == 1 |
---|
165 | assert samples.first().name == testSampleName |
---|
166 | } |
---|
167 | |
---|
168 | void testDomainFields() { |
---|
169 | def sample = Sample.findByName(testSampleName) |
---|
170 | assert sample |
---|
171 | |
---|
172 | // Make sure the domain fields exist |
---|
173 | assert sample.fieldExists('name') |
---|
174 | assert sample.fieldExists('material') |
---|
175 | |
---|
176 | // Make sure they are domain fields |
---|
177 | assert sample.isDomainField('name') |
---|
178 | assert sample.isDomainField('material') |
---|
179 | |
---|
180 | // Make sure that they have the right type |
---|
181 | assert sample.giveFieldType('name') == TemplateFieldType.STRING |
---|
182 | assert sample.giveFieldType('material') == TemplateFieldType.ONTOLOGYTERM |
---|
183 | |
---|
184 | } |
---|
185 | |
---|
186 | protected void tearDown() { |
---|
187 | super.tearDown() |
---|
188 | } |
---|
189 | |
---|
190 | } |
---|