blob: ef778b3f666a620caa61340c3152e948df3c2315 [file] [log] [blame]
Eliza Margaretha39db1ab2014-04-01 10:39:54 +00001import static org.junit.Assert.assertEquals;
2
3import java.io.IOException;
4
5import org.junit.Test;
6import org.z3950.zing.cql.CQLParseException;
7
8import de.ids_mannheim.korap.query.serialize.CQLTree;
9import de.ids_mannheim.korap.util.QueryException;
10
11
12public class CQLTest {
13
14 String query;
15
16 @Test
17 public void testBooleanQuery() throws CQLParseException, IOException, QueryException{
18 query="((Sonne) or (Mond)) and (scheint)";
19 String jsonLd =
20 "{@type=korap:group, operation=operation:sequence, distances=[" +
21 "{@type=korap:distance, key=t, min=0, max=0}" +
22 "], operands=[" +
23 "{@type=korap:group, operation=operation:or, operands=[" +
24 "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," +
25 "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
26 "]}," +
27 "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}" +
28 "]}";
29 CQLTree cqlTree = new CQLTree(query);
30 String serializedQuery = cqlTree.getRequestMap().get("query").toString();
31 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
32
33
34 query="(scheint) and ((Sonne) or (Mond))";
35 jsonLd =
36 "{@type=korap:group, operation=operation:sequence, distances=[" +
37 "{@type=korap:distance, key=t, min=0, max=0}" +
38 "], operands=[" +
39 "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}," +
40 "{@type=korap:group, operation=operation:or, operands=[" +
41 "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," +
42 "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
43 "]}" +
44 "]}";
45 cqlTree = new CQLTree(query);
46 serializedQuery = cqlTree.getRequestMap().get("query").toString();
47 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
48
49 }
50
51 @Test
52 public void testOrQuery() throws CQLParseException, IOException, QueryException{
53 query = "(Sonne) or (Mond)";
54 String jsonLd =
55 "{@type=korap:group, operation=operation:or, operands=[" +
56 "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," +
57 "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
58 "]}";
59
60 CQLTree cqlTree = new CQLTree(query);
61 String serializedQuery = cqlTree.getRequestMap().get("query").toString();
62 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
63
64 query="(\"Sonne scheint\") or (Mond)";
65 jsonLd =
66 "{@type=korap:group, operation=operation:or, operands=[" +
67 "{@type=korap:group, operation=operation:sequence, operands=[" +
68 "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," +
69 "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}" +
70 "]}," +
71 "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}" +
72 "]}";
73
74 cqlTree = new CQLTree(query);
75 serializedQuery = cqlTree.getRequestMap().get("query").toString();
76 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
77
78 query="(\"Sonne scheint\") or (\"Mond scheint\")";
79 jsonLd =
80 "{@type=korap:group, operation=operation:or, operands=[" +
81 "{@type=korap:group, operation=operation:sequence, operands=[" +
82 "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}," +
83 "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}" +
84 "]}," +
85 "{@type=korap:group, operation=operation:sequence, operands=[" +
86 "{@type=korap:token, wrap={@type=korap:term, key=Mond, layer=orth, match=match:eq}}," +
87 "{@type=korap:token, wrap={@type=korap:term, key=scheint, layer=orth, match=match:eq}}" +
88 "]}" +
89 "]}";
90 cqlTree = new CQLTree(query);
91 serializedQuery = cqlTree.getRequestMap().get("query").toString();
92 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
93 }
94
95 @Test
96 public void testTermQuery() throws CQLParseException, IOException, QueryException{
97 query = "Sonne";
98 String jsonLd = "{@type=korap:token, wrap={@type=korap:term, key=Sonne, layer=orth, match=match:eq}}";
99 CQLTree cqlTree = new CQLTree(query);
100 String serializedQuery = cqlTree.getRequestMap().get("query").toString();
101 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
102 }
103
104 @Test
105 public void testPhraseQuery() throws CQLParseException, IOException, QueryException{
106 query="\"der Mann\"";
107 String jsonLd =
108 "{@type=korap:group, operation=operation:sequence, operands=[" +
109 "{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}," +
110 "{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}" +
111 "]}";
112
113 CQLTree cqlTree = new CQLTree(query);
114 String serializedQuery = cqlTree.getRequestMap().get("query").toString();
115 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
116
117
118 query="der Mann schläft";
119 jsonLd =
120 "{@type=korap:group, operation=operation:sequence, operands=[" +
121 "{@type=korap:token, wrap={@type=korap:term, key=der, layer=orth, match=match:eq}}," +
122 "{@type=korap:token, wrap={@type=korap:term, key=Mann, layer=orth, match=match:eq}}," +
123 "{@type=korap:token, wrap={@type=korap:term, key=schläft, layer=orth, match=match:eq}}" +
124 "]}";
125
126 cqlTree = new CQLTree(query);
127 serializedQuery = cqlTree.getRequestMap().get("query").toString();
128 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
129 }
130}