Ejemplo n.º 1
0
 @Test
 public void testSingleQueryExecute() {
   data.insert(randomPersons(10));
   Result<Person> result = data.select(Person.class).get();
   assertEquals(10, result.toList().size());
   Person person = randomPerson();
   data.insert(person);
   assertEquals(11, result.toList().size());
 }
Ejemplo n.º 2
0
 @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());
   }
 }
Ejemplo n.º 3
0
 @Test
 public void testQueryOrderBy() {
   for (int i = 0; i < 5; i++) {
     Person person = randomPerson();
     person.setAge(i);
     data.insert(person);
   }
   try (Result<Tuple> query = data.select(Person.AGE).orderBy(Person.AGE.desc()).get()) {
     Integer i = query.first().get(0);
     assertTrue(i.equals(4));
   }
 }
Ejemplo n.º 4
0
 @Test
 public void testSingleQueryWhereNull() {
   data.delete(Person.class).where(Person.NAME.isNull()).get();
   Person person = randomPerson();
   person.setName(null);
   data.insert(person);
   try (Result<Person> query = data.select(Person.class).where(Person.NAME.equal(null)).get()) {
     assertEquals(1, query.toList().size());
   }
   try (Result<Person> query = data.select(Person.class).where(Person.NAME.isNull()).get()) {
     assertEquals(1, query.toList().size());
   }
 }
Ejemplo n.º 5
0
 @Test
 public void testSingleQueryWhere() {
   final String name = "duplicateFirstName";
   data.delete(Person.class).where(Person.NAME.equal(name)).get();
   for (int i = 0; i < 10; i++) {
     Person person = randomPerson();
     person.setName(name);
     data.insert(person);
   }
   try (Result<Person> query = data.select(Person.class).where(Person.NAME.equal(name)).get()) {
     assertEquals(10, query.toList().size());
   }
 }
Ejemplo n.º 6
0
 @Test
 public void testQueryMap() {
   int count = 10;
   for (int i = 0; i < count; i++) {
     Person person = randomPerson();
     if (i == 0) {
       person.setEmail("*****@*****.**");
     }
     data.insert(person);
   }
   Result<Person> result = data.select(Person.class).get();
   Map<String, Person> map = result.toMap(Person.EMAIL, new ConcurrentHashMap<String, Person>());
   assertNotNull(map.get("*****@*****.**"));
   map = result.toMap(Person.EMAIL);
   assertNotNull(map.get("*****@*****.**"));
 }
Ejemplo n.º 7
0
 @Test
 public void testQueryConsume() {
   int count = 10;
   for (int i = 0; i < count; i++) {
     Person person = randomPerson();
     data.insert(person);
   }
   final int[] counts = new int[] {0};
   Result<Person> result = data.select(Person.class).get();
   result.each(
       new Consumer<Person>() {
         @Override
         public void accept(Person e) {
           counts[0] = counts[0] + 1;
         }
       });
   assertEquals(counts[0], count);
 }
Ejemplo n.º 8
0
 @Test
 public void testQueryGroupBy() {
   for (int i = 0; i < 5; i++) {
     Person person = randomPerson();
     person.setAge(i);
     data.insert(person);
   }
   try (Result<Tuple> result =
       data.select(Person.AGE).groupBy(Person.AGE).having(Person.AGE.greaterThan(3)).get()) {
     assertTrue(result.toList().size() == 1);
   }
   assertTrue(
       data.select(Person.AGE)
           .groupBy(Person.AGE)
           .having(Person.AGE.lessThan(0))
           .get()
           .toList()
           .isEmpty());
 }
Ejemplo n.º 9
0
 @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);
     assertTrue(c.get(Calendar.YEAR) == 1800); // just check the year...
   }
 }
Ejemplo n.º 10
0
 @Test
 public void testQueryCoalesce() {
   Person person = randomPerson();
   person.setName("Carol");
   person.setEmail(null);
   data.insert(person);
   person = randomPerson();
   person.setName("Bob");
   person.setEmail("*****@*****.**");
   person.setHomepage(null);
   data.insert(person);
   Result<Tuple> result = data.select(Coalesce.coalesce(Person.EMAIL, Person.NAME)).get();
   List<Tuple> list = result.toList();
   List<String> values = new ArrayList<>();
   for (Tuple tuple : list) {
     values.add(tuple.get(0).toString());
   }
   assertEquals(values.size(), 2);
   assertTrue(values.contains("Carol"));
   assertTrue(values.contains("*****@*****.**"));
 }
Ejemplo n.º 11
0
  @Test
  public void testQuerySelectCount() {
    data.insert(randomPersons(10));
    try (Result<Tuple> result = data.select(Count.count(Person.class).as("bb")).get()) {
      assertTrue(result.first().get("bb").equals(10));
    }
    try (Result<Tuple> result = data.select(Count.count(Person.class)).get()) {
      assertTrue(result.first().get(0).equals(10));
    }
    assertEquals(10, data.count(Person.class).get().value().intValue());

    data.count(Person.class)
        .get()
        .consume(
            new Consumer<Integer>() {
              @Override
              public void accept(Integer integer) {
                assertTrue(integer == 10);
              }
            });
  }
Ejemplo n.º 12
0
  @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));
  }