blob: d9dba018390268fb353499708ef0c41c9a1b8611 [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;
Eliza Margaretha724be112014-04-01 11:13:40 +000015 String version ="1.2";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000016 @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 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +000029 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000030 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 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +000045 cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000046 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
Eliza Margaretha724be112014-04-01 11:13:40 +000060 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000061 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
Eliza Margaretha724be112014-04-01 11:13:40 +000074 cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000075 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 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +000090 cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000091 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}}";
Eliza Margaretha724be112014-04-01 11:13:40 +000099 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000100 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
Eliza Margaretha724be112014-04-01 11:13:40 +0000113 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000114 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
Eliza Margaretha724be112014-04-01 11:13:40 +0000126 cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000127 serializedQuery = cqlTree.getRequestMap().get("query").toString();
128 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace(" ", ""));
129 }
130}