Skip to content

Commit 5d32db4

Browse files
Tests: add basic link condition tests objectbox/objectbox#934
1 parent d5e6651 commit 5d32db4

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

tests/objectbox-java-test/src/test/java/io/objectbox/relation/AbstractRelationTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,16 @@ public void initBoxes() {
5555
orderBox.removeAll();
5656
}
5757

58+
/**
59+
* Puts customer Joe.
60+
*/
5861
protected Customer putCustomer() {
62+
return putCustomer("Joe");
63+
}
64+
65+
Customer putCustomer(String name) {
5966
Customer customer = new Customer();
60-
customer.setName("Joe");
67+
customer.setName(name);
6168
customerBox.put(customer);
6269
return customer;
6370
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io.objectbox.relation;
2+
3+
import io.objectbox.query.Query;
4+
import io.objectbox.query.QueryBuilder;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertNotNull;
9+
10+
/**
11+
* Tests link conditions for queries to filter on related entities.
12+
* <p>
13+
* There are more extensive tests in integration tests.
14+
*/
15+
public class LinkQueryTest extends AbstractRelationTest {
16+
17+
@Test
18+
public void link_withRegularCondition() {
19+
Customer john = putCustomer("John");
20+
putOrder(john, "Apples");
21+
putOrder(john, "Oranges");
22+
23+
Customer alice = putCustomer("Alice");
24+
putOrder(alice, "Apples");
25+
putOrder(alice, "Bananas");
26+
27+
// link condition matches orders from Alice
28+
// simple regular condition matches single order for both
29+
QueryBuilder<Order> builder = orderBox
30+
.query(Order_.text.equal("Apples"));
31+
builder.link(Order_.customer)
32+
.apply(Customer_.name.equal("Alice").alias("name"));
33+
34+
try (Query<Order> query = builder.build()) {
35+
Order order = query.findUnique();
36+
assertNotNull(order);
37+
assertEquals("Apples", order.getText());
38+
assertEquals("Alice", order.getCustomer().getTarget().getName());
39+
}
40+
41+
// link condition matches orders from Alice
42+
// complex regular conditions matches two orders for John, one for Alice
43+
QueryBuilder<Order> builderComplex = orderBox
44+
.query(Order_.text.equal("Apples").or(Order_.text.equal("Oranges")));
45+
builderComplex.link(Order_.customer)
46+
.apply(Customer_.name.equal("Alice"));
47+
48+
try (Query<Order> query = builderComplex.build()) {
49+
Order order = query.findUnique();
50+
assertNotNull(order);
51+
assertEquals("Apples", order.getText());
52+
assertEquals("Alice", order.getCustomer().getTarget().getName());
53+
}
54+
}
55+
56+
}

0 commit comments

Comments
 (0)