Changeset 813 for trunk/test


Ignore:
Timestamp:
Aug 16, 2010, 6:27:31 PM (10 years ago)
Author:
keesvb
Message:

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

File:
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/test/integration/gscf/EventGroupTests.groovy

    r774 r813  
    11package gscf
    22
    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
     3import gscf.StudyTests
     4import dbnp.studycapturing.*
    105
    116/**
    12  * Test the creation of a Sample and its TemplateEntity functionality on data model level
     7 * Test the creation of a EventGroup on data model level
    138 *
    149 * @author keesvb
    15  * @since 20100511
     10 * @since 20100704
    1611 * @package dbnp.studycapturing
    1712 *
     
    2217 */
    2318
    24 class SampleTests extends StudyTests {
     19class EventGroupTests extends StudyTests {
    2520
    2621        // This test extends StudyTests, so that we have a test study to assign as a parent study
    2722
    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"
    3524
    3625        protected void setUp() {
     26                // Create the study
    3727                super.setUp()
    3828
     
    4131                assert study
    4232
    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
    5236                )
    5337
    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}
    5648                }
    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()
    5950
    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)
    6457
    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)
    9160
    9261        }
    9362
    9463        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)
    10268
    103                 // A sample without a name should not be saveable
    104                 sampleDB.name = null
    105                 assert !sampleDB.validate()
     69                // A group without a name should not be saveable
     70                groupDB.name = null
     71                assert !groupDB.validate()
    10672
    107                 // A sample without a parent SamplingEvent should not be saveable
    108                 sampleDB.name = testSampleName
    109                 sampleDB.parentEvent = null
    110                 assert !sampleDB.validate()
    11173        }
    11274
    11375        void testDelete() {
    114                 def sampleDB = Sample.findByName(testSampleName)
    115                 sampleDB.delete()
     76                def groupDB = EventGroup.findByName(testEventGroupName)
     77                assert groupDB
     78
     79                groupDB.delete()
    11680                try {
    117                         sampleDB.save()
    118                         assert false // The save should not succeed since the sample is referenced by a study
     81                        groupDB.save()
     82                        assert false // The save should not succeed since the group is referenced by a study
    11983                }
    12084                catch(org.springframework.dao.InvalidDataAccessApiUsageException e) {
    121                         sampleDB.discard()
     85                        groupDB.discard()
    12286                        assert true // OK, correct exception (at least for the in-mem db, for PostgreSQL it's probably a different one...)
    12387                }
    12488
    125                 // Now, delete the sample from the study samples collection, and then the delete action should be cascaded to the sample itself
     89                // Now, delete the group from the study groups collection, and then the delete action should be cascaded to the group itself
    12690                def study = Study.findByTitle(testStudyName)
    12791                assert study
    128                 study.removeFromSamples sampleDB
     92                study.removeFromEventGroups groupDB
    12993
    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
    134101        }
    135102
    136         void testDeleteViaParentSubject() {
     103        void testDeleteViaStudy() {
    137104
    138                 def sampleDB = Sample.findByName(testSampleName)
    139                 assert sampleDB
     105                def groupDB = EventGroup.findByName(testEventGroupName)
     106                assert groupDB
    140107
    141                 // Retrieve the parent study
    142108                def study = Study.findByTitle(testStudyName)
    143109                assert study
     110                def msg = study.deleteEventGroup(groupDB)
     111                println msg
     112                study.save()
    144113
    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               
    165119        }
    166120
    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
    170134                assert study
    171135
    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
    204138                )
    205139
    206                 // Add the sample to the retrieved parent study
    207                 study.addToSamples(sample2)
     140                // Add the group to the retrieved parent study
     141                study.addToEventGroups(group2)
    208142
    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)
    242146
    243147        }
Note: See TracChangeset for help on using the changeset viewer.