source: trunk/test/integration/gscf/SubjectTests.groovy @ 1590

Last change on this file since 1590 was 1590, checked in by work@…, 9 years ago
  • fixing remaining test issues
  • Property svn:keywords set to Rev Author Date
File size: 8.7 KB
Line 
1package gscf
2
3import dbnp.studycapturing.*
4import org.dbnp.gdt.*
5
6/**
7 * SubjectTests Test
8 *
9 * Test the creation of a Subject and its TemplateEntity functionality on data model level
10 *
11 * @author keesvb
12 * @since 20100510
13 * @package dbnp.studycapturing
14 *
15 * Revision information:
16 * $Rev: 1590 $
17 * $Author: work@osx.eu $
18 * $Date: 2011-03-04 15:28:45 +0000 (vr, 04 mrt 2011) $
19 */
20class SubjectTests extends StudyTests {
21
22        static final String testSubjectName = "Test subject"
23        static final String testSubjectTemplateName = "Human"
24        static final String testSubjectSpeciesTerm = "Homo sapiens"
25        static final String testSubjectBMITemplateFieldName = "BMI"
26        static final double testSubjectBMI = 25.32
27        static final String testSubjectGenderTemplateFieldName = "Gender"
28        static final String testSubjectGender = "female"
29        static final String testSubjectGenderDBName = "Female"
30
31        /**
32         * Set up test: create test subject to use in the tests, thereby test creation
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                createSubject(study)
42        }
43
44        public static Subject createSubject(Study parentStudy) {
45
46                // Look up human template
47                def humanTemplate = Template.findByName(testSubjectTemplateName)
48                assert humanTemplate
49
50                def humanTerm = Term.getOrCreateTerm('Homo sapiens',Ontology.getOrCreateOntologyByNcboId(1132),'9606')
51
52                //assert humanTerm.validate()
53                //assert humanTerm.save(flush:true)
54                //assert humanTerm
55
56                def subject = new Subject(
57                        name: testSubjectName,
58                        template: humanTemplate,
59                        species: humanTerm
60                )
61
62                // At this point, the sample should not validate, because it doesn't have a parent study assigned
63                assert !subject.validate()
64
65                // Add the subject to the retrieved parent study
66                parentStudy.addToSubjects(subject)
67                assert parentStudy.subjects.find { it.name == subject.name}
68
69                // Now, the subject should validate
70                if (!subject.validate()) {
71                        subject.errors.each { println it}
72                }
73                assert subject.validate()
74
75                // Make sure the subject is saved to the database
76                assert subject.save(flush: true)
77
78                return subject
79
80        }
81
82        void testSave() {
83                // Try to retrieve the subject and make sure it's the same
84                def subjectDB = Subject.findByName(testSubjectName)
85                assert subjectDB
86                assert subjectDB.name.equals(testSubjectName)
87                assert subjectDB.template.name.equals(testSubjectTemplateName)
88                assert subjectDB.species.name.equals(testSubjectSpeciesTerm)
89        }
90
91        void testDelete() {
92                def subjectDB = Subject.findByName(testSubjectName)
93                subjectDB.delete()
94                try {
95                        subjectDB.save()
96                        assert false // The save should not succeed since the subject is referenced by a study
97                }
98                catch(org.springframework.dao.InvalidDataAccessApiUsageException e) {
99                        subjectDB.discard()
100                        assert true // OK, correct exception (at least for the in-mem db, for PostgreSQL it's probably a different one...)
101                }
102
103                // Now, delete the subject from the study subjects collection, and then the delete action should be cascaded to the subject itself
104                def study = Study.findByTitle(testStudyName)
105                assert study
106                study.removeFromSubjects subjectDB
107
108                // Make sure the subject doesn't exist anymore at this point
109                assert !Subject.findByName(testSubjectName)
110                assert Subject.count() == 0
111                assert study.subjects.size() == 0
112        }
113
114
115        void testValidators() {
116
117                def subject = new Subject(
118                        name: testSubjectName + "-test"
119                );
120                // Should not validate as template and species are missing
121                assert !subject.validate()
122
123                subject.template = Template.findByName(testSubjectTemplateName)
124                // Still, species is missing
125                assert !subject.validate()
126
127                subject.species = Term.findByName(testSubjectSpeciesTerm)
128                // Still, no parent study assigned
129                assert !subject.validate()
130
131                def study = Study.findByTitle(testStudyName)
132                assert study
133                study.addToSubjects subject
134
135                // Now, the new subject should validate
136                assert subject.validate()
137                assert subject.save()
138
139                // If the subject has the same name as another subject within the same study, it should not validate or save
140                subject.name = testSubjectName
141                assert !subject.validate()
142                assert !subject.save()
143        }
144
145        /**
146         * Test domain field setter of a subject,
147         * by finding the appropriate species as should be defined in the Bootstrap and giveDomainFields
148         * Test that species ontology is correctly defined in giveDomainFields
149         * Test setFieldValue() for domain field species
150         *
151         */
152        void testDomainFieldSetters() {
153
154                def subject = Subject.findByName(testSubjectName)
155                assert subject
156
157                // Remove species from subject, should not validate anymore
158                subject.species = null
159                assert !subject.validate()
160
161
162                // Get domain fields and make sure they are name and species
163                def domainFields = subject.giveDomainFields()
164                assert domainFields
165                assert domainFields[0].name == 'name'
166                assert domainFields[1].name == 'species'
167
168                // Also, make sure isDomainField() says the same
169                assert subject.isDomainField(domainFields[0])
170                assert subject.isDomainField(domainFields[1])
171                assert subject.isDomainField('name')
172                assert subject.isDomainField('species')
173                // To be sure it just doesn't always return true :-)
174                assert !subject.isDomainField('123~!name')
175                assert !subject.isDomainField(testSubjectBMITemplateFieldName)
176
177                // Get the ontologies from species and make sure this is 1 ontology with NCBO ID 1132
178                def speciesOntologies = domainFields[1].ontologies
179                assert speciesOntologies.size() == 1
180
181                // Getting the only element in a set is hard in Grails...
182                Ontology speciesOntology = speciesOntologies.asList().first()
183                assert speciesOntology.ncboId == 1132
184
185                def speciesTerms = speciesOntology.giveTerms()
186                def humanTerm = speciesTerms.find { it.name == testSubjectSpeciesTerm}
187                assert humanTerm
188
189                // Make sure giveTermByName returns the same term
190                assert humanTerm == speciesOntology.giveTermByName('Homo sapiens')
191
192                // Make sure the (in this test used!) Term.findByName returns the same
193                // If this doesn't hold, we probably should rewrite these tests
194                assert humanTerm == Term.findByName(testSubjectSpeciesTerm)
195
196                // Assign species, subject should now validate and save
197                subject.setFieldValue('species',humanTerm)
198                if (!subject.validate()) {
199                        subject.errors.each { println it}
200                }
201                assert subject.validate()
202                assert subject.save(flush:true)
203
204                assert subject.getFieldValue('species') == humanTerm
205                assert subject.getFieldValue('name').equals(testSubjectName)
206                assert subject.getFieldValue('species') == humanTerm
207
208        }
209
210        /**
211         * Test getFieldValue() for domain fields
212         */
213        void testDomainFieldGetters() {
214
215                def subjectDB = Subject.findByName(testSubjectName)
216                assert subjectDB
217                assert subjectDB.getFieldValue('name').equals(testSubjectName)
218                assert subjectDB.getFieldValue('species') == Term.findByName(testSubjectSpeciesTerm)
219        }
220
221        /**
222         * Test setFieldValue() and getFieldValue() for template fields
223     * This cannot be done in separate tests, as the database state is reset in between
224         */
225        void testTemplateFieldSetters() {
226
227                def subject = Subject.findByName(testSubjectName)
228                assert subject
229
230                // Assign a template field using setFieldValue: BMI
231                subject.setFieldValue(testSubjectBMITemplateFieldName,testSubjectBMI)
232
233                // Try to retrieve it using getFieldValue
234                assert subject.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI
235
236                // Save subject
237                assert subject.save(flush: true)
238
239                // Try to retrieve the subject and make sure the BMI was stored
240                def subjectDB = Subject.findByName(testSubjectName)
241                assert subjectDB
242                assert subjectDB.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI
243        }
244
245        /**
246         * Test giveFields(): should return name, species and template fields
247         */
248        void testGiveFields() {
249
250                def subject = Subject.findByName(testSubjectName)
251                assert subject
252
253                // Test giveFields
254                def fields = subject.giveFields()
255                def i = 0
256                assert fields[i++].name == 'name'
257                assert fields[i++].name == 'species'
258
259                // Look up human template
260                def humanTemplate = Template.findByName(testSubjectTemplateName)
261                assert humanTemplate
262
263                humanTemplate.fields.each {
264                        assert fields[i++].name.equals(it.name)
265                }
266
267        }
268
269        void testSetGender() {
270                def subject = Subject.findByName(testSubjectName)
271                assert subject
272
273                // Set gender
274                subject.setFieldValue(testSubjectGenderTemplateFieldName,testSubjectGender)
275
276                // Test if gender is set properly (to its canonical database name) via getFieldValue()
277                assert subject.getFieldValue(testSubjectGenderTemplateFieldName).name == testSubjectGenderDBName
278
279                // Try to save object
280                assert subject.validate()
281                assert subject.save(flush: true)
282
283                // Try to retrieve the subject and make sure the Gender was stored properly
284                def subjectDB = Subject.findByName(testSubjectName)
285                assert subjectDB
286                assert subjectDB.getFieldValue(testSubjectGenderTemplateFieldName).name == testSubjectGenderDBName
287        }
288
289        protected void tearDown() {
290                super.tearDown()
291        }
292
293}
Note: See TracBrowser for help on using the repository browser.