1 | package gscf |
---|
2 | |
---|
3 | import grails.test.* |
---|
4 | import dbnp.studycapturing.* |
---|
5 | import 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 | */ |
---|
21 | class 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 | // Change name to already existing name, should fail |
---|
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 | println speciesOntologies.class |
---|
109 | // Getting the only element in a set is hard in Grails... |
---|
110 | Ontology speciesOntology = speciesOntologies.asList().first() |
---|
111 | assert speciesOntology.ncboId == 1132 |
---|
112 | |
---|
113 | def speciesTerms = speciesOntology.giveTerms() |
---|
114 | def humanTerm = speciesTerms.find { it.name == testSubjectSpeciesTerm} |
---|
115 | assert humanTerm |
---|
116 | |
---|
117 | // Make sure giveTermByName returns the same term |
---|
118 | assert humanTerm == speciesOntology.giveTermByName('Homo sapiens') |
---|
119 | |
---|
120 | // Make sure the (in this test used!) Term.findByName returns the same |
---|
121 | // If this doesn't hold, we probably should rewrite these tests |
---|
122 | assert humanTerm == Term.findByName(testSubjectSpeciesTerm) |
---|
123 | |
---|
124 | // Assign species, subject should now validate and save |
---|
125 | subject.setFieldValue('species',humanTerm) |
---|
126 | assert subject.validate() |
---|
127 | assert subject.save(flush:true) |
---|
128 | |
---|
129 | assert subjectDB.getFieldValue('species') == humanTerm |
---|
130 | assert subjectDB.getFieldValue('name').equals(testSubjectName) |
---|
131 | assert subjectDB.getFieldValue('species') == humanTerm |
---|
132 | |
---|
133 | } |
---|
134 | |
---|
135 | /** |
---|
136 | * Test getFieldValue() for domain fields |
---|
137 | */ |
---|
138 | void testDomainFieldGetters() { |
---|
139 | |
---|
140 | def subjectDB = Subject.findByName(testSubjectName) |
---|
141 | assert subjectDB |
---|
142 | |
---|
143 | assert subjectDB.getFieldValue('name').equals(testSubjectName) |
---|
144 | assert subjectDB.getFieldValue('species') == Term.findByName(testSubjectSpeciesTerm) |
---|
145 | } |
---|
146 | |
---|
147 | /** |
---|
148 | * Test setFieldValue() for template fields |
---|
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 | |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * Test getFieldValue() for template fields |
---|
169 | */ |
---|
170 | void testTemplateFieldGetters() { |
---|
171 | |
---|
172 | // Try to retrieve the subject and make sure the BMI was stored |
---|
173 | def subjectDB = Subject.findByName(testSubjectName) |
---|
174 | assert subjectDB |
---|
175 | assert subjectDB.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI |
---|
176 | } |
---|
177 | |
---|
178 | /** |
---|
179 | * Test giveFields(): should return name, species and template fields |
---|
180 | */ |
---|
181 | void testGiveFields() { |
---|
182 | |
---|
183 | def subject = Subject.findByName(testSubjectName) |
---|
184 | assert subject |
---|
185 | |
---|
186 | // Test giveFields |
---|
187 | def fields = subject.giveFields() |
---|
188 | def i = 0 |
---|
189 | assert fields[i++].name == 'name' |
---|
190 | assert fields[i++].name == 'species' |
---|
191 | |
---|
192 | // Look up human template |
---|
193 | def humanTemplate = Template.findByName(testSubjectTemplateName) |
---|
194 | assert humanTemplate |
---|
195 | |
---|
196 | humanTemplate.fields.each { |
---|
197 | assert fields[i++].name.equals(it.name) |
---|
198 | } |
---|
199 | |
---|
200 | } |
---|
201 | |
---|
202 | protected void tearDown() { |
---|
203 | super.tearDown() |
---|
204 | } |
---|
205 | |
---|
206 | } |
---|