Skip to content

Commit e62e987

Browse files
Add some tests for the new query API.
1 parent 9a806b8 commit e62e987

File tree

4 files changed

+251
-1
lines changed

4 files changed

+251
-1
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/FunctionalTestSuite.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import io.objectbox.query.QueryFilterComparatorTest;
2323
import io.objectbox.query.QueryObserverTest;
2424
import io.objectbox.query.QueryTest;
25+
import io.objectbox.query.QueryTest2;
26+
import io.objectbox.query.QueryTestK;
2527
import io.objectbox.relation.RelationEagerTest;
2628
import io.objectbox.relation.RelationTest;
2729
import io.objectbox.relation.ToManyStandaloneTest;
@@ -48,6 +50,8 @@
4850
QueryFilterComparatorTest.class,
4951
QueryObserverTest.class,
5052
QueryTest.class,
53+
QueryTest2.class,
54+
QueryTestK.class,
5155
RelationTest.class,
5256
RelationEagerTest.class,
5357
ToManyStandaloneTest.class,

tests/objectbox-java-test/src/test/java/io/objectbox/query/QueryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ public void testOr_bad2() {
507507
@Test
508508
public void testAnd() {
509509
putTestEntitiesScalars();
510-
// OR precedence (wrong): {}, AND precedence (expected): 2008
510+
// Result if OR precedence (wrong): {}, AND precedence (expected): {2008}
511511
Query<TestEntity> query = box.query().equal(simpleInt, 2006).and().equal(simpleInt, 2007).or().equal(simpleInt, 2008).build();
512512
List<TestEntity> entities = query.find();
513513
assertEquals(1, entities.size());
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Copyright 2017 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.objectbox.query;
18+
19+
import org.junit.Test;
20+
21+
import java.util.List;
22+
23+
import io.objectbox.TestEntity;
24+
import io.objectbox.TestEntity_;
25+
26+
27+
import static io.objectbox.TestEntity_.simpleInt;
28+
import static io.objectbox.TestEntity_.simpleLong;
29+
import static org.junit.Assert.assertEquals;
30+
31+
/**
32+
* Tests {@link Query} using the new query builder API with nesting support.
33+
*/
34+
public class QueryTest2 extends AbstractQueryTest {
35+
36+
@Test
37+
public void newQueryApi() {
38+
putTestEntity("Fry", 14);
39+
putTestEntity("Fry", 12);
40+
putTestEntity("Fry", 10);
41+
42+
// current query API
43+
Query<TestEntity> query = box.query()
44+
.equal(TestEntity_.simpleString, "Fry")
45+
.less(TestEntity_.simpleInt, 12)
46+
.or()
47+
.in(TestEntity_.simpleLong, new long[]{1012})
48+
.order(TestEntity_.simpleInt)
49+
.build();
50+
51+
List<TestEntity> results = query.find();
52+
assertEquals(2, results.size());
53+
assertEquals(10, results.get(0).getSimpleInt());
54+
assertEquals(12, results.get(1).getSimpleInt());
55+
56+
// suggested query API
57+
Query<TestEntity> newQuery = box.query(
58+
TestEntity_.simpleString.equal("Fry")
59+
.and(TestEntity_.simpleInt.less(12)
60+
.or(TestEntity_.simpleLong.oneOf(new long[]{1012}))))
61+
.order(TestEntity_.simpleInt)
62+
.build();
63+
64+
List<TestEntity> newResults = newQuery.find();
65+
assertEquals(2, newResults.size());
66+
assertEquals(10, newResults.get(0).getSimpleInt());
67+
assertEquals(12, newResults.get(1).getSimpleInt());
68+
}
69+
70+
@Test
71+
public void parenthesesMatter() {
72+
putTestEntity("Fry", 14);
73+
putTestEntity("Fry", 12);
74+
putTestEntity("Fry", 10);
75+
76+
// Nested OR
77+
// (EQ OR EQ) AND LESS
78+
List<TestEntity> resultsNestedOr = box.query(
79+
TestEntity_.simpleString.equal("Fry")
80+
.or(TestEntity_.simpleString.equal("Sarah"))
81+
.and(TestEntity_.simpleInt.less(12))
82+
).build().find();
83+
// Only the Fry age 10.
84+
assertEquals(1, resultsNestedOr.size());
85+
assertEquals(10, resultsNestedOr.get(0).getSimpleInt());
86+
87+
// Nested AND
88+
// EQ OR (EQ AND LESS)
89+
List<TestEntity> resultsNestedAnd = box.query(
90+
TestEntity_.simpleString.equal("Fry")
91+
.or(TestEntity_.simpleString.equal("Sarah")
92+
.and(TestEntity_.simpleInt.less(12)))
93+
).build().find();
94+
// All three Fry's.
95+
assertEquals(3, resultsNestedAnd.size());
96+
}
97+
98+
@Test
99+
public void or() {
100+
putTestEntitiesScalars();
101+
Query<TestEntity> query = box.query(simpleInt.equal(2007).or(simpleLong.equal(3002))).build();
102+
List<TestEntity> entities = query.find();
103+
assertEquals(2, entities.size());
104+
assertEquals(3002, entities.get(0).getSimpleLong());
105+
assertEquals(2007, entities.get(1).getSimpleInt());
106+
}
107+
108+
@Test
109+
public void and() {
110+
putTestEntitiesScalars();
111+
// Result if OR precedence (wrong): {}, AND precedence (expected): {2008}
112+
Query<TestEntity> query = box.query(TestEntity_.simpleInt.equal(2006)
113+
.and(TestEntity_.simpleInt.equal(2007))
114+
.or(TestEntity_.simpleInt.equal(2008)))
115+
.build();
116+
List<TestEntity> entities = query.find();
117+
assertEquals(1, entities.size());
118+
assertEquals(2008, entities.get(0).getSimpleInt());
119+
}
120+
121+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package io.objectbox.query
2+
3+
import io.objectbox.TestEntity_
4+
import io.objectbox.kotlin.and
5+
import io.objectbox.kotlin.inValues
6+
import io.objectbox.kotlin.or
7+
import io.objectbox.kotlin.query
8+
import org.junit.Assert.assertEquals
9+
import org.junit.Test
10+
11+
/**
12+
* Preliminary tests and playground for new query API. At last should duplicate all query tests with new API.
13+
*
14+
* Note: expanding Java tests for now as we still have major Java users, see [QueryTest2].
15+
*/
16+
class QueryTestK : AbstractQueryTest() {
17+
18+
@Test
19+
fun newQueryApi() {
20+
putTestEntity("Fry", 14)
21+
putTestEntity("Fry", 12)
22+
putTestEntity("Fry", 10)
23+
24+
// current query API
25+
val query = box.query {
26+
less(TestEntity_.simpleInt, 12)
27+
or()
28+
inValues(TestEntity_.simpleLong, longArrayOf(1012))
29+
equal(TestEntity_.simpleString, "Fry")
30+
order(TestEntity_.simpleInt)
31+
}
32+
val results = query.find()
33+
assertEquals(2, results.size)
34+
assertEquals(10, results[0].simpleInt)
35+
assertEquals(12, results[1].simpleInt)
36+
37+
// suggested query API
38+
val newQuery = box.query(
39+
(TestEntity_.simpleInt.less(12) or TestEntity_.simpleLong.oneOf(longArrayOf(1012)))
40+
and TestEntity_.simpleString.equal("Fry")
41+
).order(TestEntity_.simpleInt).build()
42+
val resultsNew = newQuery.find()
43+
assertEquals(2, resultsNew.size)
44+
assertEquals(10, resultsNew[0].simpleInt)
45+
assertEquals(12, resultsNew[1].simpleInt)
46+
47+
val newQueryOr = box.query(
48+
// (EQ OR EQ) AND LESS
49+
(TestEntity_.simpleString.equal("Fry") or TestEntity_.simpleString.equal("Sarah"))
50+
and TestEntity_.simpleInt.less(12)
51+
).build().find()
52+
assertEquals(1, newQueryOr.size) // only the Fry age 10
53+
54+
val newQueryAnd = box.query(
55+
// EQ OR (EQ AND LESS)
56+
TestEntity_.simpleString.equal("Fry") or
57+
(TestEntity_.simpleString.equal("Sarah") and TestEntity_.simpleInt.less(12))
58+
).build().find()
59+
assertEquals(3, newQueryAnd.size) // all Fry's
60+
}
61+
62+
@Test
63+
fun intLessAndGreater() {
64+
putTestEntitiesScalars()
65+
val query = box.query(
66+
TestEntity_.simpleInt.greater(2003)
67+
and TestEntity_.simpleShort.less(2107)
68+
).build()
69+
assertEquals(3, query.count())
70+
}
71+
72+
@Test
73+
fun intBetween() {
74+
putTestEntitiesScalars()
75+
val query = box.query(
76+
TestEntity_.simpleInt.between(2003, 2006)
77+
).build()
78+
assertEquals(4, query.count())
79+
}
80+
81+
@Test
82+
fun intOneOf() {
83+
putTestEntitiesScalars()
84+
85+
val valuesInt = intArrayOf(1, 1, 2, 3, 2003, 2007, 2002, -1)
86+
val query = box.query(
87+
TestEntity_.simpleInt.oneOf(valuesInt).alias("int")
88+
).build()
89+
assertEquals(3, query.count())
90+
91+
val valuesInt2 = intArrayOf(2003)
92+
query.setParameters(TestEntity_.simpleInt, valuesInt2)
93+
assertEquals(1, query.count())
94+
95+
val valuesInt3 = intArrayOf(2003, 2007)
96+
query.setParameters("int", valuesInt3)
97+
assertEquals(2, query.count())
98+
}
99+
100+
101+
@Test
102+
fun or() {
103+
putTestEntitiesScalars()
104+
val query = box.query(
105+
TestEntity_.simpleInt.equal(2007) or TestEntity_.simpleLong.equal(3002)
106+
).build()
107+
val entities = query.find()
108+
assertEquals(2, entities.size.toLong())
109+
assertEquals(3002, entities[0].simpleLong)
110+
assertEquals(2007, entities[1].simpleInt.toLong())
111+
}
112+
113+
@Test
114+
fun and() {
115+
putTestEntitiesScalars()
116+
// Result if OR precedence (wrong): {}, AND precedence (expected): {2008}
117+
val query = box.query(
118+
TestEntity_.simpleInt.equal(2006) and TestEntity_.simpleInt.equal(2007) or TestEntity_.simpleInt.equal(2008)
119+
).build()
120+
val entities = query.find()
121+
assertEquals(1, entities.size.toLong())
122+
assertEquals(2008, entities[0].simpleInt.toLong())
123+
}
124+
125+
}

0 commit comments

Comments
 (0)