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

Revision 402, 5.5 KB (checked in by keesvb, 4 years ago)

some minor changes to subject tests

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
29        /**
30         * Set up test: create test subject to use in the tests, thereby test creation
31         */
32        protected void setUp() {
33                super.setUp()
34
35                // Look up human template
36                def humanTemplate = Template.findByName(testSubjectTemplateName)
37                assert humanTemplate
38
39                def humanTerm = Term.findByName(testSubjectSpeciesTerm)
40                assert humanTerm
41
42                def subject = new Subject(
43                        name: testSubjectName,
44                        template: humanTemplate,
45                        species: humanTerm
46                )
47
48                assert subject.save(flush:true)
49        }
50
51        void testSave() {
52                // Try to retrieve the subject and make sure it's the same
53                def subjectDB = Subject.findByName(testSubjectName)
54                assert subjectDB
55                assert subjectDB.name.equals(testSubjectName)
56                assert subjectDB.template.name.equals(testSubjectTemplateName)
57                assert subjectDB.species.name.equals(testSubjectSpeciesTerm)
58        }
59
60        void testValidators() {
61
62                def subject = new Subject(
63                        name: testSubjectName + "-test"
64                );
65                // Should not validate as template and species are missing
66                assert !subject.validate()
67
68                subject.template = Template.findByName(testSubjectTemplateName)
69                // Still, species is missing
70                assert !subject.validate()
71
72                subject.species = Term.findByName(testSubjectSpeciesTerm)
73                assert subject.validate()
74
75                // TODO: Set study and change name to already existing name, should fail within one study
76                // subject.name = testSubjectName
77                // assert !subject.validate()
78        }
79
80        /**
81         * Test domain field setter of a subject,
82         * by finding the appropriate species as should be defined in the Bootstrap and giveDomainFields
83         * Test that species ontology is correctly defined in giveDomainFields
84         * Test setFieldValue() for domain field species
85         *
86         */
87        void testDomainFieldSetters() {
88
89                def subject = Subject.findByName(testSubjectName)
90                assert subject
91
92                // Remove species from subject, should not validate anymore
93                subject.species = null
94                assert !subject.validate()
95
96
97                // Get domain fields and make sure they are name and species
98                def domainFields = subject.giveDomainFields()
99                assert domainFields
100                println domainFields[0]
101                assert domainFields[0].name == 'name'
102                assert domainFields[1].name == 'species'
103
104                // Get the ontologies from species and make sure this is 1 ontology with NCBO ID 1132
105                def speciesOntologies = domainFields[1].ontologies
106                assert speciesOntologies.size() == 1
107
108                // Getting the only element in a set is hard in Grails...
109                Ontology speciesOntology = speciesOntologies.asList().first()
110                assert speciesOntology.ncboId == 1132
111
112                def speciesTerms = speciesOntology.giveTerms()
113                def humanTerm = speciesTerms.find { it.name == testSubjectSpeciesTerm}
114                assert humanTerm
115
116                // Make sure giveTermByName returns the same term
117                assert humanTerm == speciesOntology.giveTermByName('Homo sapiens')
118
119                // Make sure the (in this test used!) Term.findByName returns the same
120                // If this doesn't hold, we probably should rewrite these tests
121                assert humanTerm == Term.findByName(testSubjectSpeciesTerm)
122
123                // Assign species, subject should now validate and save
124                subject.setFieldValue('species',humanTerm)
125                assert subject.validate()
126                assert subject.save(flush:true)
127
128                assert subject.getFieldValue('species') == humanTerm
129                assert subject.getFieldValue('name').equals(testSubjectName)
130                assert subject.getFieldValue('species') == humanTerm
131
132        }
133
134        /**
135         * Test getFieldValue() for domain fields
136         */
137        void testDomainFieldGetters() {
138
139                def subjectDB = Subject.findByName(testSubjectName)
140                assert subjectDB
141
142                assert subjectDB.getFieldValue('name').equals(testSubjectName)
143                assert subjectDB.getFieldValue('species') == Term.findByName(testSubjectSpeciesTerm)
144        }
145
146        /**
147         * Test setFieldValue() and getFieldValue() for template fields
148     * This cannot be done in separate tests, as the database state is reset in between
149         */
150        void testTemplateFieldSetters() {
151
152                def subject = Subject.findByName(testSubjectName)
153                assert subject
154
155                // Assign a template field using setFieldValue: BMI
156                subject.setFieldValue(testSubjectBMITemplateFieldName,testSubjectBMI)
157
158                // Try to retrieve it using getFieldValue
159                assert subject.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI
160
161                // Save subject
162                assert subject.save(flush: true)
163
164                // Try to retrieve the subject and make sure the BMI was stored
165                def subjectDB = Subject.findByName(testSubjectName)
166                assert subjectDB
167                assert subjectDB.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI
168        }
169
170        /**
171         * Test giveFields(): should return name, species and template fields
172         */
173        void testGiveFields() {
174
175                def subject = Subject.findByName(testSubjectName)
176                assert subject
177
178                // Test giveFields
179                def fields = subject.giveFields()
180                def i = 0
181                assert fields[i++].name == 'name'
182                assert fields[i++].name == 'species'
183
184                // Look up human template
185                def humanTemplate = Template.findByName(testSubjectTemplateName)
186                assert humanTemplate
187
188                humanTemplate.fields.each {
189                        assert fields[i++].name.equals(it.name)
190                }
191
192        }
193
194        protected void tearDown() {
195                super.tearDown()
196        }
197
198}
Note: See TracBrowser for help on using the browser.