1 | package gscf |
---|
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 | |
---|
9 | /** |
---|
10 | * Test the creation of a Sample and its TemplateEntity functionality on data model level |
---|
11 | * |
---|
12 | * @author keesvb |
---|
13 | * @since 20100511 |
---|
14 | * @package dbnp.studycapturing |
---|
15 | * |
---|
16 | * Revision information: |
---|
17 | * $Rev: 653 $ |
---|
18 | * $Author: keesvb $ |
---|
19 | * $Date: 2010-07-15 11:01:53 +0000 (do, 15 jul 2010) $ |
---|
20 | */ |
---|
21 | |
---|
22 | class 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 | |
---|
33 | |
---|
34 | protected void setUp() { |
---|
35 | super.setUp() |
---|
36 | |
---|
37 | // Retrieve the study that should have been created in StudyTests |
---|
38 | def study = Study.findByTitle(testStudyName) |
---|
39 | assert study |
---|
40 | |
---|
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 |
---|
50 | ) |
---|
51 | |
---|
52 | if (!samplingEvent.validate()) { |
---|
53 | samplingEvent.errors.each { println it} |
---|
54 | } |
---|
55 | assert samplingEvent.validate() |
---|
56 | |
---|
57 | |
---|
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) |
---|
84 | |
---|
85 | } |
---|
86 | |
---|
87 | void testSave() { |
---|
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) |
---|
95 | |
---|
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 |
---|
133 | } |
---|
134 | |
---|
135 | void testDomainFields() { |
---|
136 | def sample = Sample.findByName(testStudyName) |
---|
137 | assert sample |
---|
138 | |
---|
139 | // Make sure the domain fields exist |
---|
140 | assert sample.fieldExists('name') |
---|
141 | assert sample.fieldExists('material') |
---|
142 | |
---|
143 | // Make sure they are domain fields |
---|
144 | assert sample.isDomainField('name') |
---|
145 | assert sample.isDomainField('material') |
---|
146 | |
---|
147 | } |
---|
148 | |
---|
149 | protected void tearDown() { |
---|
150 | super.tearDown() |
---|
151 | } |
---|
152 | |
---|
153 | } |
---|