source: trunk/test/integration/gscf/EventGroupTests.groovy @ 817

Last change on this file since 817 was 813, checked in by keesvb, 10 years ago

updated event group with unique name constraint, added EventGroup? tests and updated deleteEventGroup to deal with empty event groups

  • Property svn:keywords set to Date Author Rev
File size: 3.8 KB
Line 
1package gscf
2
3import gscf.StudyTests
4import dbnp.studycapturing.*
5
6/**
7 * Test the creation of a EventGroup on data model level
8 *
9 * @author keesvb
10 * @since 20100704
11 * @package dbnp.studycapturing
12 *
13 * Revision information:
14 * $Rev: 813 $
15 * $Author: keesvb $
16 * $Date: 2010-08-16 16:27:31 +0000 (ma, 16 aug 2010) $
17 */
18
19class EventGroupTests extends StudyTests {
20
21        // This test extends StudyTests, so that we have a test study to assign as a parent study
22
23        final String testEventGroupName = "Test Group"
24
25        protected void setUp() {
26                // Create the study
27                super.setUp()
28
29                // Retrieve the study that should have been created in StudyTests
30                def study = Study.findByTitle(testStudyName)
31                assert study
32
33                // Create sample with the retrieved study as parent
34                def group = new EventGroup(
35                    name: testEventGroupName
36                )
37
38                // At this point, the event group should not validate, because it doesn't have a parent study assigned
39                assert !group.validate()
40
41                // Add the group to the retrieved parent study
42                study.addToEventGroups(group)
43                assert study.eventGroups.find { it.name == group.name}
44
45                // Now, the group should validate
46                if (!group.validate()) {
47                        group.errors.each { println it}
48                }
49                assert group.validate()
50
51                // Create a subject and add it to the group
52                def subject = SubjectTests.createSubject(study)
53                assert subject
54                study.addToSubjects(subject)
55                assert study.save()
56                group.addToSubjects(subject)
57
58                // Make sure the group is saved to the database
59                assert group.save(flush: true)
60
61        }
62
63        void testSave() {
64                // Try to retrieve the group and make sure it's the same
65                def groupDB = EventGroup.findByName(testEventGroupName)
66                assert groupDB
67                assert groupDB.name.equals(testEventGroupName)
68
69                // A group without a name should not be saveable
70                groupDB.name = null
71                assert !groupDB.validate()
72
73        }
74
75        void testDelete() {
76                def groupDB = EventGroup.findByName(testEventGroupName)
77                assert groupDB
78
79                groupDB.delete()
80                try {
81                        groupDB.save()
82                        assert false // The save should not succeed since the group is referenced by a study
83                }
84                catch(org.springframework.dao.InvalidDataAccessApiUsageException e) {
85                        groupDB.discard()
86                        assert true // OK, correct exception (at least for the in-mem db, for PostgreSQL it's probably a different one...)
87                }
88
89                // Now, delete the group from the study groups collection, and then the delete action should be cascaded to the group itself
90                def study = Study.findByTitle(testStudyName)
91                assert study
92                study.removeFromEventGroups groupDB
93
94                // Strangely, calling study.save() gives an error here since the subject is not removed from the group?!!
95                assert study.save()
96
97                // Make sure the group doesn't exist anymore at this point
98                assert !EventGroup.findByName(testEventGroupName)
99                assert EventGroup.count() == 0
100                assert study.eventGroups.size() == 0
101        }
102
103        void testDeleteViaStudy() {
104
105                def groupDB = EventGroup.findByName(testEventGroupName)
106                assert groupDB
107
108                def study = Study.findByTitle(testStudyName)
109                assert study
110                def msg = study.deleteEventGroup(groupDB)
111                println msg
112                study.save()
113
114                // Make sure the group doesn't exist anymore at this point
115                assert !EventGroup.findByName(testEventGroupName)
116                assert EventGroup.count() == 0
117                assert study.eventGroups.size() == 0
118               
119        }
120
121        void testParentStudy() {
122                def group = EventGroup.findByName(testEventGroupName)
123                assert group
124
125                assert group.parent
126                assert group.parent.code == testStudyCode
127        }
128
129        void testUniqueNameConstraint() {
130                def group = EventGroup.findByName(testEventGroupName)
131                assert group
132
133                def study = group.parent
134                assert study
135
136                def group2 = new EventGroup(
137                    name: testEventGroupName
138                )
139
140                // Add the group to the retrieved parent study
141                study.addToEventGroups(group2)
142
143                // At this point, the group should not validate or save, because there is already a group with that name in the study
144                assert !group2.validate()
145                assert !group2.save(flush:true)
146
147        }
148
149        protected void tearDown() {
150                super.tearDown()
151        }
152
153}
Note: See TracBrowser for help on using the repository browser.