Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 1 | import de.ids_mannheim.korap.query.serialize.CollectionQueryTree; |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 2 | import de.ids_mannheim.korap.util.QueryException; |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 3 | import org.junit.Test; |
| 4 | |
Michael Hanl | 85d4b5d | 2014-07-25 12:07:20 +0000 | [diff] [blame^] | 5 | import static org.junit.Assert.assertEquals; |
| 6 | |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 7 | public class CollectionQueryTreeTest { |
| 8 | |
Joachim Bingel | a3f51f7 | 2014-07-22 14:45:31 +0000 | [diff] [blame] | 9 | CollectionQueryTree cqt; |
| 10 | String map; |
| 11 | private String query; |
| 12 | private String expected; |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 13 | |
Joachim Bingel | a3f51f7 | 2014-07-22 14:45:31 +0000 | [diff] [blame] | 14 | private boolean equalsQueryContent(String res, String query) throws QueryException { |
| 15 | res = res.replaceAll(" ", ""); |
| 16 | cqt = new CollectionQueryTree(); |
| 17 | cqt.process(query); |
| 18 | String queryMap = cqt.getRequestMap().get("query").toString().replaceAll(" ", ""); |
| 19 | return res.equals(queryMap); |
| 20 | } |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 21 | |
Joachim Bingel | a3f51f7 | 2014-07-22 14:45:31 +0000 | [diff] [blame] | 22 | @Test |
| 23 | public void testSimple() throws QueryException { |
| 24 | query = "textClass=Sport"; |
| 25 | // String regex1 = "{@type=korap:filter, filter={@type=korap:doc, attribute=textClass, key=Sport, match=match:eq}}"; |
| 26 | expected = "{@type=korap:filter, filter={@type=korap:doc, key=textClass, value=Sport, match=match:eq}}"; |
| 27 | cqt = new CollectionQueryTree(); |
| 28 | cqt.process(query); |
| 29 | map = cqt.getRequestMap().toString(); |
| 30 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 31 | |
| 32 | query = "textClass!=Sport"; |
| 33 | // String regex1 = "{@type=korap:filter, filter={@type=korap:doc, attribute=textClass, key=Sport, match=match:eq}}"; |
| 34 | expected = "{@type=korap:filter, filter={@type=korap:doc, key=textClass, value=Sport, match=match:ne}}"; |
| 35 | cqt = new CollectionQueryTree(); |
| 36 | cqt.process(query); |
| 37 | map = cqt.getRequestMap().toString(); |
| 38 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 39 | } |
| 40 | |
| 41 | @Test |
| 42 | public void testTwoConjuncts() throws QueryException { |
| 43 | query = "textClass=Sport & year=2014"; |
| 44 | expected = |
| 45 | "{@type=korap:filter, filter=" + |
| 46 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 47 | "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," + |
| 48 | "{@type=korap:doc, key=year, value=2014, match=match:eq}" + |
| 49 | "]}" + |
| 50 | "}"; |
| 51 | cqt = new CollectionQueryTree(); |
| 52 | cqt.process(query); |
| 53 | map = cqt.getRequestMap().toString(); |
| 54 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testThreeConjuncts() throws QueryException { |
| 59 | query = "textClass=Sport & year=2014 & corpusID=WPD"; |
| 60 | expected = |
| 61 | "{@type=korap:filter, filter=" + |
| 62 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 63 | "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," + |
| 64 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 65 | "{@type=korap:doc, key=year, value=2014, match=match:eq}," + |
| 66 | "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" + |
| 67 | "]}" + |
| 68 | "]}" + |
| 69 | "}"; |
| 70 | cqt = new CollectionQueryTree(); |
| 71 | cqt.process(query); |
| 72 | map = cqt.getRequestMap().toString(); |
| 73 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | @Test |
| 78 | public void testTwoDisjuncts() throws QueryException { |
| 79 | query = "textClass=Sport | year=2014"; |
| 80 | expected = |
| 81 | "{@type=korap:filter, filter=" + |
| 82 | "{@type=korap:docGroup, relation=relation:or, operands=[" + |
| 83 | "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," + |
| 84 | "{@type=korap:doc, key=year, value=2014, match=match:eq}" + |
| 85 | "]}" + |
| 86 | "}"; |
| 87 | cqt = new CollectionQueryTree(); |
| 88 | cqt.process(query); |
| 89 | map = cqt.getRequestMap().toString(); |
| 90 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testThreeDisjuncts() throws QueryException { |
| 95 | query = "textClass=Sport | year=2014 | corpusID=WPD"; |
| 96 | expected = |
| 97 | "{@type=korap:filter, filter=" + |
| 98 | "{@type=korap:docGroup, relation=relation:or, operands=[" + |
| 99 | "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," + |
| 100 | "{@type=korap:docGroup, relation=relation:or, operands=[" + |
| 101 | "{@type=korap:doc, key=year, value=2014, match=match:eq}," + |
| 102 | "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" + |
| 103 | "]}" + |
| 104 | "]}" + |
| 105 | "}"; |
| 106 | cqt = new CollectionQueryTree(); |
| 107 | cqt.process(query); |
| 108 | map = cqt.getRequestMap().toString(); |
| 109 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 110 | } |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 111 | |
| 112 | |
Joachim Bingel | a3f51f7 | 2014-07-22 14:45:31 +0000 | [diff] [blame] | 113 | @Test |
| 114 | public void testMixed() throws QueryException { |
| 115 | query = "(textClass=Sport | textClass=ausland) & corpusID=WPD"; |
| 116 | expected = |
| 117 | "{@type=korap:filter, filter=" + |
| 118 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 119 | "{@type=korap:docGroup, relation=relation:or, operands=[" + |
| 120 | "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," + |
| 121 | "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" + |
| 122 | "]}," + |
| 123 | "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" + |
| 124 | "]}" + |
| 125 | "}"; |
| 126 | cqt = new CollectionQueryTree(); |
| 127 | cqt.process(query); |
| 128 | map = cqt.getRequestMap().toString(); |
| 129 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 130 | |
| 131 | query = "(textClass=Sport & textClass=ausland) & corpusID=WPD"; |
| 132 | expected = |
| 133 | "{@type=korap:filter, filter=" + |
| 134 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 135 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 136 | "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," + |
| 137 | "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" + |
| 138 | "]}," + |
| 139 | "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}" + |
| 140 | "]}" + |
| 141 | "}"; |
| 142 | cqt = new CollectionQueryTree(); |
| 143 | cqt.process(query); |
| 144 | map = cqt.getRequestMap().toString(); |
| 145 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 146 | |
| 147 | query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White)"; |
| 148 | expected = |
| 149 | "{@type=korap:filter, filter=" + |
| 150 | "{@type=korap:docGroup, relation=relation:or, operands=[" + |
| 151 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 152 | "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," + |
| 153 | "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" + |
| 154 | "]}," + |
| 155 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 156 | "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," + |
| 157 | "{@type=korap:doc, key=author, value=White, match=match:eq}" + |
| 158 | "]}" + |
| 159 | "]}" + |
| 160 | "}"; |
| 161 | cqt = new CollectionQueryTree(); |
| 162 | cqt.process(query); |
| 163 | map = cqt.getRequestMap().toString(); |
| 164 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Michael Hanl | 85d4b5d | 2014-07-25 12:07:20 +0000 | [diff] [blame^] | 165 | |
| 166 | query = "(textClass=Sport & textClass=ausland) | (corpusID=WPD & author=White & year=2010)"; |
Joachim Bingel | a3f51f7 | 2014-07-22 14:45:31 +0000 | [diff] [blame] | 167 | expected = |
| 168 | "{@type=korap:filter, filter=" + |
| 169 | "{@type=korap:docGroup, relation=relation:or, operands=[" + |
| 170 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 171 | "{@type=korap:doc, key=textClass, value=Sport, match=match:eq}," + |
| 172 | "{@type=korap:doc, key=textClass, value=ausland, match=match:eq}" + |
| 173 | "]}," + |
| 174 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 175 | "{@type=korap:doc, key=corpusID, value=WPD, match=match:eq}," + |
| 176 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 177 | "{@type=korap:doc, key=author, value=White, match=match:eq}," + |
| 178 | "{@type=korap:doc, key=year, value=2010, match=match:eq}" + |
| 179 | "]}" + |
| 180 | "]}" + |
| 181 | "]}" + |
| 182 | "}"; |
| 183 | cqt = new CollectionQueryTree(); |
| 184 | cqt.process(query); |
| 185 | map = cqt.getRequestMap().toString(); |
| 186 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 187 | } |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 188 | |
Joachim Bingel | a3f51f7 | 2014-07-22 14:45:31 +0000 | [diff] [blame] | 189 | @Test |
| 190 | public void testDate() throws QueryException { |
| 191 | // search for pubDate between 1990 and 2010! |
| 192 | query = "1990<pubDate<2010"; |
| 193 | expected = |
| 194 | "{@type=korap:filter, filter=" + |
| 195 | "{@type=korap:docGroup, relation=relation:and, operands=[" + |
| 196 | "{@type=korap:doc, key=pubDate, value=1990, match=match:gt}," + |
| 197 | "{@type=korap:doc, key=pubDate, value=2010, match=match:lt}" + |
| 198 | "]}" + |
| 199 | "}"; |
| 200 | cqt = new CollectionQueryTree(); |
| 201 | cqt.process(query); |
| 202 | map = cqt.getRequestMap().toString(); |
| 203 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 204 | |
| 205 | query = "pubDate>=1990"; |
| 206 | expected = |
| 207 | "{@type=korap:filter, filter=" + |
| 208 | "{@type=korap:doc, key=pubDate, value=1990, match=match:geq}" + |
| 209 | "}"; |
| 210 | cqt = new CollectionQueryTree(); |
| 211 | cqt.process(query); |
| 212 | map = cqt.getRequestMap().toString(); |
| 213 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | 624854b | 2014-07-23 13:53:28 +0000 | [diff] [blame] | 214 | |
| 215 | query = "pubDate>=1990-05"; |
| 216 | expected = |
| 217 | "{@type=korap:filter, filter=" + |
| 218 | "{@type=korap:doc, key=pubDate, value=1990-05, match=match:geq}" + |
| 219 | "}"; |
| 220 | cqt = new CollectionQueryTree(); |
| 221 | cqt.process(query); |
| 222 | map = cqt.getRequestMap().toString(); |
| 223 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 224 | |
| 225 | query = "pubDate>=1990-05-01"; |
| 226 | expected = |
| 227 | "{@type=korap:filter, filter=" + |
| 228 | "{@type=korap:doc, key=pubDate, value=1990-05-01, match=match:geq}" + |
| 229 | "}"; |
| 230 | cqt = new CollectionQueryTree(); |
| 231 | cqt.process(query); |
| 232 | map = cqt.getRequestMap().toString(); |
| 233 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | a3f51f7 | 2014-07-22 14:45:31 +0000 | [diff] [blame] | 234 | } |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 235 | |
Joachim Bingel | a3f51f7 | 2014-07-22 14:45:31 +0000 | [diff] [blame] | 236 | @Test |
| 237 | public void testRegex() throws QueryException { |
| 238 | query = "author=/Go.*he/"; |
| 239 | expected = |
| 240 | "{@type=korap:filter, filter=" + |
| 241 | "{@type=korap:doc, key=author, value=Go.*he, type=type:regex, match=match:eq}" + |
| 242 | "}"; |
| 243 | cqt = new CollectionQueryTree(); |
| 244 | cqt.process(query); |
| 245 | map = cqt.getRequestMap().toString(); |
| 246 | assertEquals(expected.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 247 | } |
Michael Hanl | f1fead4 | 2014-05-14 15:13:33 +0000 | [diff] [blame] | 248 | |
| 249 | } |
| 250 | |