root/trunk/test/integration/gscf/SubjectTests.groovy @ 406

Revision 406, 6.8 KB (checked in by keesvb, 4 years ago)

added study tests, skip study adds when in test environment in bootstrap, refactored TemplateEntity?

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$
18 * $Author$
19 * $Date$
20 */
21class SubjectTests extends GrailsUnitTestCase {
22
23        final String testSubjectName = "Test subject"
24        final String testSubjectTemplateName = "Human"
25        final String testSubjectSpeciesTerm = "Homo sapiens"
26        final String testSubjectBMITemplateFieldName = "BMI"
27        final double testSubjectBMI = 25.32
28        final String testSubjectGenderTemplateFieldName = "Gender"
29        final String testSubjectGender = "female"
30        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                // Look up human template
39                def humanTemplate = Template.findByName(testSubjectTemplateName)
40                assert humanTemplate
41
42                def humanTerm = Term.findByName(testSubjectSpeciesTerm)
43                assert humanTerm
44
45                def subject = new Subject(
46                        name: testSubjectName,
47                        template: humanTemplate,
48                        species: humanTerm
49                )
50
51                assert subject.save(flush:true)
52        }
53
54        void testSave() {
55                // Try to retrieve the subject and make sure it's the same
56                def subjectDB = Subject.findByName(testSubjectName)
57                assert subjectDB
58                assert subjectDB.name.equals(testSubjectName)
59                assert subjectDB.template.name.equals(testSubjectTemplateName)
60                assert subjectDB.species.name.equals(testSubjectSpeciesTerm)
61        }
62
63        void testValidators() {
64
65                def subject = new Subject(
66                        name: testSubjectName + "-test"
67                );
68                // Should not validate as template and species are missing
69                assert !subject.validate()
70
71                subject.template = Template.findByName(testSubjectTemplateName)
72                // Still, species is missing
73                assert !subject.validate()
74
75                subject.species = Term.findByName(testSubjectSpeciesTerm)
76                assert subject.validate()
77
78                // TODO: Set study and change name to already existing name, should fail within one study
79                // subject.name = testSubjectName
80                // assert !subject.validate()
81        }
82
83        /**
84         * Test domain field setter of a subject,
85         * by finding the appropriate species as should be defined in the Bootstrap and giveDomainFields
86         * Test that species ontology is correctly defined in giveDomainFields
87         * Test setFieldValue() for domain field species
88         *
89         */
90        void testDomainFieldSetters() {
91
92                def subject = Subject.findByName(testSubjectName)
93                assert subject
94
95                // Remove species from subject, should not validate anymore
96                subject.species = null
97                assert !subject.validate()
98
99
100                // Get domain fields and make sure they are name and species
101                def domainFields = subject.giveDomainFields()
102                assert domainFields
103                assert domainFields[0].name == 'name'
104                assert domainFields[1].name == 'species'
105
106                // Also, make sure isDomainField() says the same
107                assert subject.isDomainField(domainFields[0])
108                assert subject.isDomainField(domainFields[1])
109                assert subject.isDomainField('name')
110                assert subject.isDomainField('species')
111                // To be sure it just doesn't always return true :-)
112                assert !subject.isDomainField('123~!name')
113                assert !subject.isDomainField(testSubjectBMITemplateFieldName)
114
115                // Get the ontologies from species and make sure this is 1 ontology with NCBO ID 1132
116                def speciesOntologies = domainFields[1].ontologies
117                assert speciesOntologies.size() == 1
118
119                // Getting the only element in a set is hard in Grails...
120                Ontology speciesOntology = speciesOntologies.asList().first()
121                assert speciesOntology.ncboId == 1132
122
123                def speciesTerms = speciesOntology.giveTerms()
124                def humanTerm = speciesTerms.find { it.name == testSubjectSpeciesTerm}
125                assert humanTerm
126
127                // Make sure giveTermByName returns the same term
128                assert humanTerm == speciesOntology.giveTermByName('Homo sapiens')
129
130                // Make sure the (in this test used!) Term.findByName returns the same
131                // If this doesn't hold, we probably should rewrite these tests
132                assert humanTerm == Term.findByName(testSubjectSpeciesTerm)
133
134                // Assign species, subject should now validate and save
135                subject.setFieldValue('species',humanTerm)
136                if (!subject.validate()) {
137                        subject.errors.each { println it}
138                }
139                assert subject.validate()
140                assert subject.save(flush:true)
141
142                assert subject.getFieldValue('species') == humanTerm
143                assert subject.getFieldValue('name').equals(testSubjectName)
144                assert subject.getFieldValue('species') == humanTerm
145
146        }
147
148        /**
149         * Test getFieldValue() for domain fields
150         */
151        void testDomainFieldGetters() {
152
153                def subjectDB = Subject.findByName(testSubjectName)
154                assert subjectDB
155                assert subjectDB.getFieldValue('name').equals(testSubjectName)
156                assert subjectDB.getFieldValue('species') == Term.findByName(testSubjectSpeciesTerm)
157        }
158
159        /**
160         * Test setFieldValue() and getFieldValue() for template fields
161     * This cannot be done in separate tests, as the database state is reset in between
162         */
163        void testTemplateFieldSetters() {
164
165                def subject = Subject.findByName(testSubjectName)
166                assert subject
167
168                // Assign a template field using setFieldValue: BMI
169                subject.setFieldValue(testSubjectBMITemplateFieldName,testSubjectBMI)
170
171                // Try to retrieve it using getFieldValue
172                assert subject.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI
173
174                // Save subject
175                assert subject.save(flush: true)
176
177                // Try to retrieve the subject and make sure the BMI was stored
178                def subjectDB = Subject.findByName(testSubjectName)
179                assert subjectDB
180                assert subjectDB.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI
181        }
182
183        /**
184         * Test giveFields(): should return name, species and template fields
185         */
186        void testGiveFields() {
187
188                def subject = Subject.findByName(testSubjectName)
189                assert subject
190
191                // Test giveFields
192                def fields = subject.giveFields()
193                def i = 0
194                assert fields[i++].name == 'name'
195                assert fields[i++].name == 'species'
196
197                // Look up human template
198                def humanTemplate = Template.findByName(testSubjectTemplateName)
199                assert humanTemplate
200
201                humanTemplate.fields.each {
202                        assert fields[i++].name.equals(it.name)
203                }
204
205        }
206
207        void testSetGender() {
208                def subject = Subject.findByName(testSubjectName)
209                assert subject
210
211                // Set gender
212                subject.setFieldValue(testSubjectGenderTemplateFieldName,testSubjectGender)
213
214                // Test if gender is set properly (to its canonical database name) via getFieldValue()
215                assert subject.getFieldValue(testSubjectGenderTemplateFieldName).name == testSubjectGenderDBName
216
217                // Try to save object
218                assert subject.validate()
219                assert subject.save(flush: true)
220
221                // Try to retrieve the subject and make sure the Gender was stored properly
222                def subjectDB = Subject.findByName(testSubjectName)
223                assert subjectDB
224                assert subjectDB.getFieldValue(testSubjectGenderTemplateFieldName).name == testSubjectGenderDBName
225        }
226
227        protected void tearDown() {
228                super.tearDown()
229        }
230
231}
Note: See TracBrowser for help on using the browser.