1 | package dbnp.query |
---|
2 | |
---|
3 | import dbnp.studycapturing.* |
---|
4 | import grails.test.* |
---|
5 | import org.dbnp.gdt.AssayModule |
---|
6 | import org.codehaus.groovy.grails.commons.ApplicationHolder |
---|
7 | import dbnp.authentication.* |
---|
8 | import groovy.mock.interceptor.MockFor; |
---|
9 | |
---|
10 | /** |
---|
11 | * SearchTests Test |
---|
12 | * |
---|
13 | * Description of my test |
---|
14 | * |
---|
15 | * @author your email (+name?) |
---|
16 | * @since 2010mmdd |
---|
17 | * @package ??? |
---|
18 | * |
---|
19 | * Revision information: |
---|
20 | * $Rev: 1504 $ |
---|
21 | * $Author: s.h.sikkema@gmail.com $ |
---|
22 | * $Date: 2011-02-08 09:48:41 +0000 (di, 08 feb 2011) $ |
---|
23 | */ |
---|
24 | class StudySearchTests extends GrailsUnitTestCase { |
---|
25 | |
---|
26 | def mockApplicationHolder |
---|
27 | |
---|
28 | protected void setUp() { |
---|
29 | super.setUp() |
---|
30 | |
---|
31 | def studies = [ new Study( title: 'Study TNO-1', code: 'abc' ), new Study( title: 'Study TNO-2', code: 'def' ), new Study( title: 'Study NMC-1', code: 'ghi' ), new Study( title: 'Study NMC-2', code: 'jkl' ) ]; |
---|
32 | def subjects = [ new Subject( name: 'Subject 1', parent: studies[0] ), new Subject( name: 'Subject 2', parent: studies[1] ) ] |
---|
33 | def events = [ new Event( startTime: 3600, parent: studies[0] ), new Event( startTime: 7200, parent: studies[1] ) ] |
---|
34 | def samplingEvents = [ new SamplingEvent( startTime: 3600, parent: studies[0] ), new SamplingEvent( startTime: 7200, parent: studies[1] ) ] |
---|
35 | def samples = [ |
---|
36 | new Sample( name: 'Sample 1', parent: studies[0], parentSubject: subjects[ 0 ], parentEvent: samplingEvents[ 0 ] ), |
---|
37 | new Sample( name: 'Sample 2', parent: studies[1], parentSubject: subjects[ 1 ], parentEvent: samplingEvents[ 1 ] ) |
---|
38 | ] |
---|
39 | def assays = [ new Assay( name: 'Assay 1', parent: studies[0], samples: [samples[0]] ), new Assay( name: 'Assay 2', parent: studies[1], samples: [samples[1]] ) ] |
---|
40 | |
---|
41 | def users = [ new SecUser( username: "abc" ) ]; |
---|
42 | |
---|
43 | mockDomain( Study, studies ); |
---|
44 | mockDomain( Subject, subjects ); |
---|
45 | mockDomain( Sample, samples ); |
---|
46 | mockDomain( Event, events ); |
---|
47 | mockDomain( SamplingEvent, samplingEvents); |
---|
48 | mockDomain( Assay, assays ); |
---|
49 | |
---|
50 | mockDomain( AssayModule ); |
---|
51 | mockDomain( SecUser, users ) |
---|
52 | mockDomain( SecUserSecRole ) |
---|
53 | mockDomain( SecRole ) |
---|
54 | |
---|
55 | subjects.each { it.parent.addToSubjects( it ); } |
---|
56 | samples.each { it.parent.addToSamples( it ); } |
---|
57 | events.each { it.parent.addToEvents( it ); } |
---|
58 | samplingEvents.each { it.parent.addToSamplingEvents( it ); } |
---|
59 | samples.each { it.parent.addToSamples( it ); } |
---|
60 | assays.each { it.parent.addToAssays( it ); } |
---|
61 | studies.each { |
---|
62 | it.owner = users[0] |
---|
63 | } |
---|
64 | |
---|
65 | mockApplicationHolder = new MockFor(ApplicationHolder) |
---|
66 | |
---|
67 | // some mocks to make sure test doesn't break on finding 'moduleCommunicationService' |
---|
68 | mockApplicationHolder.demand.getApplication(1..10) { [getMainContext: |
---|
69 | { [getBean: |
---|
70 | { what -> |
---|
71 | if( what == "authenticationService" ) |
---|
72 | return [getLoggedInUser: { return users[0]; } ] |
---|
73 | else if( what == "moduleCommunicationService" ) |
---|
74 | return null |
---|
75 | } |
---|
76 | ] } |
---|
77 | ] } |
---|
78 | |
---|
79 | } |
---|
80 | |
---|
81 | protected void tearDown() { |
---|
82 | super.tearDown() |
---|
83 | ApplicationHolder.metaClass.static.getApplication = null |
---|
84 | } |
---|
85 | |
---|
86 | void testExecute() { |
---|
87 | |
---|
88 | mockApplicationHolder.use { |
---|
89 | |
---|
90 | List criteria = [ |
---|
91 | new Criterion( entity: "Study", field: "title", operator: Operator.contains, value: "TNO" ), |
---|
92 | new Criterion( entity: "Study", field: "code", operator: Operator.equals, value: "abc" ), |
---|
93 | new Criterion( entity: "Study", field: "code", operator: Operator.equals, value: "ghi" ) |
---|
94 | ] |
---|
95 | |
---|
96 | def studySearch = new StudySearch(); |
---|
97 | |
---|
98 | // Search without criteria |
---|
99 | studySearch.setCriteria(); |
---|
100 | studySearch.execute(); |
---|
101 | |
---|
102 | assert studySearch.getResults().size() == 4 |
---|
103 | assert studySearch.getResults()[0] instanceof Study |
---|
104 | assert studySearch.getResults()*.code.contains( "abc" ); |
---|
105 | assert studySearch.getResults()*.code.contains( "def" ); |
---|
106 | assert studySearch.getResults()*.code.contains( "ghi" ); |
---|
107 | assert studySearch.getResults()*.code.contains( "jkl" ); |
---|
108 | |
---|
109 | studySearch.setCriteria( [ criteria[0] ] ); |
---|
110 | studySearch.execute(); |
---|
111 | assert studySearch.getResults().size() == 2 |
---|
112 | |
---|
113 | assert studySearch.getResults()*.code.contains( "abc" ); |
---|
114 | assert studySearch.getResults()*.code.contains( "def" ); |
---|
115 | |
---|
116 | studySearch.setCriteria( [ criteria[0], criteria[1] ] ); |
---|
117 | studySearch.execute(); |
---|
118 | assert studySearch.getResults().size() == 1 |
---|
119 | assert studySearch.getResults()[0].code == "abc" |
---|
120 | |
---|
121 | studySearch.setCriteria( [ criteria[0], criteria[2] ] ); |
---|
122 | studySearch.execute(); |
---|
123 | assert studySearch.getResults().size() == 0 |
---|
124 | |
---|
125 | studySearch.setCriteria( [ criteria[1], criteria[2] ] ); |
---|
126 | studySearch.execute(); |
---|
127 | assert studySearch.getResults().size() == 0 |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | void testExecuteDifferentCriteria() { |
---|
132 | |
---|
133 | mockApplicationHolder.use { |
---|
134 | |
---|
135 | List criteria = [ |
---|
136 | new Criterion( entity: "Study", field: "title", operator: Operator.contains, value: "TNO-1" ), |
---|
137 | new Criterion( entity: "Subject", field: "name", operator: Operator.contains, value: "1" ), |
---|
138 | new Criterion( entity: "Sample", field: "name", operator: Operator.contains, value: "1" ), |
---|
139 | new Criterion( entity: "Assay", field: "name", operator: Operator.contains, value: "1" ), |
---|
140 | new Criterion( entity: "Event", field: "startTime", operator: Operator.equals, value: "3600" ), |
---|
141 | new Criterion( entity: "SamplingEvent", field: "startTime", operator: Operator.equals, value: "3600" ), |
---|
142 | ] |
---|
143 | |
---|
144 | def studySearch = new StudySearch(); |
---|
145 | |
---|
146 | // All criteria should result in 1 study with code 'abc' |
---|
147 | criteria.each { |
---|
148 | println "Criterion " + it |
---|
149 | studySearch.setCriteria( [ it ] ); |
---|
150 | studySearch.execute(); |
---|
151 | assert studySearch.getResults().size() == 1 |
---|
152 | assert studySearch.getResults()[0].code == "abc"; |
---|
153 | } |
---|
154 | } |
---|
155 | } |
---|
156 | |
---|
157 | void testExecuteNonExistingCriteria() { |
---|
158 | |
---|
159 | mockApplicationHolder.use { |
---|
160 | |
---|
161 | List criteria = [ |
---|
162 | new Criterion( entity: "Study", field: "title", operator: Operator.contains, value: "TNO-3" ), |
---|
163 | new Criterion( entity: "Subject", field: "name", operator: Operator.contains, value: "4" ), |
---|
164 | new Criterion( entity: "Sample", field: "name", operator: Operator.contains, value: "5" ), |
---|
165 | new Criterion( entity: "Assay", field: "name", operator: Operator.contains, value: "6" ), |
---|
166 | new Criterion( entity: "Event", field: "startTime", operator: Operator.equals, value: "4800" ), |
---|
167 | new Criterion( entity: "SamplingEvent", field: "startTime", operator: Operator.equals, value: "360" ), |
---|
168 | ] |
---|
169 | |
---|
170 | def studySearch = new StudySearch(); |
---|
171 | |
---|
172 | // All criteria should result in 1 study with code 'abc' |
---|
173 | criteria.each { |
---|
174 | println "Criterion " + it |
---|
175 | studySearch.setCriteria( [ it ] ); |
---|
176 | studySearch.execute(); |
---|
177 | assert studySearch.getResults().size() == 0 |
---|
178 | } |
---|
179 | |
---|
180 | } |
---|
181 | } |
---|
182 | } |
---|