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

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