blob: 1f7a88b8fe6e73519d14a6a33d6c0a0fac77b246 [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 Margaretha46793572014-04-02 15:11:25 +000016
17 @Test
18 public void testExceptions() throws CQLParseException, IOException {
19 query = "(Kuh) prox (Germ) ";
20 try {
21 CQLTree cqlTree = new CQLTree(query, version);
22 } catch (QueryException e) {
23 assertEquals(48,e.getErrorCode());
24 }
25
26 query = "(Kuh) or/rel.combine=sum (Germ) ";
27 try {
28 CQLTree cqlTree = new CQLTree(query, version);
29 }catch (QueryException e) {
30 assertEquals(20,e.getErrorCode());
31 }
32
33 query = "dc.title any Germ ";
34 try {
35 CQLTree cqlTree = new CQLTree(query, version);
36 } catch (QueryException e) {
37 assertEquals(16,e.getErrorCode());
38 }
39
40 query = "cql.serverChoice any Germ ";
41 try {
42 CQLTree cqlTree = new CQLTree(query, version);
43 } catch (QueryException e) {
44 assertEquals(19,e.getErrorCode());
45 }
46
47 query = "";
48 try {
49 CQLTree cqlTree = new CQLTree(query, version);
50 } catch (QueryException e) {
51 assertEquals(27,e.getErrorCode());
52 }
53 }
54
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000055 @Test
56 public void testBooleanQuery() throws CQLParseException, IOException, QueryException{
57 query="((Sonne) or (Mond)) and (scheint)";
58 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000059 "{@type:korap:group, operation:operation:sequence, distances:[" +
60 "{@type:korap:distance, key:t, min:0, max:0}" +
61 "], operands:[" +
62 "{@type:korap:group, operation:operation:or, operands:[" +
63 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
64 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000065 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000066 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000067 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +000068 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000069 String serializedQuery = cqlTree.getRequestMap().get("query").toString();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000070 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000071
72
73 query="(scheint) and ((Sonne) or (Mond))";
74 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000075 "{@type:korap:group, operation:operation:sequence, distances:[" +
76 "{@type:korap:distance, key:t, min:0, max:0}" +
77 "], operands:[" +
78 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}," +
79 "{@type:korap:group, operation:operation:or, operands:[" +
80 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
81 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000082 "]}" +
83 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +000084 cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000085 serializedQuery = cqlTree.getRequestMap().get("query").toString();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000086 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000087
88 }
89
90 @Test
91 public void testOrQuery() throws CQLParseException, IOException, QueryException{
92 query = "(Sonne) or (Mond)";
93 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000094 "{@type:korap:group, operation:operation:or, operands:[" +
95 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
96 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000097 "]}";
98
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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000101 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000102
103 query="(\"Sonne scheint\") or (Mond)";
104 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000105 "{@type:korap:group, operation:operation:or, operands:[" +
106 "{@type:korap:group, operation:operation:sequence, operands:[" +
107 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
108 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000109 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000110 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000111 "]}";
112
Eliza Margaretha724be112014-04-01 11:13:40 +0000113 cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000114 serializedQuery = cqlTree.getRequestMap().get("query").toString();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000115 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000116
117 query="(\"Sonne scheint\") or (\"Mond scheint\")";
118 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000119 "{@type:korap:group, operation:operation:or, operands:[" +
120 "{@type:korap:group, operation:operation:sequence, operands:[" +
121 "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}," +
122 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000123 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000124 "{@type:korap:group, operation:operation:sequence, operands:[" +
125 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}," +
126 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000127 "]}" +
128 "]}";
Eliza Margaretha724be112014-04-01 11:13:40 +0000129 cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000130 serializedQuery = cqlTree.getRequestMap().get("query").toString();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000131 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000132 }
133
134 @Test
135 public void testTermQuery() throws CQLParseException, IOException, QueryException{
136 query = "Sonne";
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000137 String jsonLd = "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}";
Eliza Margaretha724be112014-04-01 11:13:40 +0000138 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000139 String serializedQuery = cqlTree.getRequestMap().get("query").toString();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000140 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000141 }
142
143 @Test
144 public void testPhraseQuery() throws CQLParseException, IOException, QueryException{
145 query="\"der Mann\"";
146 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000147 "{@type:korap:group, operation:operation:sequence, operands:[" +
148 "{@type:korap:token, wrap:{@type:korap:term, key:der, layer:orth, match:match:eq}}," +
149 "{@type:korap:token, wrap:{@type:korap:term, key:Mann, layer:orth, match:match:eq}}" +
150 "]}";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000151
Eliza Margaretha724be112014-04-01 11:13:40 +0000152 CQLTree cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000153 String serializedQuery = cqlTree.getRequestMap().get("query").toString();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000154 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000155
156
157 query="der Mann schläft";
158 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000159 "{@type:korap:group, operation:operation:sequence, operands:[" +
160 "{@type:korap:token, wrap:{@type:korap:term, key:der, layer:orth, match:match:eq}}," +
161 "{@type:korap:token, wrap:{@type:korap:term, key:Mann, layer:orth, match:match:eq}}," +
162 "{@type:korap:token, wrap:{@type:korap:term, key:schläft, layer:orth, match:match:eq}}" +
163 "]}";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000164
Eliza Margaretha724be112014-04-01 11:13:40 +0000165 cqlTree = new CQLTree(query, version);
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000166 serializedQuery = cqlTree.getRequestMap().get("query").toString();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000167 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000168 }
169}