Search in sources :

Example 1 with Tuple

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);
}
Also used : Person(io.requery.test.autovalue.Person) Date(java.util.Date) Tuple(io.requery.query.Tuple) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 2 with Tuple

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));
}
Also used : Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Tuple(io.requery.query.Tuple) Test(org.junit.Test)

Example 3 with Tuple

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());
    }
}
Also used : Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Tuple(io.requery.query.Tuple) Test(org.junit.Test)

Example 4 with Tuple

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");
}
Also used : Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Tuple(io.requery.query.Tuple) Test(org.junit.Test)

Example 5 with Tuple

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);
    }
}
Also used : Calendar(java.util.Calendar) Person(io.requery.test.model.Person) Group_Person(io.requery.test.model.Group_Person) Tuple(io.requery.query.Tuple) Date(java.util.Date) Test(org.junit.Test)

Aggregations

Tuple (io.requery.query.Tuple)14 Test (org.junit.Test)13 Group_Person (io.requery.test.model.Group_Person)12 Person (io.requery.test.model.Person)12 Group (io.requery.test.model.Group)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 Attribute (io.requery.meta.Attribute)1 QueryAttribute (io.requery.meta.QueryAttribute)1 EntityProxy (io.requery.proxy.EntityProxy)1 AliasedExpression (io.requery.query.AliasedExpression)1 Expression (io.requery.query.Expression)1 QueryElement (io.requery.query.element.QueryElement)1 WHERE (io.requery.sql.Keyword.WHERE)1 Person (io.requery.test.autovalue.Person)1 Consumer (io.requery.util.function.Consumer)1 Calendar (java.util.Calendar)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1