Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit a96d748

Browse files
author
hideki
committed
Added unit test for query with AND and LIKE operators
1 parent 4eafb8f commit a96d748

File tree

1 file changed

+78
-0
lines changed
  • android/CouchbaseLite/src/androidTest/java/com/couchbase/lite

1 file changed

+78
-0
lines changed

android/CouchbaseLite/src/androidTest/java/com/couchbase/lite/QueryTest.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2655,4 +2655,82 @@ public void changed(QueryChange change) {
26552655
q.removeChangeListener(token);
26562656
}
26572657
}
2658+
2659+
// https://github.com/couchbase/couchbase-lite-android/issues/1689
2660+
@Test
2661+
public void testQueryAndNLikeOperators() throws Exception {
2662+
2663+
MutableDocument mDoc1 = new MutableDocument("doc1");
2664+
mDoc1.setString("name", "food");
2665+
mDoc1.setString("description", "bar");
2666+
save(mDoc1);
2667+
2668+
MutableDocument mDoc2 = new MutableDocument("doc2");
2669+
mDoc2.setString("name", "foo");
2670+
mDoc2.setString("description", "unknown");
2671+
save(mDoc2);
2672+
2673+
MutableDocument mDoc3 = new MutableDocument("doc3");
2674+
mDoc3.setString("name", "water");
2675+
mDoc3.setString("description", "drink");
2676+
save(mDoc3);
2677+
2678+
MutableDocument mDoc4 = new MutableDocument("doc4");
2679+
mDoc4.setString("name", "chocolate");
2680+
mDoc4.setString("description", "bar");
2681+
save(mDoc4);
2682+
2683+
// LIKE operator only
2684+
Query q = QueryBuilder.select(SelectResult.expression(Meta.id))
2685+
.from(DataSource.database(db))
2686+
.where(Expression.property("name").like(Expression.string("%foo%")))
2687+
.orderBy(Ordering.expression(Meta.id));
2688+
ResultSet rs = q.execute();
2689+
int numRows = verifyQuery(q, new QueryResult() {
2690+
@Override
2691+
public void check(int n, Result result) throws Exception {
2692+
assertEquals(1, result.count());
2693+
if(n == 1)
2694+
assertEquals("doc1", result.getString(0));
2695+
else
2696+
assertEquals("doc2", result.getString(0));
2697+
2698+
}
2699+
}, true);
2700+
assertEquals(2, numRows);
2701+
2702+
// EQUAL operator only
2703+
q = QueryBuilder.select(SelectResult.expression(Meta.id))
2704+
.from(DataSource.database(db))
2705+
.where(Expression.property("description").equalTo(Expression.string("bar")))
2706+
.orderBy(Ordering.expression(Meta.id));
2707+
rs = q.execute();
2708+
numRows = verifyQuery(q, new QueryResult() {
2709+
@Override
2710+
public void check(int n, Result result) throws Exception {
2711+
assertEquals(1, result.count());
2712+
if(n == 1)
2713+
assertEquals("doc1", result.getString(0));
2714+
else
2715+
assertEquals("doc4", result.getString(0));
2716+
2717+
}
2718+
}, true);
2719+
assertEquals(2, numRows);
2720+
2721+
// AND and LIKE operators
2722+
q = QueryBuilder.select(SelectResult.expression(Meta.id))
2723+
.from(DataSource.database(db))
2724+
.where(Expression.property("name").like(Expression.string("%foo%")).and(Expression.property("description").equalTo(Expression.string("bar"))))
2725+
.orderBy(Ordering.expression(Meta.id));
2726+
rs = q.execute();
2727+
numRows = verifyQuery(q, new QueryResult() {
2728+
@Override
2729+
public void check(int n, Result result) throws Exception {
2730+
assertEquals(1, result.count());
2731+
assertEquals("doc1", result.getString(0));
2732+
}
2733+
}, true);
2734+
assertEquals(1, numRows);
2735+
}
26582736
}

0 commit comments

Comments
 (0)