Changeset 400
- Timestamp:
- May 11, 2010, 9:26:40 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/integration/gscf/SubjectTests.groovy
r398 r400 22 22 23 23 final String testSubjectName = "Test subject" 24 final String testSubjectTemplateName = "Human" 25 final String testSubjectSpeciesTerm = "Homo sapiens" 24 26 final String testSubjectBMITemplateFieldName = "BMI" 25 27 final double testSubjectBMI = 25.32 26 28 29 /** 30 * Set up test: create test subject to use in the tests, thereby test creation 31 */ 27 32 protected void setUp() { 28 33 super.setUp() 29 }30 31 protected void tearDown() {32 super.tearDown()33 }34 35 /**36 * Test creation and saving of a new human subject,37 * by finding the appropriate template as should be defined in the Bootstrap,38 * and finding the appropriate species as should be defined in the Bootstrap and giveDomainFields39 *40 */41 void testCreation() {42 34 43 35 // Look up human template 44 def humanTemplate = Template.findByName( 'Human')36 def humanTemplate = Template.findByName(testSubjectTemplateName) 45 37 assert humanTemplate 38 39 def humanTerm = Term.findByName(testSubjectSpeciesTerm) 40 assert humanTerm 46 41 47 42 def subject = new Subject( 48 43 name: testSubjectName, 49 template: humanTemplate 44 template: humanTemplate, 45 species: humanTerm 50 46 ) 51 47 52 // This subject should not validate as required fields species is missing 53 assert !subject.validate() 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 54 96 55 97 // Get domain fields and make sure they are name and species … … 70 112 71 113 def speciesTerms = speciesOntology.giveTerms() 72 def humanTerm = speciesTerms.find { it.name == 'Homo sapiens'}// or speciesOntology.giveTermByName('Homo sapiens')114 def humanTerm = speciesTerms.find { it.name == testSubjectSpeciesTerm} 73 115 assert humanTerm 74 116 75 // Assign species, subject should now validate 76 subject.species = humanTerm 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) 77 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 78 154 79 155 // Assign a template field using setFieldValue: BMI … … 86 162 assert subject.save(flush: true) 87 163 88 // Try to retrieve the subject and make sure it's the same 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 89 173 def subjectDB = Subject.findByName(testSubjectName) 90 174 assert subjectDB 91 assert subjectDB.name.equals(testSubjectName) 92 assert subjectDB.template.name.equals(humanTemplate.name) 93 assert subjectDB.species.name.equals(humanTerm.name) 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 94 185 95 186 // Test giveFields 96 def fields = subject DB.giveFields()187 def fields = subject.giveFields() 97 188 def i = 0 98 189 assert fields[i++].name == 'name' 99 190 assert fields[i++].name == 'species' 191 192 // Look up human template 193 def humanTemplate = Template.findByName(testSubjectTemplateName) 194 assert humanTemplate 195 100 196 humanTemplate.fields.each { 101 197 assert fields[i++].name.equals(it.name) 102 198 } 103 199 104 // Test getFieldValue 105 assert subjectDB.getFieldValue('name').equals(testSubjectName) 106 assert subjectDB.getFieldValue('species') == humanTerm 107 assert subjectDB.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI 108 200 } 201 202 protected void tearDown() { 203 super.tearDown() 109 204 } 110 205
Note: See TracChangeset
for help on using the changeset viewer.