blob: 27a3dd7a442aea07f08fc8b5effddbb59781bec0 [file] [log] [blame]
Akron60dfa7e2015-08-03 22:15:17 +02001package de.ids_mannheim.korap.collection;
2
3import java.util.*;
4import java.io.*;
5
6import de.ids_mannheim.korap.collection.CollectionBuilder;
7
8import static org.junit.Assert.*;
9import org.junit.Test;
10import org.junit.Ignore;
11import org.junit.runner.RunWith;
12import org.junit.runners.JUnit4;
13
14// TODO: More extensive testing!
15
16
17@RunWith(JUnit4.class)
18public class TestCollectionBuilder {
19
20 @Test
21 public void builderTerm () throws IOException {
22 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +020023 assertEquals("author:tree", kc.term("author", "tree").toString());
Akron60dfa7e2015-08-03 22:15:17 +020024 };
25
Akron40550172015-08-04 03:06:12 +020026
Akron60dfa7e2015-08-03 22:15:17 +020027 @Test
28 public void builderRegex () throws IOException {
29 CollectionBuilder kc = new CollectionBuilder();
30 assertEquals("QueryWrapperFilter(author:/tre*?/)",
Akron40550172015-08-04 03:06:12 +020031 kc.re("author", "tre*?").toString());
Akron60dfa7e2015-08-03 22:15:17 +020032 };
33
Akron40550172015-08-04 03:06:12 +020034
Akron60dfa7e2015-08-03 22:15:17 +020035 @Test
36 public void builderDateYear () throws IOException {
37 CollectionBuilder kc = new CollectionBuilder();
38 assertEquals("pubDate:[20050000 TO 20059999]",
Akron40550172015-08-04 03:06:12 +020039 kc.date("pubDate", "2005").toString());
Akron60dfa7e2015-08-03 22:15:17 +020040 };
41
Akron40550172015-08-04 03:06:12 +020042
Akron60dfa7e2015-08-03 22:15:17 +020043 @Test
44 public void builderDateMonth () throws IOException {
45 CollectionBuilder kc = new CollectionBuilder();
46 assertEquals("pubDate:[20051000 TO 20051099]",
Akron40550172015-08-04 03:06:12 +020047 kc.date("pubDate", "2005-10").toString());
Akron60dfa7e2015-08-03 22:15:17 +020048 };
49
Akron40550172015-08-04 03:06:12 +020050
Akron60dfa7e2015-08-03 22:15:17 +020051 @Test
52 public void builderDateDay () throws IOException {
53 CollectionBuilder kc = new CollectionBuilder();
54 assertEquals("pubDate:[20051011 TO 20051011]",
Akron40550172015-08-04 03:06:12 +020055 kc.date("pubDate", "2005-10-11").toString());
Akron60dfa7e2015-08-03 22:15:17 +020056 };
57
Akron40550172015-08-04 03:06:12 +020058
Akron60dfa7e2015-08-03 22:15:17 +020059 @Test
60 public void builderDateBorders () throws IOException {
61 CollectionBuilder kc = new CollectionBuilder();
62 // CollectionBuilderNew.Interface kbi = ;
63 assertNull(kc.date("pubDate", ""));
64
65 assertEquals("pubDate:[20051580 TO 20051580]",
Akron40550172015-08-04 03:06:12 +020066 kc.date("pubDate", "2005-15-80").toString());
Akron60dfa7e2015-08-03 22:15:17 +020067
68 assertNull(kc.date("pubDate", "2005-15-8"));
69 assertNull(kc.date("pubDate", "2005-5-18"));
70 assertNull(kc.date("pubDate", "200-05-18"));
71 };
72
Akron40550172015-08-04 03:06:12 +020073
Akron60dfa7e2015-08-03 22:15:17 +020074 @Test
75 public void builderSince () throws IOException {
76 CollectionBuilder kc = new CollectionBuilder();
77 assertEquals("pubDate:[20050000 TO 99999999]",
Akron40550172015-08-04 03:06:12 +020078 kc.since("pubDate", "2005").toString());
Akron60dfa7e2015-08-03 22:15:17 +020079
80 assertEquals("pubDate:[20051000 TO 99999999]",
Akron40550172015-08-04 03:06:12 +020081 kc.since("pubDate", "2005-10").toString());
Akron60dfa7e2015-08-03 22:15:17 +020082
83 assertEquals("pubDate:[20051012 TO 99999999]",
Akron40550172015-08-04 03:06:12 +020084 kc.since("pubDate", "2005-10-12").toString());
Akron60dfa7e2015-08-03 22:15:17 +020085 };
86
87
88 @Test
89 public void builderTill () throws IOException {
90 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +020091 assertEquals("pubDate:[0 TO 20059999]", kc.till("pubDate", "2005")
92 .toString());
Akron60dfa7e2015-08-03 22:15:17 +020093
Akron40550172015-08-04 03:06:12 +020094 assertEquals("pubDate:[0 TO 20051299]", kc.till("pubDate", "2005-12")
95 .toString());
Akron60dfa7e2015-08-03 22:15:17 +020096
Akron40550172015-08-04 03:06:12 +020097 assertEquals("pubDate:[0 TO 20051204]", kc
98 .till("pubDate", "2005-12-04").toString());
Akron60dfa7e2015-08-03 22:15:17 +020099 };
100
101
102 @Test
103 public void builderAndSimple () throws IOException {
104 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200105 assertEquals("author:tree",
106 kc.andGroup().with(kc.term("author", "tree")).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200107 };
108
Akron40550172015-08-04 03:06:12 +0200109
Akron60dfa7e2015-08-03 22:15:17 +0200110 @Test
111 public void builderOrSimple () throws IOException {
112 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200113 assertEquals("author:tree", kc.orGroup()
114 .with(kc.term("author", "tree")).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200115 };
116
Akron40550172015-08-04 03:06:12 +0200117
Akron60dfa7e2015-08-03 22:15:17 +0200118 @Test
119 public void builderAndCombined () throws IOException {
120 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200121 assertEquals(
122 "AndGroup(author:tree title:name)",
123 kc.andGroup().with(kc.term("author", "tree"))
124 .with(kc.term("title", "name")).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200125 };
126
Akron40550172015-08-04 03:06:12 +0200127
Akron60dfa7e2015-08-03 22:15:17 +0200128 @Test
129 public void builderAndNestedSimple () throws IOException {
130 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200131 assertEquals(
132 "AndGroup(author:tree title:name)",
133 kc.andGroup()
134 .with(kc.andGroup().with(kc.term("author", "tree"))
135 .with(kc.term("title", "name"))).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200136 };
137
138
139 @Test
140 public void builderOrCombined () throws IOException {
141 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200142 assertEquals(
143 "OrGroup(author:tree title:name)",
144 kc.orGroup().with(kc.term("author", "tree"))
145 .with(kc.term("title", "name")).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200146 };
147
Akron40550172015-08-04 03:06:12 +0200148
Akron60dfa7e2015-08-03 22:15:17 +0200149 @Test
150 public void builderOrNestedSimple () throws IOException {
151 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200152 assertEquals(
153 "OrGroup(author:tree title:name)",
154 kc.orGroup()
155 .with(kc.orGroup().with(kc.term("author", "tree"))
156 .with(kc.term("title", "name"))).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200157 };
158
Akron40550172015-08-04 03:06:12 +0200159
Akron60dfa7e2015-08-03 22:15:17 +0200160 @Test
161 public void builderGroups () throws IOException {
162 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200163 String g = kc
164 .orGroup()
165 .with(kc.orGroup().with(kc.term("author", "tree1"))
166 .with(kc.term("title", "name1")))
167 .with(kc.andGroup().with(kc.term("author", "tree2"))
168 .with(kc.term("title", "name2"))).toString();
169 assertEquals(
170 "OrGroup(OrGroup(author:tree1 title:name1) AndGroup(author:tree2 title:name2))",
171 g);
Akron60dfa7e2015-08-03 22:15:17 +0200172 };
173
Akron40550172015-08-04 03:06:12 +0200174
Akron60dfa7e2015-08-03 22:15:17 +0200175 @Test
176 public void builderNegationRoot () throws IOException {
177 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200178 CollectionBuilder.Interface kbi = kc.orGroup()
179 .with(kc.term("author", "tree1"))
180 .with(kc.term("title", "name1"));
181 assertEquals("OrGroup(author:tree1 title:name1)", kbi.toString());
Akron60dfa7e2015-08-03 22:15:17 +0200182 assertFalse(kbi.isNegative());
183
Akron40550172015-08-04 03:06:12 +0200184 kbi = kc.andGroup()
185 .with(kc.orGroup().with(kc.term("author", "tree1"))
186 .with(kc.term("title", "name1"))).not();
Akron60dfa7e2015-08-03 22:15:17 +0200187 assertEquals("OrGroup(author:tree1 title:name1)", kbi.toString());
188 assertTrue(kbi.isNegative());
189 };
190
191
192 @Test
193 public void builderNegation () throws IOException {
194 CollectionBuilder kc = new CollectionBuilder();
Akron40550172015-08-04 03:06:12 +0200195 CollectionBuilder.Interface kbi = kc.term("author", "tree").not();
Akron60dfa7e2015-08-03 22:15:17 +0200196 assertEquals("author:tree", kbi.toString());
197 assertTrue(kbi.isNegative());
198
199 kbi = kc.andGroup().with(kc.term("author", "tree").not());
200 assertEquals("author:tree", kbi.toString());
201 assertTrue(kbi.isNegative());
202
203 kbi = kc.orGroup().with(kc.term("author", "tree").not());
204 assertEquals("author:tree", kbi.toString());
205 assertTrue(kbi.isNegative());
206 };
207
Akron40550172015-08-04 03:06:12 +0200208
Akron60dfa7e2015-08-03 22:15:17 +0200209 // The legacy tests were adopted from the legacy collection builder and reformuated
210
211 @Test
212 public void LegacyFilterExample () throws IOException {
213 CollectionBuilder kf = new CollectionBuilder();
214
215 /*
216 assertEquals("+textClass:tree", kf.and("textClass", "tree").toString());
217 */
Akron40550172015-08-04 03:06:12 +0200218 assertEquals("textClass:tree",
219 kf.andGroup().with(kf.term("textClass", "tree")).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200220
221 /*
222 assertEquals("+textClass:tree +textClass:sport",
223 kf.and("textClass", "tree").and("textClass", "sport")
224 .toString());
225 */
Akron40550172015-08-04 03:06:12 +0200226 assertEquals(
227 "AndGroup(textClass:tree textClass:sport)",
228 kf.andGroup().with(kf.term("textClass", "tree"))
229 .with(kf.term("textClass", "sport")).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200230
231 /*
232 assertEquals(
233 "+textClass:tree +textClass:sport textClass:news",
234 kf.and("textClass", "tree").and("textClass", "sport")
235 .or("textClass", "news").toString());
236 */
Akron40550172015-08-04 03:06:12 +0200237 assertEquals(
238 "OrGroup(AndGroup(textClass:tree textClass:sport) textClass:news)",
239 kf.orGroup()
240 .with(kf.andGroup().with("textClass", "tree")
241 .with("textClass", "sport"))
242 .with("textClass", "news").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200243
244 /*
245 assertEquals("+textClass:tree +textClass:sport +textClass:news", kf
246 .and("textClass", "tree", "sport", "news").toString());
247 */
Akron40550172015-08-04 03:06:12 +0200248 assertEquals(
249 "AndGroup(textClass:tree textClass:sport textClass:news)",
250 kf.andGroup().with("textClass", "tree")
251 .with("textClass", "sport").with("textClass", "news")
252 .toString());
Akron60dfa7e2015-08-03 22:15:17 +0200253
254 /*
255 assertEquals("corpusID:c-1 corpusID:c-2 corpusID:c-3",
256 kf.or("corpusID", "c-1", "c-2", "c-3").toString());
257 */
Akron40550172015-08-04 03:06:12 +0200258 assertEquals("OrGroup(corpusID:c-1 corpusID:c-2 corpusID:c-3)", kf
259 .orGroup().with("corpusID", "c-1").with("corpusID", "c-2")
260 .with("corpusID", "c-3").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200261
262 };
263
Akron40550172015-08-04 03:06:12 +0200264
Akron60dfa7e2015-08-03 22:15:17 +0200265 @Test
266 public void LegacyRangeExample () throws IOException {
267 CollectionBuilder kf = new CollectionBuilder();
268 /*
269 assertEquals("+pubDate:[20030604 TO 20030899]",
270 kf.between("2003-06-04", "2003-08-99").toString());
271 */
272 // This will be optimized and probably crash
Akron40550172015-08-04 03:06:12 +0200273 assertEquals(
274 "AndGroup(pubDate:[20030604 TO 99999999] pubDate:[0 TO 20030899])",
275 kf.andGroup().with(kf.since("pubDate", "2003-06-04"))
276 .with(kf.till("pubDate", "2003-08-99")).toString());
Akron60dfa7e2015-08-03 22:15:17 +0200277
278 /*
279 assertEquals("+pubDate:[0 TO 20030604]", kf.till("2003-06-04")
280 .toString());
281 */
Akron40550172015-08-04 03:06:12 +0200282 assertEquals("pubDate:[0 TO 20030604]", kf
283 .till("pubDate", "2003-06-04").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200284
285
286 /*
287 assertEquals("+pubDate:[20030604 TO 99999999]", kf.since("2003-06-04")
288 .toString());
289 */
Akron40550172015-08-04 03:06:12 +0200290 assertEquals("pubDate:[20030604 TO 99999999]",
291 kf.since("pubDate", "2003-06-04").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200292
293 /*
294 assertEquals("+pubDate:20030604", kf.date("2003-06-04").toString());
295 */
Akron40550172015-08-04 03:06:12 +0200296 assertEquals("pubDate:[20030604 TO 20030604]",
297 kf.date("pubDate", "2003-06-04").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200298 };
299
300
301 @Test
302 public void LegacyRangeLimited () throws IOException {
303 CollectionBuilder kf = new CollectionBuilder();
304 /*
305 assertEquals("+pubDate:[20050000 TO 20099999]",
306 kf.between("2005", "2009").toString());
307 */
Akron40550172015-08-04 03:06:12 +0200308 assertEquals(
309 "AndGroup(pubDate:[20050000 TO 99999999] pubDate:[0 TO 20099999])",
310 kf.between("pubDate", "2005", "2009").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200311
312 /*
313 assertEquals("+pubDate:[20051000 TO 20090899]",
314 kf.between("200510", "200908").toString());
315 */
Akron40550172015-08-04 03:06:12 +0200316 assertEquals(
317 "AndGroup(pubDate:[20051000 TO 99999999] pubDate:[0 TO 20090899])",
318 kf.between("pubDate", "200510", "200908").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200319
320 /*
321 assertEquals("+pubDate:[20051000 TO 20090899]",
322 kf.between("2005-10", "2009-08").toString());
323 */
Akron40550172015-08-04 03:06:12 +0200324 assertEquals(
325 "AndGroup(pubDate:[20051000 TO 99999999] pubDate:[0 TO 20090899])",
326 kf.between("pubDate", "2005-10", "2009-08").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200327
328
329 /*
330 assertEquals("+pubDate:[20051006 TO 20090803]",
331 kf.between("2005-1006", "2009-0803").toString());
332 */
Akron40550172015-08-04 03:06:12 +0200333 assertEquals(
334 "AndGroup(pubDate:[20051006 TO 99999999] pubDate:[0 TO 20090803])",
335 kf.between("pubDate", "2005-1006", "2009-0803").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200336
337 /*
338 assertEquals("+pubDate:[20051006 TO 20090803]",
339 kf.between("2005-10-06", "2009-08-03").toString());
340 */
Akron40550172015-08-04 03:06:12 +0200341 assertEquals(
342 "AndGroup(pubDate:[20051006 TO 99999999] pubDate:[0 TO 20090803])",
343 kf.between("pubDate", "2005-10-06", "2009-08-03").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200344
345 /*
346 assertEquals("+pubDate:[0 TO 20059999]", kf.till("2005").toString());
347 */
Akron40550172015-08-04 03:06:12 +0200348 assertEquals("pubDate:[0 TO 20059999]", kf.till("pubDate", "2005")
349 .toString());
Akron60dfa7e2015-08-03 22:15:17 +0200350
351 /*
352 assertEquals("+pubDate:[0 TO 20051099]", kf.till("200510").toString());
353 */
Akron40550172015-08-04 03:06:12 +0200354 assertEquals("pubDate:[0 TO 20051099]", kf.till("pubDate", "200510")
355 .toString());
Akron60dfa7e2015-08-03 22:15:17 +0200356
357 /*
358 assertEquals("+pubDate:[0 TO 20051099]", kf.till("200510").toString());
359 */
Akron40550172015-08-04 03:06:12 +0200360 assertEquals("pubDate:[0 TO 20051099]", kf.till("pubDate", "200510")
361 .toString());
Akron60dfa7e2015-08-03 22:15:17 +0200362
363 /*
364 assertEquals("+pubDate:[0 TO 20051099]", kf.till("2005-10").toString());
365 */
Akron40550172015-08-04 03:06:12 +0200366 assertEquals("pubDate:[0 TO 20051099]", kf.till("pubDate", "2005-10")
367 .toString());
Akron60dfa7e2015-08-03 22:15:17 +0200368
369 /*
370 assertEquals("+pubDate:[0 TO 20051006]", kf.till("2005-1006")
371 .toString());
372 */
373 assertEquals("pubDate:[0 TO 20051006]", kf.till("pubDate", "2005-1006")
374 .toString());
375
376 /*
377 assertEquals("+pubDate:[0 TO 20051006]", kf.till("2005-10-06")
378 .toString());
379 */
Akron40550172015-08-04 03:06:12 +0200380 assertEquals("pubDate:[0 TO 20051006]", kf
381 .till("pubDate", "2005-10-06").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200382
383 /*
384 assertEquals("+pubDate:[20050000 TO 99999999]", kf.since("2005")
385 .toString());
386 */
Akron40550172015-08-04 03:06:12 +0200387 assertEquals("pubDate:[20050000 TO 99999999]",
388 kf.since("pubDate", "2005").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200389
390 /*
391 assertEquals("+pubDate:[20051000 TO 99999999]", kf.since("200510")
392 .toString());
393 */
Akron40550172015-08-04 03:06:12 +0200394 assertEquals("pubDate:[20051000 TO 99999999]",
395 kf.since("pubDate", "200510").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200396
397
398 /*
399 assertEquals("+pubDate:[20051000 TO 99999999]", kf.since("2005-10")
400 .toString());
401 */
Akron40550172015-08-04 03:06:12 +0200402 assertEquals("pubDate:[20051000 TO 99999999]",
403 kf.since("pubDate", "2005-10").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200404
405 /*
406 assertEquals("+pubDate:[20051006 TO 99999999]", kf.since("2005-1006")
407 .toString());
408 */
Akron40550172015-08-04 03:06:12 +0200409 assertEquals("pubDate:[20051006 TO 99999999]",
410 kf.since("pubDate", "2005-1006").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200411
412 /*
413 assertEquals("+pubDate:[20051006 TO 99999999]", kf.since("2005-10-06")
414 .toString());
415 */
Akron40550172015-08-04 03:06:12 +0200416 assertEquals("pubDate:[20051006 TO 99999999]",
417 kf.since("pubDate", "2005-10-06").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200418
419 /*
420 assertEquals("+pubDate:[20050000 TO 20059999]", kf.date("2005")
421 .toString());
422 */
Akron40550172015-08-04 03:06:12 +0200423 assertEquals("pubDate:[20050000 TO 20059999]",
424 kf.date("pubDate", "2005").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200425
426
427 /*
428 assertEquals("+pubDate:[20051000 TO 20051099]", kf.date("200510")
429 .toString());
430 */
Akron40550172015-08-04 03:06:12 +0200431 assertEquals("pubDate:[20051000 TO 20051099]",
432 kf.date("pubDate", "200510").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200433
434 /*
435 assertEquals("+pubDate:[20051000 TO 20051099]", kf.date("2005-10")
436 .toString());
437 */
Akron40550172015-08-04 03:06:12 +0200438 assertEquals("pubDate:[20051000 TO 20051099]",
439 kf.date("pubDate", "2005-10").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200440
441 /*
442 assertEquals("+pubDate:20051006", kf.date("2005-1006").toString());
443 */
Akron40550172015-08-04 03:06:12 +0200444 assertEquals("pubDate:[20051006 TO 20051006]",
445 kf.date("pubDate", "2005-1006").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200446
447 /*
448 assertEquals("+pubDate:20051006", kf.date("2005-10-06").toString());
449 */
Akron40550172015-08-04 03:06:12 +0200450 assertEquals("pubDate:[20051006 TO 20051006]",
451 kf.date("pubDate", "2005-10-06").toString());
Akron60dfa7e2015-08-03 22:15:17 +0200452 };
453
454
455 @Test
456 public void LegacyRangeFailure () throws IOException {
457 CollectionBuilder kf = new CollectionBuilder();
458 /*
459 assertEquals("", kf.between("aaaa-bb-cc", "aaaabbcc").toString());
460 assertEquals("", kf.till("aaaa-bb-cc").toString());
461 assertEquals("", kf.since("aaaa-bb-cc").toString());
462 assertEquals("", kf.date("aaaa-bb-cc").toString());
463 */
464 assertNull(kf.between("pubDate", "aaaa-bb-cc", "aaaabbcc"));
465 assertNull(kf.till("pubDate", "aaaa-bb-cc"));
466 assertNull(kf.since("pubDate", "aaaa-bb-cc"));
467 assertNull(kf.date("pubDate", "aaaa-bb-cc"));
468 };
469};