blob: f71eabbde0ccd1c910eb28173c6202f802f8cd1e [file] [log] [blame]
Joachim Bingel43a444a2014-08-07 15:06:02 +00001import static org.junit.Assert.*;
Michael Hanlf1fead42014-05-14 15:13:33 +00002import de.ids_mannheim.korap.query.serialize.CollectionQueryTree;
Michael Hanlf1fead42014-05-14 15:13:33 +00003import de.ids_mannheim.korap.util.QueryException;
Michael Hanlf1fead42014-05-14 15:13:33 +00004import org.junit.Test;
5
6public class CollectionQueryTreeTest {
7
Joachim Bingela3f51f72014-07-22 14:45:31 +00008 CollectionQueryTree cqt;
9 String map;
10 private String query;
11 private String expected;
Michael Hanlf1fead42014-05-14 15:13:33 +000012
Joachim Bingela3f51f72014-07-22 14:45:31 +000013 private boolean equalsQueryContent(String res, String query) throws QueryException {
14 res = res.replaceAll(" ", "");
15 cqt = new CollectionQueryTree();
16 cqt.process(query);
17 String queryMap = cqt.getRequestMap().get("query").toString().replaceAll(" ", "");
18 return res.equals(queryMap);
19 }
Michael Hanlf1fead42014-05-14 15:13:33 +000020
Joachim Bingela3f51f72014-07-22 14:45:31 +000021 @Test
22 public void testSimple() throws QueryException {
23 query = "textClass=Sport";
24 // String regex1 = "{@type=korap:filter, filter={@type=korap:doc, attribute=textClass, key=Sport, match=match:eq}}";
Joachim Bingel43a444a2014-08-07 15:06:02 +000025 expected = "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000026 cqt = new CollectionQueryTree();
27 cqt.process(query);
28 map = cqt.getRequestMap().toString();
29 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
30
31 query = "textClass!=Sport";
32 // String regex1 = "{@type=korap:filter, filter={@type=korap:doc, attribute=textClass, key=Sport, match=match:eq}}";
Joachim Bingel43a444a2014-08-07 15:06:02 +000033 expected = "{@type=korap:doc, key=textClass, value=Sport, match=match:ne}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000034 cqt = new CollectionQueryTree();
35 cqt.process(query);
36 map = cqt.getRequestMap().toString();
37 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
38 }
39
40 @Test
41 public void testTwoConjuncts() throws QueryException {
42 query = "textClass=Sport & year=2014";
43 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000044 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000045 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingeldf22b922014-10-01 09:42:33 +000046 "{@type=korap:doc, key=year, value={@type=xsd:date, @value=2014}, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000047 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000048 cqt = new CollectionQueryTree();
49 cqt.process(query);
50 map = cqt.getRequestMap().toString();
51 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
52 }
53
54 @Test
55 public void testThreeConjuncts() throws QueryException {
56 query = "textClass=Sport & year=2014 & corpusID=WPD";
57 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000058 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000059 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +000060 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingeldf22b922014-10-01 09:42:33 +000061 "{@type=korap:doc, key=year, value={@type=xsd:date, @value=2014}, match=match:eq}," +
Joachim Bingela3f51f72014-07-22 14:45:31 +000062 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
63 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000064 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000065 cqt = new CollectionQueryTree();
66 cqt.process(query);
67 map = cqt.getRequestMap().toString();
68 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
69 }
70
71
72 @Test
73 public void testTwoDisjuncts() throws QueryException {
74 query = "textClass=Sport | year=2014";
75 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000076 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000077 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingeldf22b922014-10-01 09:42:33 +000078 "{@type=korap:doc, key=year, value={@type=xsd:date, @value=2014}, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000079 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000080 cqt = new CollectionQueryTree();
81 cqt.process(query);
82 map = cqt.getRequestMap().toString();
83 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
84 }
85
86 @Test
87 public void testThreeDisjuncts() throws QueryException {
88 query = "textClass=Sport | year=2014 | corpusID=WPD";
89 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000090 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000091 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +000092 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingeldf22b922014-10-01 09:42:33 +000093 "{@type=korap:doc, key=year, value={@type=xsd:date, @value=2014}, match=match:eq}," +
Joachim Bingela3f51f72014-07-22 14:45:31 +000094 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
95 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000096 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000097 cqt = new CollectionQueryTree();
98 cqt.process(query);
99 map = cqt.getRequestMap().toString();
100 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
101 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000102
103
Joachim Bingela3f51f72014-07-22 14:45:31 +0000104 @Test
105 public void testMixed() throws QueryException {
106 query = "(textClass=Sport | textClass=ausland) & corpusID=WPD";
107 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000108
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000109 "{@type=korap:docGroup, operation=operation:and, operands=[" +
110 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000111 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
112 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
113 "]}," +
114 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000115 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000116 cqt = new CollectionQueryTree();
117 cqt.process(query);
118 map = cqt.getRequestMap().toString();
119 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
120
121 query = "(textClass=Sport & textClass=ausland) & corpusID=WPD";
122 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000123
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000124 "{@type=korap:docGroup, operation=operation:and, operands=[" +
125 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000126 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
127 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
128 "]}," +
129 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000130 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000131 cqt = new CollectionQueryTree();
132 cqt.process(query);
133 map = cqt.getRequestMap().toString();
134 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
135
136 query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White)";
137 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000138
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000139 "{@type=korap:docGroup, operation=operation:or, operands=[" +
140 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000141 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
142 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
143 "]}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000144 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000145 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," +
146 "{@type=korap:doc, key=author, value=White, match=match:eq}" +
147 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000148 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000149 cqt = new CollectionQueryTree();
150 cqt.process(query);
151 map = cqt.getRequestMap().toString();
152 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel43a444a2014-08-07 15:06:02 +0000153
154 query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White & year=2010)";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000155 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000156 "{@type=korap:docGroup, operation=operation:or, operands=[" +
157 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000158 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
159 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
160 "]}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000161 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000162 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000163 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000164 "{@type=korap:doc, key=author, value=White, match=match:eq}," +
Joachim Bingeldf22b922014-10-01 09:42:33 +0000165 "{@type=korap:doc, key=year, value={@type=xsd:date, @value=2010}, match=match:eq}" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000166 "]}" +
167 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000168 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000169 cqt = new CollectionQueryTree();
170 cqt.process(query);
171 map = cqt.getRequestMap().toString();
172 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
173 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000174
Joachim Bingela3f51f72014-07-22 14:45:31 +0000175 @Test
176 public void testDate() throws QueryException {
177 // search for pubDate between 1990 and 2010!
178 query = "1990<pubDate<2010";
179 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000180 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingeldf22b922014-10-01 09:42:33 +0000181 "{@type=korap:doc, key=pubDate, value={@type=xsd:date, @value=1990}, match=match:gt}," +
182 "{@type=korap:doc, key=pubDate, value={@type=xsd:date, @value=2010}, match=match:lt}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000183 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000184 cqt = new CollectionQueryTree();
185 cqt.process(query);
186 map = cqt.getRequestMap().toString();
187 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
188
189 query = "pubDate>=1990";
190 expected =
Joachim Bingeldf22b922014-10-01 09:42:33 +0000191 "{@type=korap:doc, key=pubDate, value={@type=xsd:date, @value=1990}, match=match:geq}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000192 cqt = new CollectionQueryTree();
193 cqt.process(query);
194 map = cqt.getRequestMap().toString();
195 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel624854b2014-07-23 13:53:28 +0000196
197 query = "pubDate>=1990-05";
198 expected =
Joachim Bingeldf22b922014-10-01 09:42:33 +0000199 "{@type=korap:doc, key=pubDate, value={@type=xsd:date, @value=1990-05}, match=match:geq}";
Joachim Bingel624854b2014-07-23 13:53:28 +0000200 cqt = new CollectionQueryTree();
201 cqt.process(query);
202 map = cqt.getRequestMap().toString();
203 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
204
205 query = "pubDate>=1990-05-01";
206 expected =
Joachim Bingeldf22b922014-10-01 09:42:33 +0000207 "{@type=korap:doc, key=pubDate, value={@type=xsd:date, @value=1990-05-01}, match=match:geq}";
Joachim Bingel624854b2014-07-23 13:53:28 +0000208 cqt = new CollectionQueryTree();
209 cqt.process(query);
210 map = cqt.getRequestMap().toString();
211 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingela3f51f72014-07-22 14:45:31 +0000212 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000213
Joachim Bingela3f51f72014-07-22 14:45:31 +0000214 @Test
215 public void testRegex() throws QueryException {
216 query = "author=/Go.*he/";
217 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000218 "{@type=korap:doc, key=author, value=Go.*he, type=type:regex, match=match:eq}";
219 cqt = new CollectionQueryTree();
220 cqt.process(query);
221 map = cqt.getRequestMap().toString();
222 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
223 }
224
225 @Test
226 public void testContentFilter() throws QueryException {
227 query = "[base=Schwalbe]";
228 expected =
229 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}";
230 cqt = new CollectionQueryTree();
231 cqt.process(query);
232 map = cqt.getRequestMap().toString();
233 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
234
235 query = "[cnx/base=Schwalbe]";
236 expected =
237 "{@type=korap:token, wrap={@type=korap:term, foundry=cnx, layer=lemma, key=Schwalbe, match=match:eq}}";
238 cqt = new CollectionQueryTree();
239 cqt.process(query);
240 map = cqt.getRequestMap().toString();
241 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
242
243 query = "[base!=Schwalbe]";
244 expected =
245 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:ne}}";
246 cqt = new CollectionQueryTree();
247 cqt.process(query);
248 map = cqt.getRequestMap().toString();
249 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
250
251 query = "[base=Schwalbe] & [orth=Foul]";
252 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000253 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000254 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}," +
255 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Foul, match=match:eq}}" +
256 "]}";
257 cqt = new CollectionQueryTree();
258 cqt.process(query);
259 map = cqt.getRequestMap().toString();
260 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
261 }
262
263 @Test
264 public void testContentMetaMixed() throws QueryException {
265 query = "textClass=Sport & [base=Schwalbe]";
266 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000267 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000268 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
269 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}" +
270 "]}";
271 cqt = new CollectionQueryTree();
272 cqt.process(query);
273 map = cqt.getRequestMap().toString();
274 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
275
276 query = "[base=Schwalbe] & textClass=Sport";
277 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000278 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000279 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}," +
280 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}" +
281 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000282 cqt = new CollectionQueryTree();
283 cqt.process(query);
284 map = cqt.getRequestMap().toString();
285 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
286 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000287
288}
289