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 | |
---|
8 | /** |
---|
9 | * SearchTests Test |
---|
10 | * |
---|
11 | * Description of my test |
---|
12 | * |
---|
13 | * @author your email (+name?) |
---|
14 | * @since 2010mmdd |
---|
15 | * @package ??? |
---|
16 | * |
---|
17 | * Revision information: |
---|
18 | * $Rev: 1478 $ |
---|
19 | * $Author: s.h.sikkema@gmail.com $ |
---|
20 | * $Date: 2011-02-02 12:52:01 +0000 (wo, 02 feb 2011) $ |
---|
21 | */ |
---|
22 | class StudySearchTests extends GrailsUnitTestCase { |
---|
23 | protected void setUp() { |
---|
24 | super.setUp() |
---|
25 | |
---|
26 | 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' ) ]; |
---|
27 | def subjects = [ new Subject( name: 'Subject 1', parent: studies[0] ), new Subject( name: 'Subject 2', parent: studies[1] ) ] |
---|
28 | def events = [ new Event( startTime: 3600, parent: studies[0] ), new Event( startTime: 7200, parent: studies[1] ) ] |
---|
29 | def samplingEvents = [ new SamplingEvent( startTime: 3600, parent: studies[0] ), new SamplingEvent( startTime: 7200, parent: studies[1] ) ] |
---|
30 | def samples = [ |
---|
31 | new Sample( name: 'Sample 1', parent: studies[0], parentSubject: subjects[ 0 ], parentEvent: samplingEvents[ 0 ] ), |
---|
32 | new Sample( name: 'Sample 2', parent: studies[1], parentSubject: subjects[ 1 ], parentEvent: samplingEvents[ 1 ] ) |
---|
33 | ] |
---|
34 | def assays = [ new Assay( name: 'Assay 1', parent: studies[0], samples: [samples[0]] ), new Assay( name: 'Assay 2', parent: studies[1], samples: [samples[1]] ) ] |
---|
35 | |
---|
36 | mockDomain( Study, studies ); |
---|
37 | mockDomain( Subject, subjects ); |
---|
38 | mockDomain( Sample, samples ); |
---|
39 | mockDomain( Event, events ); |
---|
40 | mockDomain( SamplingEvent, samplingEvents); |
---|
41 | mockDomain( Assay, assays ); |
---|
42 | |
---|
43 | mockDomain( AssayModule ); |
---|
44 | |
---|
45 | subjects.each { it.parent.addToSubjects( it ); } |
---|
46 | samples.each { it.parent.addToSamples( it ); } |
---|
47 | events.each { it.parent.addToEvents( it ); } |
---|
48 | samplingEvents.each { it.parent.addToSamplingEvents( it ); } |
---|
49 | samples.each { it.parent.addToSamples( it ); } |
---|
50 | assays.each { it.parent.addToAssays( it ); } |
---|
51 | |
---|
52 | // some mocks to make sure test doesn't break on finding 'moduleCommunicationService' |
---|
53 | ApplicationHolder.metaClass.static.getApplication = { [getMainContext: { [getBean: {a -> null}] }] } |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | protected void tearDown() { |
---|
58 | super.tearDown() |
---|
59 | } |
---|
60 | |
---|
61 | void testExecute() { |
---|
62 | |
---|
63 | List criteria = [ |
---|
64 | new Criterion( entity: "Study", field: "title", operator: Operator.contains, value: "TNO" ), |
---|
65 | new Criterion( entity: "Study", field: "code", operator: Operator.equals, value: "abc" ), |
---|
66 | new Criterion( entity: "Study", field: "code", operator: Operator.equals, value: "ghi" ) |
---|
67 | ] |
---|
68 | |
---|
69 | def studySearch = new StudySearch(); |
---|
70 | |
---|
71 | // Search without criteria |
---|
72 | studySearch.setCriteria( ); |
---|
73 | studySearch.execute(); |
---|
74 | |
---|
75 | assert studySearch.getResults().size() == 4 |
---|
76 | assert studySearch.getResults()[0] instanceof Study |
---|
77 | assert studySearch.getResults()*.code.contains( "abc" ); |
---|
78 | assert studySearch.getResults()*.code.contains( "def" ); |
---|
79 | assert studySearch.getResults()*.code.contains( "ghi" ); |
---|
80 | assert studySearch.getResults()*.code.contains( "jkl" ); |
---|
81 | |
---|
82 | studySearch.setCriteria( [ criteria[0] ] ); |
---|
83 | studySearch.execute(); |
---|
84 | assert studySearch.getResults().size() == 2 |
---|
85 | |
---|
86 | assert studySearch.getResults()*.code.contains( "abc" ); |
---|
87 | assert studySearch.getResults()*.code.contains( "def" ); |
---|
88 | |
---|
89 | studySearch.setCriteria( [ criteria[0], criteria[1] ] ); |
---|
90 | studySearch.execute(); |
---|
91 | assert studySearch.getResults().size() == 1 |
---|
92 | assert studySearch.getResults()[0].code == "abc" |
---|
93 | |
---|
94 | studySearch.setCriteria( [ criteria[0], criteria[2] ] ); |
---|
95 | studySearch.execute(); |
---|
96 | assert studySearch.getResults().size() == 0 |
---|
97 | |
---|
98 | studySearch.setCriteria( [ criteria[1], criteria[2] ] ); |
---|
99 | studySearch.execute(); |
---|
100 | assert studySearch.getResults().size() == 0 |
---|
101 | } |
---|
102 | |
---|
103 | void testExecuteDifferentCriteria() { |
---|
104 | List criteria = [ |
---|
105 | new Criterion( entity: "Study", field: "title", operator: Operator.contains, value: "TNO-1" ), |
---|
106 | new Criterion( entity: "Subject", field: "name", operator: Operator.contains, value: "1" ), |
---|
107 | new Criterion( entity: "Sample", field: "name", operator: Operator.contains, value: "1" ), |
---|
108 | new Criterion( entity: "Assay", field: "name", operator: Operator.contains, value: "1" ), |
---|
109 | new Criterion( entity: "Event", field: "startTime", operator: Operator.equals, value: "3600" ), |
---|
110 | new Criterion( entity: "SamplingEvent", field: "startTime", operator: Operator.equals, value: "3600" ), |
---|
111 | ] |
---|
112 | |
---|
113 | def studySearch = new StudySearch(); |
---|
114 | |
---|
115 | // All criteria should result in 1 study with code 'abc' |
---|
116 | criteria.each { |
---|
117 | println "Criterion " + it |
---|
118 | studySearch.setCriteria( [ it ] ); |
---|
119 | studySearch.execute(); |
---|
120 | assert studySearch.getResults().size() == 1 |
---|
121 | assert studySearch.getResults()[0].code == "abc"; |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | void testExecuteNonExistingCriteria() { |
---|
126 | List criteria = [ |
---|
127 | new Criterion( entity: "Study", field: "title", operator: Operator.contains, value: "TNO-3" ), |
---|
128 | new Criterion( entity: "Subject", field: "name", operator: Operator.contains, value: "4" ), |
---|
129 | new Criterion( entity: "Sample", field: "name", operator: Operator.contains, value: "5" ), |
---|
130 | new Criterion( entity: "Assay", field: "name", operator: Operator.contains, value: "6" ), |
---|
131 | new Criterion( entity: "Event", field: "startTime", operator: Operator.equals, value: "4800" ), |
---|
132 | new Criterion( entity: "SamplingEvent", field: "startTime", operator: Operator.equals, value: "360" ), |
---|
133 | ] |
---|
134 | |
---|
135 | def studySearch = new StudySearch(); |
---|
136 | |
---|
137 | // All criteria should result in 1 study with code 'abc' |
---|
138 | criteria.each { |
---|
139 | println "Criterion " + it |
---|
140 | studySearch.setCriteria( [ it ] ); |
---|
141 | studySearch.execute(); |
---|
142 | assert studySearch.getResults().size() == 0 |
---|
143 | } |
---|
144 | } |
---|
145 | } |
---|