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

Last change on this file since 1504 was 1504, checked in by s.h.sikkema@…, 10 years ago

Partially fixed the integration tests

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