Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame^] | 1 | import static org.junit.Assert.*; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import de.ids_mannheim.korap.query.serialize.AqlTree; |
| 6 | import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree; |
| 7 | import de.ids_mannheim.korap.util.QueryException; |
| 8 | |
| 9 | public class AqlTreeTest { |
| 10 | |
| 11 | AqlTree aqlt; |
| 12 | String map; |
| 13 | private String query; |
| 14 | |
| 15 | private boolean equalsQueryContent(String res, String query) throws QueryException { |
| 16 | res = res.replaceAll(" ", ""); |
| 17 | aqlt = new AqlTree(query); |
| 18 | String queryMap = aqlt.getRequestMap().get("query").toString().replaceAll(" ", ""); |
| 19 | return res.equals(queryMap); |
| 20 | } |
| 21 | |
| 22 | @Test |
| 23 | public void testContext() throws QueryException { |
| 24 | String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld"; |
| 25 | aqlt = new AqlTree("Test"); |
| 26 | assertEquals(contextString.replaceAll(" ", ""), aqlt.getRequestMap().get("@context").toString().replaceAll(" ", "")); |
| 27 | } |
| 28 | |
| 29 | @Test |
| 30 | public void testSingleTokens() throws QueryException { |
| 31 | // "Mann" |
| 32 | query = "\"Mann\""; |
| 33 | String token1 = "{@type=korap:token, wrap={@type=korap:term, match=match:eq, key=Mann}}"; |
| 34 | assertTrue(equalsQueryContent(token1, query)); |
| 35 | |
| 36 | // [orth!=Frau] |
| 37 | query = "tok!=\"Frau\""; |
| 38 | String token2 = "{@type=korap:token, wrap={@type=korap:term, match=match:ne, key=Frau}}"; |
| 39 | assertTrue(equalsQueryContent(token2, query)); |
| 40 | |
| 41 | // Mann |
| 42 | query = "Mann"; |
| 43 | String token4 = "{@type=korap:span, layer=Mann}"; |
| 44 | assertTrue(equalsQueryContent(token4, query)); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testSpans() throws QueryException { |
| 49 | query = "node"; |
| 50 | String span1 = |
| 51 | "{@type=korap:span}"; |
| 52 | aqlt = new AqlTree(query); |
| 53 | map = aqlt.getRequestMap().get("query").toString(); |
| 54 | assertEquals(span1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testRegex() throws QueryException { |
| 59 | query = "/Mann/"; |
| 60 | String regex1 = "{@type=korap:token, wrap={@type=korap:term, match=match:eq, type=type:regex, key=Mann}}"; |
| 61 | aqlt = new AqlTree(query); |
| 62 | map = aqlt.getRequestMap().get("query").toString(); |
| 63 | assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 64 | } |
| 65 | |
| 66 | @Test |
| 67 | public void testLayers() throws QueryException { |
| 68 | query = "cnx/cat=\"NP\""; |
| 69 | String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, match=match:eq, key=NP}"; |
| 70 | aqlt = new AqlTree(query); |
| 71 | map = aqlt.getRequestMap().get("query").toString(); |
| 72 | assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 73 | |
| 74 | query = "treetagger/pos=\"NN\""; |
| 75 | String layers2 = "{@type=korap:token, wrap={@type=korap:term, foundry=treetagger, layer=pos, key=NN, match=match:eq}}"; |
| 76 | aqlt = new AqlTree(query); |
| 77 | map = aqlt.getRequestMap().get("query").toString(); |
| 78 | assertEquals(layers2.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void testSimpleDominance() throws QueryException { |
| 83 | query = "node & node & #2 > #1"; |
| 84 | String dom1 = |
| 85 | "{@type=korap:group, operation=operation:treeRelation, operands=[" + |
| 86 | "{@type=korap:span}," + |
| 87 | "{@type=korap:span}" + |
| 88 | "], treeRelation=dominance}"; |
| 89 | aqlt = new AqlTree(query); |
| 90 | map = aqlt.getRequestMap().get("query").toString(); |
| 91 | assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 92 | } |
| 93 | |
| 94 | } |
| 95 | |