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

Last change on this file since 1456 was 1456, checked in by business@…, 10 years ago

moved gdt package to org.dbnp, moved tests for RelTime?, TemplateEntity?, Template etc. to gdt plugin

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