blob: df48e5272c6819ed4840fcce94d6bec9cb9547d8 [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 =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000020 "{@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}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000026 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000027 "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000028 "]}";
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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000031 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000032
33
34 query="(scheint) and ((Sonne) or (Mond))";
35 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000036 "{@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}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000043 "]}" +
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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000047 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000048
49 }
50
51 @Test
52 public void testOrQuery() throws CQLParseException, IOException, QueryException{
53 query = "(Sonne) or (Mond)";
54 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000055 "{@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}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000058 "]}";
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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000062 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000063
64 query="(\"Sonne scheint\") or (Mond)";
65 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000066 "{@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}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000070 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000071 "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000072 "]}";
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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000076 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000077
78 query="(\"Sonne scheint\") or (\"Mond scheint\")";
79 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000080 "{@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}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000084 "]}," +
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000085 "{@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}}" +
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000088 "]}" +
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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000092 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +000093 }
94
95 @Test
96 public void testTermQuery() throws CQLParseException, IOException, QueryException{
97 query = "Sonne";
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +000098 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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000101 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000102 }
103
104 @Test
105 public void testPhraseQuery() throws CQLParseException, IOException, QueryException{
106 query="\"der Mann\"";
107 String jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000108 "{@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 "]}";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000112
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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000115 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000116
117
118 query="der Mann schläft";
119 jsonLd =
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000120 "{@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 "]}";
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000125
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();
Eliza Margaretha8d74c4a2014-04-02 08:43:56 +0000128 assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", ""));
Eliza Margaretha39db1ab2014-04-01 10:39:54 +0000129 }
130}