1 | package gscf |
---|
2 | |
---|
3 | import gscf.StudyTests |
---|
4 | import 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: 818 $ |
---|
15 | * $Author: keesvb $ |
---|
16 | * $Date: 2010-08-17 12:52:44 +0000 (di, 17 aug 2010) $ |
---|
17 | */ |
---|
18 | |
---|
19 | class 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 | // This test is switched off, as event groups should be deleted via study.deleteEventGroup() and not directly |
---|
76 | void dontTestDelete() { |
---|
77 | def groupDB = EventGroup.findByName(testEventGroupName) |
---|
78 | assert groupDB |
---|
79 | |
---|
80 | groupDB.delete() |
---|
81 | try { |
---|
82 | groupDB.save() |
---|
83 | assert false // The save should not succeed since the group is referenced by a study |
---|
84 | } |
---|
85 | catch(org.springframework.dao.InvalidDataAccessApiUsageException e) { |
---|
86 | groupDB.discard() |
---|
87 | assert true // OK, correct exception (at least for the in-mem db, for PostgreSQL it's probably a different one...) |
---|
88 | } |
---|
89 | |
---|
90 | // Now, delete the group from the study groups collection, and then the delete action should be cascaded to the group itself |
---|
91 | def study = Study.findByTitle(testStudyName) |
---|
92 | assert study |
---|
93 | study.removeFromEventGroups groupDB |
---|
94 | |
---|
95 | // Strangely, calling study.save() gives an error here since the subject is not removed from the group?!! |
---|
96 | assert study.save() |
---|
97 | |
---|
98 | // Make sure the group doesn't exist anymore at this point |
---|
99 | assert !EventGroup.findByName(testEventGroupName) |
---|
100 | assert EventGroup.count() == 0 |
---|
101 | assert study.eventGroups.size() == 0 |
---|
102 | } |
---|
103 | |
---|
104 | void testDeleteViaStudy() { |
---|
105 | |
---|
106 | def groupDB = EventGroup.findByName(testEventGroupName) |
---|
107 | assert groupDB |
---|
108 | |
---|
109 | def study = Study.findByTitle(testStudyName) |
---|
110 | assert study |
---|
111 | def msg = study.deleteEventGroup(groupDB) |
---|
112 | println msg |
---|
113 | study.save() |
---|
114 | |
---|
115 | // Make sure the group doesn't exist anymore at this point |
---|
116 | assert !EventGroup.findByName(testEventGroupName) |
---|
117 | assert EventGroup.count() == 0 |
---|
118 | assert study.eventGroups.size() == 0 |
---|
119 | |
---|
120 | } |
---|
121 | |
---|
122 | void testParentStudy() { |
---|
123 | def group = EventGroup.findByName(testEventGroupName) |
---|
124 | assert group |
---|
125 | |
---|
126 | assert group.parent |
---|
127 | assert group.parent.code == testStudyCode |
---|
128 | } |
---|
129 | |
---|
130 | void testUniqueNameConstraint() { |
---|
131 | def group = EventGroup.findByName(testEventGroupName) |
---|
132 | assert group |
---|
133 | |
---|
134 | def study = group.parent |
---|
135 | assert study |
---|
136 | |
---|
137 | def group2 = new EventGroup( |
---|
138 | name: testEventGroupName |
---|
139 | ) |
---|
140 | |
---|
141 | // Add the group to the retrieved parent study |
---|
142 | study.addToEventGroups(group2) |
---|
143 | |
---|
144 | // At this point, the group should not validate or save, because there is already a group with that name in the study |
---|
145 | assert !group2.validate() |
---|
146 | assert !group2.save(flush:true) |
---|
147 | |
---|
148 | } |
---|
149 | |
---|
150 | protected void tearDown() { |
---|
151 | super.tearDown() |
---|
152 | } |
---|
153 | |
---|
154 | } |
---|