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

Last change on this file since 398 was 398, checked in by keesvb, 9 years ago

added Ontology/Term? and Subject integration tests

File size: 3.1 KB
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 testSubjectBMITemplateFieldName = "BMI"
25        final double testSubjectBMI = 25.32
26
27        protected void setUp() {
28                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 giveDomainFields
39         *
40         */
41        void testCreation() {
42
43                // Look up human template
44                def humanTemplate = Template.findByName('Human')
45                assert humanTemplate
46
47                def subject = new Subject(
48                        name: testSubjectName,
49                        template: humanTemplate
50                )
51
52                // This subject should not validate as required fields species is missing
53                assert !subject.validate()
54
55                // Get domain fields and make sure they are name and species
56                def domainFields = subject.giveDomainFields()
57                assert domainFields
58                println domainFields[0]
59                assert domainFields[0].name == 'name'
60                assert domainFields[1].name == 'species'
61
62                // Get the ontologies from species and make sure this is 1 ontology with NCBO ID 1132
63                def speciesOntologies = domainFields[1].ontologies
64                assert speciesOntologies.size() == 1
65
66                println speciesOntologies.class
67                // Getting the only element in a set is hard in Grails...
68                Ontology speciesOntology = speciesOntologies.asList().first()
69                assert speciesOntology.ncboId == 1132
70
71                def speciesTerms = speciesOntology.giveTerms()
72                def humanTerm = speciesTerms.find { it.name == 'Homo sapiens'}// or speciesOntology.giveTermByName('Homo sapiens')
73                assert humanTerm
74
75                // Assign species, subject should now validate
76                subject.species = humanTerm
77                assert subject.validate()
78
79                // Assign a template field using setFieldValue: BMI
80                subject.setFieldValue(testSubjectBMITemplateFieldName,testSubjectBMI)
81
82                // Try to retrieve it using getFieldValue
83                assert subject.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI
84
85                // Save subject
86                assert subject.save(flush: true)
87
88                // Try to retrieve the subject and make sure it's the same
89                def subjectDB = Subject.findByName(testSubjectName)
90                assert subjectDB
91                assert subjectDB.name.equals(testSubjectName)
92                assert subjectDB.template.name.equals(humanTemplate.name)
93                assert subjectDB.species.name.equals(humanTerm.name)
94
95                // Test giveFields
96                def fields = subjectDB.giveFields()
97                def i = 0
98                assert fields[i++].name == 'name'
99                assert fields[i++].name == 'species'
100                humanTemplate.fields.each {
101                        assert fields[i++].name.equals(it.name)
102                }
103
104                // Test getFieldValue
105                assert subjectDB.getFieldValue('name').equals(testSubjectName)
106                assert subjectDB.getFieldValue('species') == humanTerm
107                assert subjectDB.getFieldValue(testSubjectBMITemplateFieldName) == testSubjectBMI
108
109        }
110
111}
Note: See TracBrowser for help on using the repository browser.