Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 1 | import com.fasterxml.jackson.databind.JsonNode; |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 2 | import de.ids_mannheim.korap.query.serialize.QuerySerializer; |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.utils.CollectionQueryBuilder3; |
| 4 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 5 | import org.junit.Test; |
| 6 | |
| 7 | /** |
| 8 | * @author hanl |
| 9 | * @date 12/08/2015 |
| 10 | */ |
| 11 | public class CollectionQueryBuilderTest { |
| 12 | |
| 13 | @Test |
| 14 | public void testsimpleAdd() { |
| 15 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 16 | b.addQuery("corpusID=WPD"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 17 | |
| 18 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 19 | |
| 20 | assert node != null; |
| 21 | assert node.at("/collection/@type").asText().equals("koral:doc"); |
| 22 | assert node.at("/collection/key").asText().equals("corpusID"); |
| 23 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 27 | public void testSimpleConjunction() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 28 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 29 | b.addQuery("corpusID=WPD & textClass=freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 30 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 31 | |
| 32 | assert node != null; |
| 33 | assert node.at("/collection/@type").asText().equals("koral:docGroup"); |
| 34 | assert node.at("/collection/operation").asText() |
| 35 | .equals("operation:and"); |
| 36 | assert node.at("/collection/operands/0/key").asText() |
| 37 | .equals("corpusID"); |
| 38 | assert node.at("/collection/operands/1/key").asText() |
| 39 | .equals("textClass"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 43 | public void testSimpleDisjunction() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 44 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 45 | b.addQuery("corpusID=WPD | textClass=freizeit"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 46 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
| 47 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 48 | System.out.println("_____________________________________________"); |
| 49 | System.out.println(node); |
| 50 | |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 51 | assert node != null; |
| 52 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 53 | assert node.at("/collection/operands/0/key").asText() |
| 54 | .equals("corpusID"); |
| 55 | assert node.at("/collection/operands/1/key").asText() |
| 56 | .equals("textClass"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Test |
| 60 | public void testComplexSubQuery() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 61 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 62 | b.addQuery( |
| 63 | "(corpusID=WPD) | (textClass=freizeit & corpusID=WPD)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 64 | JsonNode node = JsonUtils.readTree(b.toJSON()); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 65 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 66 | System.out.println( |
| 67 | "_____________________________________________ COMPLEX"); |
| 68 | System.out.println(node); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 69 | assert node != null; |
| 70 | assert node.at("/collection/operation").asText().equals("operation:or"); |
| 71 | assert node.at("/collection/operands/0/key").asText() |
| 72 | .equals("corpusID"); |
| 73 | assert node.at("/collection/operands/1/@type").asText() |
| 74 | .equals("koral:docGroup"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 75 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Test |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 79 | public void testAddResourceQueryAfter() { |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 80 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 81 | b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 82 | |
| 83 | CollectionQueryBuilder3 c = new CollectionQueryBuilder3(); |
| 84 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 85 | c.addQuery("textClass=wissenschaft"); |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 86 | |
| 87 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
| 88 | |
| 89 | assert node != null; |
| 90 | assert node.at("/collection/operands/2/@type").asText() |
| 91 | .equals("koral:doc"); |
| 92 | assert node.at("/collection/operands/2/value").asText() |
| 93 | .equals("wissenschaft"); |
| 94 | } |
| 95 | |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 96 | @Test |
| 97 | public void testAddComplexResourceQueryAfter() { |
| 98 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 99 | b.addQuery("(corpusID=ADF) | (textClass=freizeit & corpusID=WPD)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 100 | |
| 101 | CollectionQueryBuilder3 c = new CollectionQueryBuilder3(); |
| 102 | c.setBaseQuery(b.toJSON()); |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 103 | c.addQuery("(textClass=politik & corpusID=AZPS)"); |
Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 104 | |
| 105 | JsonNode node = JsonUtils.readTree(c.toJSON()); |
| 106 | |
| 107 | assert node != null; |
| 108 | assert node.at("/collection/operands/2/@type").asText() |
| 109 | .equals("koral:docGroup"); |
| 110 | assert node.at("/collection/operands/2/operands/0/value").asText() |
| 111 | .equals("politik"); |
| 112 | assert node.at("/collection/operands/2/operands/1/value").asText() |
| 113 | .equals("AZPS"); |
| 114 | |
| 115 | } |
| 116 | |
Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 117 | @Test |
| 118 | public void buildQuery() { |
| 119 | String query = "[base=Haus]"; |
| 120 | QuerySerializer s = new QuerySerializer(); |
| 121 | s.setQuery(query, "poliqarp"); |
| 122 | CollectionQueryBuilder3 b = new CollectionQueryBuilder3(); |
| 123 | b.addQuery("corpusID=WPD"); |
| 124 | s.setCollection("corpusID=WPD"); |
| 125 | |
| 126 | System.out.println("QUERY " + s.toJSON()); |
| 127 | } |
| 128 | |
| 129 | @Test |
| 130 | public void testBaseQueryBuild() { |
| 131 | |
| 132 | } |
| 133 | |
Michael Hanl | fb839b9 | 2015-09-19 21:32:34 +0200 | [diff] [blame] | 134 | } |