Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 1 | import static org.junit.Assert.assertEquals; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | |
| 5 | import org.junit.Test; |
| 6 | import org.z3950.zing.cql.CQLParseException; |
| 7 | |
| 8 | import de.ids_mannheim.korap.query.serialize.CQLTree; |
| 9 | import de.ids_mannheim.korap.util.QueryException; |
| 10 | |
| 11 | |
| 12 | public class CQLTest { |
| 13 | |
| 14 | String query; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 15 | String version ="1.2"; |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 16 | @Test |
| 17 | public void testBooleanQuery() throws CQLParseException, IOException, QueryException{ |
| 18 | query="((Sonne) or (Mond)) and (scheint)"; |
| 19 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 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}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 26 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 27 | "{@type:korap:token, wrap:{@type:korap:term, key:scheint, layer:orth, match:match:eq}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 28 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 29 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 30 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 31 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 32 | |
| 33 | |
| 34 | query="(scheint) and ((Sonne) or (Mond))"; |
| 35 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 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}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 43 | "]}" + |
| 44 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 45 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 46 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 47 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 48 | |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testOrQuery() throws CQLParseException, IOException, QueryException{ |
| 53 | query = "(Sonne) or (Mond)"; |
| 54 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 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}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 58 | "]}"; |
| 59 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 60 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 61 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 62 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 63 | |
| 64 | query="(\"Sonne scheint\") or (Mond)"; |
| 65 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 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}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 70 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 71 | "{@type:korap:token, wrap:{@type:korap:term, key:Mond, layer:orth, match:match:eq}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 72 | "]}"; |
| 73 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 74 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 75 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 76 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 77 | |
| 78 | query="(\"Sonne scheint\") or (\"Mond scheint\")"; |
| 79 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 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}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 84 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 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}}" + |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 88 | "]}" + |
| 89 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 90 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 91 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 92 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testTermQuery() throws CQLParseException, IOException, QueryException{ |
| 97 | query = "Sonne"; |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 98 | String jsonLd = "{@type:korap:token, wrap:{@type:korap:term, key:Sonne, layer:orth, match:match:eq}}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 99 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 100 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 101 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void testPhraseQuery() throws CQLParseException, IOException, QueryException{ |
| 106 | query="\"der Mann\""; |
| 107 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 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 | "]}"; |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 112 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 113 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 114 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 115 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 116 | |
| 117 | |
| 118 | query="der Mann schläft"; |
| 119 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 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 | "]}"; |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 125 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 126 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 127 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame^] | 128 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 129 | } |
| 130 | } |