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 | 4679357 | 2014-04-02 15:11:25 +0000 | [diff] [blame^] | 16 | |
| 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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 55 | @Test |
| 56 | public void testBooleanQuery() throws CQLParseException, IOException, QueryException{ |
| 57 | query="((Sonne) or (Mond)) and (scheint)"; |
| 58 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 59 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 65 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 66 | "{@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] | 67 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 68 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 69 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 70 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 71 | |
| 72 | |
| 73 | query="(scheint) and ((Sonne) or (Mond))"; |
| 74 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 75 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 82 | "]}" + |
| 83 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 84 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 85 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 86 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 87 | |
| 88 | } |
| 89 | |
| 90 | @Test |
| 91 | public void testOrQuery() throws CQLParseException, IOException, QueryException{ |
| 92 | query = "(Sonne) or (Mond)"; |
| 93 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 94 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 97 | "]}"; |
| 98 | |
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 | query="(\"Sonne scheint\") or (Mond)"; |
| 104 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 105 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 109 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 110 | "{@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] | 111 | "]}"; |
| 112 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 113 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 114 | 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 | query="(\"Sonne scheint\") or (\"Mond scheint\")"; |
| 118 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 119 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 123 | "]}," + |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 124 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 127 | "]}" + |
| 128 | "]}"; |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 129 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 130 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 131 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testTermQuery() throws CQLParseException, IOException, QueryException{ |
| 136 | query = "Sonne"; |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 137 | 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] | 138 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 139 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 140 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | @Test |
| 144 | public void testPhraseQuery() throws CQLParseException, IOException, QueryException{ |
| 145 | query="\"der Mann\""; |
| 146 | String jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 147 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 151 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 152 | CQLTree cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 153 | String serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 154 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 155 | |
| 156 | |
| 157 | query="der Mann schläft"; |
| 158 | jsonLd = |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 159 | "{@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 Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 164 | |
Eliza Margaretha | 724be11 | 2014-04-01 11:13:40 +0000 | [diff] [blame] | 165 | cqlTree = new CQLTree(query, version); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 166 | serializedQuery = cqlTree.getRequestMap().get("query").toString(); |
Eliza Margaretha | 8d74c4a | 2014-04-02 08:43:56 +0000 | [diff] [blame] | 167 | assertEquals(jsonLd.replace(" ", ""), serializedQuery.replace("\"", "")); |
Eliza Margaretha | 39db1ab | 2014-04-01 10:39:54 +0000 | [diff] [blame] | 168 | } |
| 169 | } |