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