use of io.requery.query.Tuple in project requery by requery.
the class AutoValueModelTest method testInsert.
@Test
public void testInsert() {
Set<Integer> ids = new HashSet<>();
for (int i = 0; i < 10; i++) {
Person p = Person.builder().setName("person" + i).setAge(30).setEmail("test@example.com").setUUID(UUID.randomUUID()).setBirthday(new Date()).setAbout("About me").build();
Integer key = data.insert(p, Integer.class);
assertTrue(key > 0);
ids.add(key);
}
final Set<Integer> selected = new HashSet<>();
data.select(PersonType.ID).get().each(new Consumer<Tuple>() {
@Override
public void accept(Tuple tuple) {
selected.add(tuple.get(PersonType.ID));
}
});
assertEquals(ids, selected);
}
use of io.requery.query.Tuple in project requery by requery.
the class FunctionalTest method testQueryCase.
@Test
public void testQueryCase() {
String[] names = new String[] { "Carol", "Bob", "Jack" };
for (String name : names) {
Person person = randomPerson();
person.setName(name);
data.insert(person);
}
Result<Tuple> a = data.select(Person.NAME, Case.type(String.class).when(Person.NAME.equal("Bob"), "B").when(Person.NAME.equal("Carol"), "C").elseThen("Unknown")).from(Person.class).orderBy(Person.NAME).get();
List<Tuple> list = a.toList();
assertTrue(list.get(0).get(1).equals("B"));
assertTrue(list.get(1).get(1).equals("C"));
assertTrue(list.get(2).get(1).equals("Unknown"));
a = data.select(Person.NAME, Case.type(Integer.class).when(Person.NAME.equal("Bob"), 1).when(Person.NAME.equal("Carol"), 2).elseThen(0)).orderBy(Person.NAME).get();
list = a.toList();
assertTrue(list.get(0).get(1).equals(1));
assertTrue(list.get(1).get(1).equals(2));
assertTrue(list.get(2).get(1).equals(0));
}
use of io.requery.query.Tuple in project requery by requery.
the class FunctionalTest method testQuerySelectDistinct.
@Test
public void testQuerySelectDistinct() {
for (int i = 0; i < 10; i++) {
Person person = randomPerson();
person.setName(String.valueOf(i / 2));
data.insert(person);
}
try (Result<Tuple> result = data.select(Person.NAME).distinct().get()) {
assertEquals(5, result.toList().size());
}
}
use of io.requery.query.Tuple in project requery by requery.
the class FunctionalTest method testQuerySelectSubstr.
@Test
public void testQuerySelectSubstr() {
// TODO fix for SQLServer
Person person = randomPerson();
person.setName(" Name");
data.insert(person);
Tuple result = data.select(Person.NAME.substr(3, 6).as("name")).get().first();
String name = result.get(0);
assertEquals(name, "Name");
}
use of io.requery.query.Tuple in project requery by requery.
the class FunctionalTest method testQuerySelectMin.
@SuppressWarnings("MagicConstant")
@Test
public void testQuerySelectMin() {
for (int i = 0; i < 10; i++) {
Person person = randomPerson();
if (i == 9) {
Calendar calendar = Calendar.getInstance();
calendar.set(1800, Calendar.NOVEMBER, 11, 0, 0, 0);
person.setBirthday(calendar.getTime());
}
data.insert(person);
}
try (Result<Tuple> query = data.select(Person.BIRTHDAY.min().as("oldestBday")).get()) {
Date date = query.first().get("oldestBday");
Calendar c = Calendar.getInstance();
c.setTime(date);
// just check the year...
assertTrue(c.get(Calendar.YEAR) == 1800);
}
}
Aggregations