blob: 349b98ad80225afe90f27037c302c79f603b9cf9 [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 @Test
14 public void testSimple() throws QueryException {
15 query = "textClass=Sport";
16 // String regex1 = "{@type=korap:filter, filter={@type=korap:doc, attribute=textClass, key=Sport, match=match:eq}}";
Joachim Bingel43a444a2014-08-07 15:06:02 +000017 expected = "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000018 cqt = new CollectionQueryTree();
19 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000020 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000021 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
22
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:ne}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000026 cqt = new CollectionQueryTree();
27 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000028 map = cqt.getRequestMap().get("collection").toString();
29 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
30 }
31
32 @Test
33 public void testContains() throws QueryException {
34 query = "title~Mannheim";
35 expected =
36 "{@type=korap:doc, key=title, value=Mannheim, match=match:contains}";
37 cqt = new CollectionQueryTree();
38 cqt.process(query);
39 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000040 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
41 }
42
43 @Test
44 public void testTwoConjuncts() throws QueryException {
45 query = "textClass=Sport & year=2014";
46 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000047 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000048 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000049 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000050 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000051 cqt = new CollectionQueryTree();
52 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000053 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000054 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
55 }
56
57 @Test
58 public void testThreeConjuncts() throws QueryException {
59 query = "textClass=Sport & year=2014 & corpusID=WPD";
60 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000061 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000062 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +000063 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000064 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}," +
Joachim Bingela3f51f72014-07-22 14:45:31 +000065 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
66 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000067 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000068 cqt = new CollectionQueryTree();
69 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000070 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000071 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
72 }
73
74
75 @Test
76 public void testTwoDisjuncts() throws QueryException {
77 query = "textClass=Sport | year=2014";
78 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000079 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000080 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000081 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000082 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +000083 cqt = new CollectionQueryTree();
84 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +000085 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +000086 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
87 }
88
89 @Test
90 public void testThreeDisjuncts() throws QueryException {
91 query = "textClass=Sport | year=2014 | corpusID=WPD";
92 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +000093 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +000094 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +000095 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +000096 "{@type=korap:doc, key=year, type=type:date, value=2014, match=match:eq}," +
Joachim Bingela3f51f72014-07-22 14:45:31 +000097 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
98 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +000099 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000100 cqt = new CollectionQueryTree();
101 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000102 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000103 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
104 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000105
106
Joachim Bingela3f51f72014-07-22 14:45:31 +0000107 @Test
108 public void testMixed() throws QueryException {
109 query = "(textClass=Sport | textClass=ausland) & corpusID=WPD";
110 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000111
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000112 "{@type=korap:docGroup, operation=operation:and, operands=[" +
113 "{@type=korap:docGroup, operation=operation:or, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000114 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
115 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
116 "]}," +
117 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000118 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000119 cqt = new CollectionQueryTree();
120 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000121 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000122 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
123
124 query = "(textClass=Sport & textClass=ausland) & corpusID=WPD";
125 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000126
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000127 "{@type=korap:docGroup, operation=operation:and, operands=[" +
128 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000129 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
130 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
131 "]}," +
132 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000133 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000134 cqt = new CollectionQueryTree();
135 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000136 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000137 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
138
139 query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White)";
140 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000141
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000142 "{@type=korap:docGroup, operation=operation:or, operands=[" +
143 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000144 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
145 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
146 "]}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000147 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000148 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," +
149 "{@type=korap:doc, key=author, value=White, match=match:eq}" +
150 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000151 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000152 cqt = new CollectionQueryTree();
153 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000154 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000155 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel43a444a2014-08-07 15:06:02 +0000156
157 query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White & year=2010)";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000158 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000159 "{@type=korap:docGroup, operation=operation:or, operands=[" +
160 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000161 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
162 "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" +
163 "]}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000164 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000165 "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," +
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000166 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000167 "{@type=korap:doc, key=author, value=White, match=match:eq}," +
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000168 "{@type=korap:doc, key=year, type=type:date, value=2010, match=match:eq}" +
Joachim Bingela3f51f72014-07-22 14:45:31 +0000169 "]}" +
170 "]}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000171 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000172 cqt = new CollectionQueryTree();
173 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000174 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000175 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
176 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000177
Joachim Bingela3f51f72014-07-22 14:45:31 +0000178 @Test
179 public void testDate() throws QueryException {
180 // search for pubDate between 1990 and 2010!
181 query = "1990<pubDate<2010";
182 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000183 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000184 "{@type=korap:doc, key=pubDate, type=type:date, value=1990, match=match:gt}," +
185 "{@type=korap:doc, key=pubDate, type=type:date, value=2010, match=match:lt}" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000186 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000187 cqt = new CollectionQueryTree();
188 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000189 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000190 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
191
192 query = "pubDate>=1990";
193 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000194 "{@type=korap:doc, key=pubDate, type=type:date, value=1990, match=match:geq}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000195 cqt = new CollectionQueryTree();
196 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000197 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000198 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel624854b2014-07-23 13:53:28 +0000199
200 query = "pubDate>=1990-05";
201 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000202 "{@type=korap:doc, key=pubDate, type=type:date, value=1990-05, match=match:geq}";
Joachim Bingel624854b2014-07-23 13:53:28 +0000203 cqt = new CollectionQueryTree();
204 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000205 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel624854b2014-07-23 13:53:28 +0000206 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
207
208 query = "pubDate>=1990-05-01";
209 expected =
Joachim Bingel49da7ca2014-10-07 12:26:14 +0000210 "{@type=korap:doc, key=pubDate, type=type:date, value=1990-05-01, match=match:geq}";
Joachim Bingel624854b2014-07-23 13:53:28 +0000211 cqt = new CollectionQueryTree();
212 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000213 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel624854b2014-07-23 13:53:28 +0000214 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingela3f51f72014-07-22 14:45:31 +0000215 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000216
Joachim Bingela3f51f72014-07-22 14:45:31 +0000217 @Test
218 public void testRegex() throws QueryException {
219 query = "author=/Go.*he/";
220 expected =
Joachim Bingel43a444a2014-08-07 15:06:02 +0000221 "{@type=korap:doc, key=author, value=Go.*he, type=type:regex, match=match:eq}";
222 cqt = new CollectionQueryTree();
223 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000224 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000225 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
226 }
227
228 @Test
229 public void testContentFilter() throws QueryException {
230 query = "[base=Schwalbe]";
231 expected =
232 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}";
233 cqt = new CollectionQueryTree();
234 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000235 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000236 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
237
238 query = "[cnx/base=Schwalbe]";
239 expected =
240 "{@type=korap:token, wrap={@type=korap:term, foundry=cnx, layer=lemma, key=Schwalbe, match=match:eq}}";
241 cqt = new CollectionQueryTree();
242 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000243 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000244 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
245
246 query = "[base!=Schwalbe]";
247 expected =
248 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:ne}}";
249 cqt = new CollectionQueryTree();
250 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000251 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000252 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
253
254 query = "[base=Schwalbe] & [orth=Foul]";
255 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000256 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000257 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}," +
258 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Foul, match=match:eq}}" +
259 "]}";
260 cqt = new CollectionQueryTree();
261 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000262 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000263 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
264 }
265
266 @Test
267 public void testContentMetaMixed() throws QueryException {
268 query = "textClass=Sport & [base=Schwalbe]";
269 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000270 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000271 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," +
272 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}" +
273 "]}";
274 cqt = new CollectionQueryTree();
275 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000276 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingel43a444a2014-08-07 15:06:02 +0000277 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
278
279 query = "[base=Schwalbe] & textClass=Sport";
280 expected =
Joachim Bingelb895c1f2014-09-29 09:31:05 +0000281 "{@type=korap:docGroup, operation=operation:and, operands=[" +
Joachim Bingel43a444a2014-08-07 15:06:02 +0000282 "{@type=korap:token, wrap={@type=korap:term, layer=lemma, key=Schwalbe, match=match:eq}}," +
283 "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}" +
284 "]}";
Joachim Bingela3f51f72014-07-22 14:45:31 +0000285 cqt = new CollectionQueryTree();
286 cqt.process(query);
Joachim Bingela499e922014-10-08 13:32:50 +0000287 map = cqt.getRequestMap().get("collection").toString();
Joachim Bingela3f51f72014-07-22 14:45:31 +0000288 assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", ""));
289 }
Michael Hanlf1fead42014-05-14 15:13:33 +0000290
291}
292