Changeset 774


Ignore:
Timestamp:
Aug 4, 2010, 4:12:38 PM (10 years ago)
Author:
keesvb
Message:

added deleteSubject method to Study, updated Study and Sample integration tests, disabled unused ontology tests

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/grails-app/domain/dbnp/studycapturing/Study.groovy

    r754 r774  
    136136                return this.template
    137137        }
     138
     139
     140         /**
     141        * Delete a specific subject from this study, including all its relations
     142        * @param subject The subject to be deleted
     143        * @return A String which contains a (user-readable) message describing the changes to the database
     144        */
     145        String deleteSubject(Subject subject) {
     146
     147                String msg = "Subject ${subject.name} was deleted"
     148
     149                // Delete the subject from the event groups it was referenced in
     150                this.eventGroups.each {
     151                        if (it.subjects.contains(subject)) {
     152                                it.removeFromSubjects(subject)
     153                                msg += ", deleted from event group '${it.name}'"
     154                        }
     155                }
     156
     157                // Delete the samples that have this subject as parent
     158                this.samples.findAll { it.parentSubject.equals(subject) }.each {
     159                        // This should remove the sample itself too, because of the cascading belongsTo relation
     160                        this.removeFromSamples(it)
     161                        // But apparently it needs an explicit delete() too
     162                        it.delete()
     163                        msg += ", sample '${it.name}' was deleted"
     164                }
     165
     166                // This should remove the subject itself too, because of the cascading belongsTo relation
     167                this.removeFromSubjects(subject)
     168                // But apparently it needs an explicit delete() too
     169                subject.delete()
     170
     171                return msg
     172        }
    138173}
  • trunk/test/integration/gscf/OntologyTests.groovy

    r552 r774  
    152152         * Add all OLS ontologies to the database via the Ontocat framework
    153153         */
    154         void testOntocatOLSOntologies() {
     154        private void testOntocatOLSOntologies() {
    155155                // Instantiate EBI OLS service
    156156                uk.ac.ebi.ontocat.OntologyService os = new uk.ac.ebi.ontocat.ols.OlsOntologyService()
     
    161161         * Add all BioPortal ontologies to the database via the Ontocat framework
    162162         */
    163         void testOntocatBioPortalOntologies() {
     163        private void testOntocatBioPortalOntologies() {
    164164                // Instantiate BioPortal service
    165165                uk.ac.ebi.ontocat.OntologyService os = new uk.ac.ebi.ontocat.bioportal.BioportalOntologyService()
  • trunk/test/integration/gscf/SampleTests.groovy

    r737 r774  
    77import dbnp.studycapturing.Sample
    88import dbnp.studycapturing.TemplateFieldType
     9import dbnp.studycapturing.Subject
    910
    1011/**
     
    5455                        samplingEvent.errors.each { println it}
    5556                }
     57                // The SamplingEvent should not validate at this point because it doesn't have a parent study
     58                assert !samplingEvent.validate()
     59
     60                study.addToSamplingEvents(samplingEvent)
     61                // It should do fine now
    5662                assert samplingEvent.validate()
    57 
    5863                assert samplingEvent.save(flush:true)
    5964
     
    125130                // Make sure the sample doesn't exist anymore at this point
    126131                assert !Sample.findByName(testSampleName)
     132                assert Sample.count() == 0
    127133                assert study.samples.size() == 0
     134        }
     135
     136        void testDeleteViaParentSubject() {
     137
     138                def sampleDB = Sample.findByName(testSampleName)
     139                assert sampleDB
     140
     141                // Retrieve the parent study
     142                def study = Study.findByTitle(testStudyName)
     143                assert study
     144
     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
    128165        }
    129166
  • trunk/test/integration/gscf/SubjectTests.groovy

    r496 r774  
    1919 * $Date$
    2020 */
    21 class SubjectTests extends GrailsUnitTestCase {
    22 
    23         final String testSubjectName = "Test subject"
    24         final String testSubjectTemplateName = "Human"
    25         final String testSubjectSpeciesTerm = "Homo sapiens"
    26         final String testSubjectBMITemplateFieldName = "BMI"
    27         final double testSubjectBMI = 25.32
    28         final String testSubjectGenderTemplateFieldName = "Gender"
    29         final String testSubjectGender = "female"
    30         final String testSubjectGenderDBName = "Female"
     21class SubjectTests extends StudyTests {
     22
     23        static final String testSubjectName = "Test subject"
     24        static final String testSubjectTemplateName = "Human"
     25        static final String testSubjectSpeciesTerm = "Homo sapiens"
     26        static final String testSubjectBMITemplateFieldName = "BMI"
     27        static final double testSubjectBMI = 25.32
     28        static final String testSubjectGenderTemplateFieldName = "Gender"
     29        static final String testSubjectGender = "female"
     30        static final String testSubjectGenderDBName = "Female"
    3131
    3232        /**
     
    3535        protected void setUp() {
    3636                super.setUp()
     37
     38                // Retrieve the study that should have been created in StudyTests
     39                def study = Study.findByTitle(testStudyName)
     40                assert study
     41
     42                createSubject(study)
     43        }
     44
     45        public static Subject createSubject(Study parentStudy) {
    3746
    3847                // Look up human template
     
    4049                assert humanTemplate
    4150
    42                 def humanTerm = Term.findByName(testSubjectSpeciesTerm)
     51                def speciesOntology = Ontology.getOrCreateOntologyByNcboId(1132)
     52                def humanTerm = new Term(
     53                        name: 'Homo sapiens',
     54                        ontology: speciesOntology,
     55                        accession: '9606')
     56
     57                assert humanTerm.validate()
     58                assert humanTerm.save(flush:true)
    4359                assert humanTerm
    4460
     
    4965                )
    5066
    51                 assert subject.save(flush:true)
     67                // At this point, the sample should not validate, because it doesn't have a parent study assigned
     68                assert !subject.validate()
     69
     70                // Add the subject to the retrieved parent study
     71                parentStudy.addToSubjects(subject)
     72                assert parentStudy.subjects.find { it.name == subject.name}
     73
     74                // Now, the subject should validate
     75                if (!subject.validate()) {
     76                        subject.errors.each { println it}
     77                }
     78                assert subject.validate()
     79
     80                // Make sure the subject is saved to the database
     81                assert subject.save(flush: true)
     82
     83                return subject
     84
    5285        }
    5386
     
    6093                assert subjectDB.species.name.equals(testSubjectSpeciesTerm)
    6194        }
     95
     96        void testDelete() {
     97                def subjectDB = Subject.findByName(testSubjectName)
     98                subjectDB.delete()
     99                try {
     100                        subjectDB.save()
     101                        assert false // The save should not succeed since the subject is referenced by a study
     102                }
     103                catch(org.springframework.dao.InvalidDataAccessApiUsageException e) {
     104                        subjectDB.discard()
     105                        assert true // OK, correct exception (at least for the in-mem db, for PostgreSQL it's probably a different one...)
     106                }
     107
     108                // Now, delete the subject from the study subjects collection, and then the delete action should be cascaded to the subject itself
     109                def study = Study.findByTitle(testStudyName)
     110                assert study
     111                study.removeFromSubjects subjectDB
     112
     113                // Make sure the subject doesn't exist anymore at this point
     114                assert !Subject.findByName(testSubjectName)
     115                assert Subject.count() == 0
     116                assert study.subjects.size() == 0
     117        }
     118
    62119
    63120        void testValidators() {
     
    74131
    75132                subject.species = Term.findByName(testSubjectSpeciesTerm)
    76                 assert subject.validate()
    77 
    78                 // TODO: Set study and change name to already existing name, should fail within one study
    79                 // subject.name = testSubjectName
    80                 // assert !subject.validate()
     133                // Still, no parent study assigned
     134                assert !subject.validate()
     135
     136                def study = Study.findByTitle(testStudyName)
     137                assert study
     138                study.addToSubjects subject
     139
     140                // Now, the new subject should validate
     141                assert subject.validate()
     142                assert subject.save()
     143
     144                // If the subject has the same name as another subject within the same study, it should not validate or save
     145                subject.name = testSubjectName
     146                assert !subject.validate()
     147                assert !subject.save()
    81148        }
    82149
Note: See TracChangeset for help on using the changeset viewer.