Changeset 653 for trunk/test


Ignore:
Timestamp:
Jul 15, 2010, 1:01:53 PM (13 years ago)
Author:
keesvb
Message:

updated study tests with code, added samplingevent and sample tests

Location:
trunk/test/integration/gscf
Files:
1 edited
1 copied

Legend:

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

    r651 r653  
    11package gscf
    22
    3 import grails.test.*
    4 import dbnp.studycapturing.*
    5 import dbnp.data.*
     3import dbnp.studycapturing.Study
     4import dbnp.studycapturing.Template
     5import grails.test.GrailsUnitTestCase
     6import dbnp.studycapturing.SamplingEvent
     7import dbnp.studycapturing.Sample
    68
    79/**
    8  * Test the creation of a Subject and its TemplateEntity functionality on data model level
     10 * Test the creation of a Sample and its TemplateEntity functionality on data model level
    911 *
    1012 * @author keesvb
     
    1719 * $Date$
    1820 */
    19 class StudyTests extends GrailsUnitTestCase {
    2021
    21         final String testStudyName = "Test study"
    22         final String testStudyTemplateName = "Academic study"
    23         final Date testStudyStartDate = Date.parse('yyyy-MM-dd','2007-12-11')
    24         final Date testStudyStartDate2 = Date.parse('yyyy-MM-dd','2008-05-11')
    25         final String testStudyStartDateString2 = "11-5-2008"
    26         // The following dates do net yet work:
    27         //final String testStudyStartDateString2 = "11-05-2010"
    28         //final String testStudyStartDateString2 = "Tue Dec 13 00:00:00 EST 2008"
     22class SampleTests extends StudyTests {
     23
     24        // This test extends StudyTests, so that we have a test study to assign as a parent study
     25
     26        final String testSampleName = "Test sample"
     27        final String testSampleTemplateName = "Human blood sample"
     28
     29        final String testSamplingEventName = "Test sampling event"
     30        final String testSamplingEventTemplateName = "Blood extraction"
     31        final long testSamplingEventTime = 34534534L
     32
    2933
    3034        protected void setUp() {
    3135                super.setUp()
    3236
    33                 // Look up academic template
    34                 def studyTemplate = Template.findByName(testStudyTemplateName)
    35                 assert studyTemplate
     37                // Retrieve the study that should have been created in StudyTests
     38                def study = Study.findByTitle(testStudyName)
     39                assert study
    3640
    37                 def study = new Study(
    38                     title: testStudyName,
    39                     template: studyTemplate,
    40                     startDate: testStudyStartDate
     41                // Look up sampling event template
     42                def samplingEventTemplate = Template.findByName(testSamplingEventTemplateName)
     43                assert samplingEventTemplate
     44
     45                // Create parent sampling event
     46                def samplingEvent = new SamplingEvent(
     47                        startTime: testSamplingEventTime,
     48                        endTime: testSamplingEventTime,
     49                        template: samplingEventTemplate
    4150                )
    4251
    43                 if (!study.validate()) {
    44                         study.errors.each { println it}
     52                if (!samplingEvent.validate()) {
     53                        samplingEvent.errors.each { println it}
    4554                }
    46                 assert study.validate()
     55                assert samplingEvent.validate()
    4756
    4857
    49                 assert study.save(flush: true)
     58                // Look up sample template
     59                def sampleTemplate = Template.findByName(testSampleTemplateName)
     60                assert sampleTemplate
     61
     62                // Create sample with the retrieved study as parent
     63                def sample = new Sample(
     64                    name: testSampleName,
     65                    template: sampleTemplate,
     66                    parentEvent: samplingEvent
     67                )
     68
     69                // At this point, the sample should not validate, because it doesn't have a parent study assigned
     70                assert !sample.validate()
     71
     72                // Add the sample to the retrieved parent study
     73                study.addToSamples(sample)
     74                assert study.samples.find { it.name == sample.name}
     75
     76                // Now, the sample should validate
     77                if (!sample.validate()) {
     78                        sample.errors.each { println it}
     79                }
     80                assert sample.validate()
     81
     82                // Make sure the sample is saved to the database
     83                assert sample.save(flush: true)
    5084
    5185        }
    5286
    5387        void testSave() {
    54                 // Try to retrieve the study and make sure it's the same
    55                 def studyDB = Study.findByTitle(testStudyName)
    56                 assert studyDB
    57                 assert studyDB.title.equals(testStudyName)
    58                 assert studyDB.template.name.equals(testStudyTemplateName)
    59                 assert studyDB.startDate.equals(testStudyStartDate)
     88                // Try to retrieve the sample and make sure it's the same
     89                def sampleDB = Sample.findByName(testSampleName)
     90                assert sampleDB
     91                assert sampleDB.name.equals(testSampleName)
     92                assert sampleDB.template.name.equals(testSampleTemplateName)
     93                assert sampleDB.parentEvent
     94                assert sampleDB.parentEvent.startTime.equals(testSamplingEventTime)
    6095
    61                 // A study without a title should not be saveable
    62                 studyDB.title = null
    63                 assert !studyDB.validate()
     96                // A sample without a name should not be saveable
     97                sampleDB.name = null
     98                assert !sampleDB.validate()
     99
     100                // A sample without a parent SamplingEvent should not be saveable
     101                sampleDB.name = testSampleName
     102                sampleDB.parentEvent = null
     103                assert !sampleDB.validate()
     104        }
     105
     106        void testStudyRelation() {
     107                // Retrieve the parent study
     108                def study = Study.findByTitle(testStudyName)
     109                assert study
     110
     111                // Test giveSamplingEventTemplates
     112                def templates = study.giveSamplingEventTemplates()
     113                assert templates
     114                assert templates.size() == 1
     115                assert templates
     116
     117                // Test if the sample is in the samples collection
     118                assert study.samples
     119                assert study.samples.size() == 1
     120                assert study.samples.first().name == testSampleName
     121        }
     122
     123        void testFindViaSamplingEvent() {
     124                // Try to retrieve the sampling event by using the time...
     125                // (should be also the parent study but that's not yet implemented)
     126                def samplingEventDB = SamplingEvent.findByStartTime(testSamplingEventTime)
     127                assert samplingEventDB
     128
     129                def samples = samplingEventDB.getSamples()
     130                assert samples
     131                assert samples.size() == 1
     132                assert samples.first().name == testSampleName
    64133        }
    65134
    66135        void testDomainFields() {
    67                 def study = Study.findByTitle(testStudyName)
    68                 assert study
     136                def sample = Sample.findByName(testStudyName)
     137                assert sample
    69138
    70139                // Make sure the domain fields exist
    71                 assert study.fieldExists('title')
    72                 assert study.fieldExists('startDate')
     140                assert sample.fieldExists('name')
     141                assert sample.fieldExists('material')
    73142
    74143                // Make sure they are domain fields
    75                 assert study.isDomainField('title')
    76                 assert study.isDomainField('startDate')
    77                
    78         }
    79 
    80         void testSetDate() {
    81                 def study = Study.findByTitle(testStudyName)
    82                 assert study
    83 
    84                 // Set a new date, using a string, and check whether that is stored correctly
    85                 study.setFieldValue("startDate",testStudyStartDateString2)
    86                 assert study.validate()
    87                 assert study.save(flush:true)
    88                 assert study.getFieldValue("startDate").equals(testStudyStartDate2)
     144                assert sample.isDomainField('name')
     145                assert sample.isDomainField('material')
    89146
    90147        }
  • trunk/test/integration/gscf/StudyTests.groovy

    r496 r653  
    2121        final String testStudyName = "Test study"
    2222        final String testStudyTemplateName = "Academic study"
     23        final String testStudyCode = "AAA-Test"
    2324        final Date testStudyStartDate = Date.parse('yyyy-MM-dd','2007-12-11')
    2425        final Date testStudyStartDate2 = Date.parse('yyyy-MM-dd','2008-05-11')
     
    3637
    3738                def study = new Study(
    38                     title: testStudyName,
    39                     template: studyTemplate,
    40                     startDate: testStudyStartDate
     39                        title: testStudyName,
     40                        template: studyTemplate,
     41                        startDate: testStudyStartDate,
     42                        code: testStudyCode
    4143                )
    4244
     
    5355        void testSave() {
    5456                // Try to retrieve the study and make sure it's the same
    55                 def studyDB = Study.findByTitle(testStudyName)
     57                def studyDB = Study.findByCode(testStudyCode)
    5658                assert studyDB
    5759                assert studyDB.title.equals(testStudyName)
     60                assert studyDB.code.equals(testStudyCode)
    5861                assert studyDB.template.name.equals(testStudyTemplateName)
    5962                assert studyDB.startDate.equals(testStudyStartDate)
     
    6568
    6669        void testDomainFields() {
    67                 def study = Study.findByTitle(testStudyName)
     70                def study = Study.findByCode(testStudyCode)
    6871                assert study
    6972
     
    7174                assert study.fieldExists('title')
    7275                assert study.fieldExists('startDate')
     76                assert study.fieldExists('code')
     77
    7378
    7479                // Make sure they are domain fields
    7580                assert study.isDomainField('title')
    7681                assert study.isDomainField('startDate')
     82                assert study.isDomainField('code')
    7783               
    7884        }
    7985
    8086        void testSetDate() {
    81                 def study = Study.findByTitle(testStudyName)
     87                def study = Study.findByCode(testStudyCode)
    8288                assert study
    8389
     
    9197
    9298        protected void tearDown() {
     99
     100                // Delete the created study
     101                def study = Study.findByCode(testStudyCode)
     102                assert study
     103
     104                study.delete()
     105                assert Study.findByCode(testStudyCode) == null
     106
    93107                super.tearDown()
    94108        }
Note: See TracChangeset for help on using the changeset viewer.