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

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