assertTrue(result.isEmpty());
}
public void testQueryOperators(IDatabases dbs) throws IOException, SQLException {
IUserPersistence users = dbs.getDatabase1().users();
users.deleteAll();
User brad = users.createDefaultInstance().setHandle("Brad").setBio("Soccer player").setNumPosts(1).setCreatedAtMillis(1l);
User brandon = users.createDefaultInstance().setHandle("Brandon").setBio("Formula 1 driver").setNumPosts(2).setCreatedAtMillis(1l).setSomeDatetime(0l);
User casey = users.createDefaultInstance().setHandle("Casey").setBio("Singer").setNumPosts(2).setCreatedAtMillis(2l);
User john = users.createDefaultInstance().setHandle("John").setBio("Ice skater").setNumPosts(3).setCreatedAtMillis(2l);
User james = users.createDefaultInstance().setHandle("James").setBio("Surfer").setNumPosts(5).setCreatedAtMillis(3l).setSomeDatetime(1000000l);
brad.save();
brandon.save();
casey.save();
john.save();
james.save();
Set<User> result;
// Equal To
result = users.query().whereHandle(equalTo("Brad")).find();
assertEquals(1, result.size());
assertTrue(result.contains(brad));
// Between
result = users.query().whereNumPosts(between(4, 8)).find();
assertEquals(1, result.size());
assertTrue(result.contains(james));
// Less Than
result = users.query().whereCreatedAtMillis(lessThan(2l)).find();
assertEquals(2, result.size());
assertTrue(result.contains(brad));
assertTrue(result.contains(brandon));
// Greater Than
result = users.query().whereCreatedAtMillis(greaterThan(1l)).find();
assertEquals(3, result.size());
// Less Than Or Equal To
result = users.query().whereCreatedAtMillis(lessThanOrEqualTo(2l)).find();
assertEquals(4, result.size());
// Greater Than Or Equal To
result = users.query().whereCreatedAtMillis(greaterThanOrEqualTo(1l)).find();
assertEquals(5, result.size());
// Ends With
result = users.query().whereBio(endsWith("er")).find();
assertEquals(5, result.size());
// StartsWith
result = users.query().whereBio(startsWith("er")).find();
assertTrue(result.isEmpty());
// In with empty collection
result = users.query().whereSomeDatetime(in(Collections.<Long>emptySet()))
.find();
assertTrue(result.isEmpty());
// NotIn with empty collection
try {
users.query().whereSomeDatetime(notIn(Collections.<Long>emptySet()))
.find();
fail("Using a NotIn operator with an empty collection should throw an exception.");
} catch (IllegalArgumentException e) {
//This is expected
}
// Contains and In
result = users.query().whereBio(contains("f"))
.whereNumPosts(in(1, 3, 5))
.find();
assertEquals(1, result.size());
assertTrue(result.contains(james));
// Not In and Not Equal To
result = users.query().whereHandle(notIn("Brad", "Brandon", "Jennifer", "John"))
.whereNumPosts(notEqualTo(5))
.find();
assertEquals(1, result.size());
assertTrue(result.contains(casey));
result = users.query().whereSomeDatetime(JackMatchers.<Long>isNull()).find();
assertEquals(3, result.size());
result = users.query().whereSomeDatetime(JackMatchers.<Long>isNotNull()).find();
assertEquals(2, result.size());
assertTrue(result.contains(brandon));
assertTrue(result.contains(james));
// If a null parameter is passed, an exeception should be thrown
try {
users.query().whereHandle(in(null, "brandon")).find();
fail("an In query with one null parameter should throw an exception");
} catch (IllegalArgumentException e) {
// This exception is expected
}
}