@Test public void testFillResult() { data.delete(Person.class).get(); Person person = randomPerson(); data.insert(person); assertEquals(1, data.select(Person.class).get().collect(new HashSet<Person>()).size()); }
@Test public void testQueryCompoundConditions() { Person person = randomPerson(); person.setAge(75); data.insert(person); Person p = data.select(Person.class).where(Person.AGE.gte(75)).get().first(); }
@Test public void testUpdateNoChanges() { Person person = randomPerson(); data.insert(person); assertTrue(person.getId() > 0); data.update(person); }
@Test public void testDeleteManyToMany() { final Person person = randomPerson(); data.insert(person); final Collection<Group> groups = new ArrayList<>(); data.runInTransaction( new Callable<Object>() { @Override public Object call() throws Exception { for (int i = 0; i < 10; i++) { Group group = new Group(); group.setName("DeleteGroup" + i); data.insert(group); person.getGroups().add(group); groups.add(group); } data.update(person); return null; } }); for (Group g : groups) { person.getGroups().remove(g); } data.update(person); }
@Test public void testListResult() { data.delete(Person.class).get(); Person person = randomPerson(); data.insert(person); assertEquals(1, data.select(Person.class).get().toList().size()); }
@Test public void testUpdate() { Person person = randomPerson(); data.insert(person); assertTrue(person.getId() > 0); person.setName("Bob Smith"); Calendar calendar = Calendar.getInstance(); calendar.set(1983, Calendar.NOVEMBER, 11); person.setBirthday(calendar.getTime()); EntityProxy<Person> proxy = Person.$TYPE.proxyProvider().apply(person); int count = 0; for (Property ignored : proxy.filterProperties( new Predicate<Property<Person, ?>>() { @Override public boolean test(Property value) { return value.state() == PropertyState.MODIFIED; } })) { count++; } assertEquals(2, count); data.update(person); for (Property ignored : proxy.filterProperties( new Predicate<Property<Person, ?>>() { @Override public boolean test(Property value) { return value.state() == PropertyState.MODIFIED; } })) { fail(); } }
@Test public void testQueryNotNull() { for (int i = 0; i < 10; i++) { Person person = randomPerson(); data.insert(person); } assertNotNull(data.select(Person.class).where(Person.NAME.notNull()).get().first()); }
@Test public void testInsert() { Person person = randomPerson(); data.insert(person); assertTrue(person.getId() > 0); Person cached = data.select(Person.class).where(Person.ID.equal(person.getId())).get().first(); assertSame(cached, person); }
@Test public void testQueryBetween() { Person person = randomPerson(); person.setAge(75); data.insert(person); Person p = data.select(Person.class).where(Person.AGE.between(50, 100)).get().first(); assertTrue(p == person); }
@Test public void testFindByKey() { Person person = randomPerson(); data.insert(person); assertTrue(person.getId() > 0); Person other = data.findByKey(Person.class, person.getId()); assertSame(person, other); }
@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()); }
@Test public void testUpdateOneToOneCascade() { Address address = randomAddress(); Person person = randomPerson(); data.insert(person); person.setAddress(address); data.update(person); assertSame(address.getPerson(), person); }
@Test public void testQuerySelectTrim() { // TODO fix use ltrim/rtrim for SQLServer Person person = randomPerson(); person.setName(" Name "); data.insert(person); Tuple result = data.select(Person.NAME.trim().as("name")).get().first(); String name = result.get(0); assertEquals(name, "Name"); }
@Test public void testDeleteOne() { Person person = randomPerson(); data.insert(person); assertTrue(person.getId() > 0); data.delete(person); Person cached = data.select(Person.class).where(Person.ID.equal(person.getId())).get().firstOrNull(); assertNull(cached); }
@Test public void testDeleteCascadeOneToOne() { Address address = randomAddress(); data.insert(address); assertTrue(address.getId() > 0); Person person = randomPerson(); data.insert(person); data.delete(person); assertNull(address.getPerson()); }
@Test public void testRefreshAttributes() { Person person = randomPerson(); data.insert(person); Phone phone = randomPhone(); person.getPhoneNumbers().add(phone); data.update(person); data.refresh(person, Person.NAME, Person.PHONE_NUMBERS_SET, Person.ADDRESS, Person.EMAIL); assertTrue(person.getPhoneNumbersSet().contains(phone)); }
@Test public void testRefreshAll() { Person person = randomPerson(); data.insert(person); Phone phone = randomPhone(); person.getPhoneNumbers().add(phone); data.update(person); data.refreshAll(person); assertTrue(person.getPhoneNumbersSet().contains(phone)); }
@Test public void testQuerySelectWhereIn() { final String name = "Hello!"; Person person = randomPerson(); person.setName(name); data.insert(person); Group group = new Group(); group.setName("Hello!"); data.insert(group); person.getGroups().add(group); data.update(person); Return<Result<Tuple>> groupNames = data.select(Group.NAME).where(Group.NAME.equal(name)); Person p = data.select(Person.class).where(Person.NAME.in(groupNames)).get().first(); assertEquals(p.getName(), name); p = data.select(Person.class).where(Person.NAME.notIn(groupNames)).get().firstOrNull(); assertNull(p); p = data.select(Person.class) .where(Person.NAME.in(Arrays.asList("Hello!", "Other"))) .get() .first(); assertEquals(p.getName(), name); p = data.select(Person.class) .where(Person.NAME.in(Collections.singleton("Hello!"))) .get() .first(); assertEquals(p.getName(), name); assertTrue( data.select(Person.class) .where(Person.NAME.notIn(Collections.singleton("Hello!"))) .get() .toList() .isEmpty()); }
@Test(expected = PersistenceException.class) public void testViolateUniqueConstraint() { UUID uuid = UUID.randomUUID(); Person p1 = randomPerson(); p1.setUUID(uuid); data.insert(p1); Person p2 = randomPerson(); p2.setUUID(uuid); data.insert(p2); fail(); }
@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()); } }
@Test public void testDeleteAll() { final String name = "someName"; for (int i = 0; i < 10; i++) { Person person = randomPerson(); person.setName(name); data.insert(person); } assertTrue(data.delete(Person.class).get().value() > 0); assertTrue(data.select(Person.class).get().toList().isEmpty()); }
@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)); } }
@Test public void testInsertOneToManyInverseThroughSet() { Person person = randomPerson(); data.insert(person); Phone phone1 = randomPhone(); Phone phone2 = randomPhone(); person.getPhoneNumbers().add(phone1); person.getPhoneNumbers().add(phone2); data.update(person); assertEquals(2, person.getPhoneNumbersSet().size()); assertTrue(person.getPhoneNumbersSet().containsAll(Arrays.asList(phone1, phone2))); }
@Test public void testQuerySelectCountWhere() { for (int i = 0; i < 10; i++) { Person person = randomPerson(); if (i == 0) { person.setName("countme"); } data.insert(person); } assertEquals( 1, data.count(Person.class).where(Person.NAME.eq("countme")).get().value().intValue()); }
@Test public void testDeleteBatch() { List<Person> persons = new ArrayList<>(); for (int i = 0; i < 100; i++) { Person person = randomPerson(); persons.add(person); } data.insert(persons); assertEquals(100, data.count(Person.class).get().value().intValue()); data.delete(persons); assertEquals(0, data.count(Person.class).get().value().intValue()); }
@Test public void testInsertOneToManyOneInsert() { Person person = randomPerson(); Phone phone1 = randomPhone(); Phone phone2 = randomPhone(); person.getPhoneNumbers().add(phone1); person.getPhoneNumbers().add(phone2); data.insert(person); data.refresh(person, Person.PHONE_NUMBERS); HashSet<Phone> set = new HashSet<>(person.getPhoneNumbers().toList()); assertEquals(2, set.size()); assertTrue(set.containsAll(Arrays.asList(phone1, phone2))); }
@Test public void testVersionIncrement() { Group group = new Group(); group.setName("group"); group.setType(GroupType.PRIVATE); data.insert(group); // Assert.assertTrue(group.version() == 1); group.setType(GroupType.PUBLIC); data.update(group); data.refresh(group, Group.VERSION); // System.out.println("group.version + " + group.version()); assertTrue(group.getVersion() > 0); }
@Test public void testDeleteCascadeOneToMany() { Person person = randomPerson(); data.insert(person); Phone phone1 = randomPhone(); phone1.setOwner(person); data.insert(phone1); int phoneId = phone1.getId(); person.getPhoneNumbers(); data.delete(person); Phone phone = data.select(Phone.class).where(Phone.ID.equal(phoneId)).get().firstOrNull(); assertNull(phone); }
@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()); } }
@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()); } }