Changeset 813
- Timestamp:
- Aug 16, 2010, 6:27:31 PM (11 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
trunk/grails-app/domain/dbnp/studycapturing/EventGroup.groovy
r784 r813 20 20 21 21 static constraints = { 22 // Ensure that the event group name is unique within the study 23 name(unique:['parent']) 22 24 } 23 25 } -
trunk/grails-app/domain/dbnp/studycapturing/Study.groovy
r812 r813 268 268 println "-" 269 269 println "-" 270 println "REMOVING AN D ENENTGROUP DOES NOT DELETE SAMPLES"270 println "REMOVING AN EVENTGROUP DOES NOT DELETE SAMPLES" 271 271 println "-" 272 272 println "-" … … 279 279 String msg = "EventGroup ${eventGroup} was deleted" 280 280 281 // remove all samples that originate from this eventGroup 282 if (eventGroup.samplingEvents.size()) { 283 // find all samples related to this eventGroup 284 // - subject comparison is relatively straightforward and 285 // behaves as expected 286 // - event comparison behaves strange, so now we compare 287 // 1. database id's or, 288 // 2. object identifiers or, 289 // 3. objects itself 290 // this seems now to work as expected 291 this.samples.findAll { sample -> 292 ( 293 (eventGroup.subjects.findAll { 294 it.equals(sample.parentSubject) 295 }) 296 && 297 (eventGroup.samplingEvents.findAll { 298 ( 299 (it.id && sample.parentEvent.id && it.id==sample.parentEvent.id) 300 || 301 (it.getIdentifier() == sample.parentEvent.getIdentifier()) 302 || 303 it.equals(sample.parentEvent) 304 ) 305 }) 306 ) 307 }.each() { 308 // remove sample from study 309 310 // ------- 311 // NOTE, the right samples are found, but the don't 312 // get deleted from the database! 313 // ------- 314 315 println ".removing sample '${it.name}' from study '${this.title}'" 316 msg += ", sample '${it.name}' was deleted" 317 this.removeFromSamples( it ) 281 // If the event group contains sampling events 282 if (eventGroup.samplingEvents) { 283 // remove all samples that originate from this eventGroup 284 if (eventGroup.samplingEvents.size()) { 285 // find all samples related to this eventGroup 286 // - subject comparison is relatively straightforward and 287 // behaves as expected 288 // - event comparison behaves strange, so now we compare 289 // 1. database id's or, 290 // 2. object identifiers or, 291 // 3. objects itself 292 // this seems now to work as expected 293 this.samples.findAll { sample -> 294 ( 295 (eventGroup.subjects.findAll { 296 it.equals(sample.parentSubject) 297 }) 298 && 299 (eventGroup.samplingEvents.findAll { 300 ( 301 (it.id && sample.parentEvent.id && it.id==sample.parentEvent.id) 302 || 303 (it.getIdentifier() == sample.parentEvent.getIdentifier()) 304 || 305 it.equals(sample.parentEvent) 306 ) 307 }) 308 ) 309 }.each() { 310 // remove sample from study 311 312 // ------- 313 // NOTE, the right samples are found, but the don't 314 // get deleted from the database! 315 // ------- 316 317 println ".removing sample '${it.name}' from study '${this.title}'" 318 msg += ", sample '${it.name}' was deleted" 319 this.removeFromSamples( it ) 320 } 318 321 } 319 } 320 321 // remove all samplingEvents from this eventGroup 322 eventGroup.samplingEvents.findAll{}.each() { 323 eventGroup.removeFromSamplingEvents(it) 324 println ".removed samplingEvent '${it.name}' from eventGroup '${eventGroup.name}'" 325 msg += ", samplingEvent '${it.name}' was removed from eventGroup '${eventGroup.name}'" 326 } 327 328 // remove all subject from this eventGroup 329 eventGroup.subjects.findAll{}.each() { 330 eventGroup.removeFromSubjects(it) 331 println ".removed subject '${it.name}' from eventGroup '${eventGroup.name}'" 332 msg += ", subject '${it.name}' was removed from eventGroup '${eventGroup.name}'" 322 323 // remove all samplingEvents from this eventGroup 324 eventGroup.samplingEvents.findAll{}.each() { 325 eventGroup.removeFromSamplingEvents(it) 326 println ".removed samplingEvent '${it.name}' from eventGroup '${eventGroup.name}'" 327 msg += ", samplingEvent '${it.name}' was removed from eventGroup '${eventGroup.name}'" 328 } 329 } 330 331 // If the event group contains subjects 332 if (eventGroup.subjects) { 333 // remove all subject from this eventGroup 334 eventGroup.subjects.findAll{}.each() { 335 eventGroup.removeFromSubjects(it) 336 println ".removed subject '${it.name}' from eventGroup '${eventGroup.name}'" 337 msg += ", subject '${it.name}' was removed from eventGroup '${eventGroup.name}'" 338 } 333 339 } 334 340 -
trunk/test/integration/gscf/EventGroupTests.groovy
r774 r813 1 1 package gscf 2 2 3 import dbnp.studycapturing.Study 4 import dbnp.studycapturing.Template 5 import grails.test.GrailsUnitTestCase 6 import dbnp.studycapturing.SamplingEvent 7 import dbnp.studycapturing.Sample 8 import dbnp.studycapturing.TemplateFieldType 9 import dbnp.studycapturing.Subject 3 import gscf.StudyTests 4 import dbnp.studycapturing.* 10 5 11 6 /** 12 * Test the creation of a Sample and its TemplateEntity functionalityon data model level7 * Test the creation of a EventGroup on data model level 13 8 * 14 9 * @author keesvb 15 * @since 20100 51110 * @since 20100704 16 11 * @package dbnp.studycapturing 17 12 * … … 22 17 */ 23 18 24 class SampleTests extends StudyTests {19 class EventGroupTests extends StudyTests { 25 20 26 21 // This test extends StudyTests, so that we have a test study to assign as a parent study 27 22 28 final String testSampleName = "Test sample @XYZ!" 29 final String testSampleTemplateName = "Human blood sample" 30 31 final String testSamplingEventName = "Test sampling event" 32 final String testSamplingEventTemplateName = "Blood extraction" 33 final long testSamplingEventTime = 34534534L 34 23 final String testEventGroupName = "Test Group" 35 24 36 25 protected void setUp() { 26 // Create the study 37 27 super.setUp() 38 28 … … 41 31 assert study 42 32 43 // Look up sampling event template 44 def samplingEventTemplate = Template.findByName(testSamplingEventTemplateName) 45 assert samplingEventTemplate 46 47 // Create parent sampling event 48 def samplingEvent = new SamplingEvent( 49 startTime: testSamplingEventTime, 50 endTime: testSamplingEventTime, 51 template: samplingEventTemplate 33 // Create sample with the retrieved study as parent 34 def group = new EventGroup( 35 name: testEventGroupName 52 36 ) 53 37 54 if (!samplingEvent.validate()) { 55 samplingEvent.errors.each { println it} 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} 56 48 } 57 // The SamplingEvent should not validate at this point because it doesn't have a parent study 58 assert !samplingEvent.validate() 49 assert group.validate() 59 50 60 study.addToSamplingEvents(samplingEvent) 61 // It should do fine now 62 assert samplingEvent.validate() 63 assert samplingEvent.save(flush:true) 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) 64 57 65 // Look up sample template 66 def sampleTemplate = Template.findByName(testSampleTemplateName) 67 assert sampleTemplate 68 69 // Create sample with the retrieved study as parent 70 def sample = new Sample( 71 name: testSampleName, 72 template: sampleTemplate, 73 parentEvent: samplingEvent 74 ) 75 76 // At this point, the sample should not validate, because it doesn't have a parent study assigned 77 assert !sample.validate() 78 79 // Add the sample to the retrieved parent study 80 study.addToSamples(sample) 81 assert study.samples.find { it.name == sample.name} 82 83 // Now, the sample should validate 84 if (!sample.validate()) { 85 sample.errors.each { println it} 86 } 87 assert sample.validate() 88 89 // Make sure the sample is saved to the database 90 assert sample.save(flush: true) 58 // Make sure the group is saved to the database 59 assert group.save(flush: true) 91 60 92 61 } 93 62 94 63 void testSave() { 95 // Try to retrieve the sample and make sure it's the same 96 def sampleDB = Sample.findByName(testSampleName) 97 assert sampleDB 98 assert sampleDB.name.equals(testSampleName) 99 assert sampleDB.template.name.equals(testSampleTemplateName) 100 assert sampleDB.parentEvent 101 assert sampleDB.parentEvent.startTime.equals(testSamplingEventTime) 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) 102 68 103 // A samplewithout a name should not be saveable104 sampleDB.name = null105 assert ! sampleDB.validate()69 // A group without a name should not be saveable 70 groupDB.name = null 71 assert !groupDB.validate() 106 72 107 // A sample without a parent SamplingEvent should not be saveable108 sampleDB.name = testSampleName109 sampleDB.parentEvent = null110 assert !sampleDB.validate()111 73 } 112 74 113 75 void testDelete() { 114 def sampleDB = Sample.findByName(testSampleName) 115 sampleDB.delete() 76 def groupDB = EventGroup.findByName(testEventGroupName) 77 assert groupDB 78 79 groupDB.delete() 116 80 try { 117 sampleDB.save()118 assert false // The save should not succeed since the sampleis referenced by a study81 groupDB.save() 82 assert false // The save should not succeed since the group is referenced by a study 119 83 } 120 84 catch(org.springframework.dao.InvalidDataAccessApiUsageException e) { 121 sampleDB.discard()85 groupDB.discard() 122 86 assert true // OK, correct exception (at least for the in-mem db, for PostgreSQL it's probably a different one...) 123 87 } 124 88 125 // Now, delete the sample from the study samples collection, and then the delete action should be cascaded to the sampleitself89 // Now, delete the group from the study groups collection, and then the delete action should be cascaded to the group itself 126 90 def study = Study.findByTitle(testStudyName) 127 91 assert study 128 study.removeFrom Samples sampleDB92 study.removeFromEventGroups groupDB 129 93 130 // Make sure the sample doesn't exist anymore at this point 131 assert !Sample.findByName(testSampleName) 132 assert Sample.count() == 0 133 assert study.samples.size() == 0 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 134 101 } 135 102 136 void testDeleteVia ParentSubject() {103 void testDeleteViaStudy() { 137 104 138 def sampleDB = Sample.findByName(testSampleName)139 assert sampleDB105 def groupDB = EventGroup.findByName(testEventGroupName) 106 assert groupDB 140 107 141 // Retrieve the parent study142 108 def study = Study.findByTitle(testStudyName) 143 109 assert study 110 def msg = study.deleteEventGroup(groupDB) 111 println msg 112 study.save() 144 113 145 def subject = SubjectTests.createSubject(study) 146 assert subject 147 148 sampleDB.parentSubject = subject 149 assert sampleDB.validate() 150 assert sampleDB.save() 151 152 // Use the deleteSubject method 153 def msg = study.deleteSubject(subject) 154 println msg 155 assert study.save() 156 157 assert !study.subjects.contains(subject) 158 159 assert !Subject.findByName(testSampleName) 160 assert !Sample.findByName(testSampleName) 161 162 assert Subject.count() == 0 163 assert Sample.count() == 0 164 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 165 119 } 166 120 167 void testStudyRelation() { 168 // Retrieve the parent study 169 def study = Study.findByTitle(testStudyName) 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 170 134 assert study 171 135 172 // Test giveSampleTemplates 173 def templates = study.giveSampleTemplates() 174 assert templates 175 assert templates.size() == 1 176 assert templates.asList().first().name == testSampleTemplateName 177 178 // Test if the sample is in the samples collection 179 assert study.samples 180 assert study.samples.size() == 1 181 assert study.samples.first().name == testSampleName 182 } 183 184 185 void testParentStudy() { 186 def sample = Sample.findByName(testSampleName) 187 assert sample 188 189 assert sample.parent 190 assert sample.parent.code == testStudyCode 191 } 192 193 void testSampleUniqueNameConstraint() { 194 def sample = Sample.findByName(testSampleName) 195 assert sample 196 197 def study = sample.parent 198 assert study 199 200 def sample2 = new Sample( 201 name: testSampleName, 202 template: sample.template, 203 parentEvent: sample.parentEvent 136 def group2 = new EventGroup( 137 name: testEventGroupName 204 138 ) 205 139 206 // Add the sampleto the retrieved parent study207 study.addTo Samples(sample2)140 // Add the group to the retrieved parent study 141 study.addToEventGroups(group2) 208 142 209 // At this point, the sample should not validate or save, because there is already a sample with that name in the study 210 assert !sample2.validate() 211 assert !sample2.save(flush:true) 212 213 } 214 215 void testFindViaSamplingEvent() { 216 // Try to retrieve the sampling event by using the time... 217 // (should be also the parent study but that's not yet implemented) 218 def samplingEventDB = SamplingEvent.findByStartTime(testSamplingEventTime) 219 assert samplingEventDB 220 221 def samples = samplingEventDB.getSamples() 222 assert samples 223 assert samples.size() == 1 224 assert samples.first().name == testSampleName 225 } 226 227 void testDomainFields() { 228 def sample = Sample.findByName(testSampleName) 229 assert sample 230 231 // Make sure the domain fields exist 232 assert sample.fieldExists('name') 233 assert sample.fieldExists('material') 234 235 // Make sure they are domain fields 236 assert sample.isDomainField('name') 237 assert sample.isDomainField('material') 238 239 // Make sure that they have the right type 240 assert sample.giveFieldType('name') == TemplateFieldType.STRING 241 assert sample.giveFieldType('material') == TemplateFieldType.ONTOLOGYTERM 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) 242 146 243 147 }
Note: See TracChangeset
for help on using the changeset viewer.