Search in sources :

Example 31 with TestEntity

use of org.greenrobot.greendao.daotest.TestEntity in project greenDAO by greenrobot.

the class RelationEntityTest method testToOneUpdateKey.

public void testToOneUpdateKey() {
    RelationEntity entity = insertEntityWithRelations(42l);
    TestEntity testEntity = entity.getTestEntity();
    RelationEntity entity2 = insertEntityWithRelations(43l);
    TestEntity testEntity2 = entity2.getTestEntity();
    entity.setTestId(testEntity2.getId());
    assertEquals(testEntity2.getId(), entity.getTestEntity().getId());
    entity.setTestId(null);
    assertNull(entity.getTestEntity());
    entity.setTestId(testEntity.getId());
    assertEquals(testEntity.getId(), entity.getTestEntity().getId());
}
Also used : TestEntity(org.greenrobot.greendao.daotest.TestEntity) RelationEntity(org.greenrobot.greendao.daotest.RelationEntity)

Example 32 with TestEntity

use of org.greenrobot.greendao.daotest.TestEntity in project greenDAO by greenrobot.

the class RelationEntityTest method insertEntityWithRelations.

protected RelationEntity insertEntityWithRelations(Long testEntityId) {
    TestEntity testEntity = daoSession.getTestEntityDao().load(testEntityId);
    if (testEntity == null) {
        testEntity = new TestEntity(testEntityId);
        testEntity.setSimpleStringNotNull("mytest");
        daoSession.getTestEntityDao().insert(testEntity);
    }
    RelationEntity parentEntity = createEntity(null);
    parentEntity.setSimpleString("I'm a parent");
    parentEntity.setTestNotNull(testEntity);
    dao.insert(parentEntity);
    RelationEntity entity = createEntity(null);
    entity.setTestId(testEntityId);
    entity.setParentId(parentEntity.getId());
    entity.setSimpleString("findMe");
    entity.setTestNotNull(testEntity);
    dao.insert(entity);
    return entity;
}
Also used : TestEntity(org.greenrobot.greendao.daotest.TestEntity) RelationEntity(org.greenrobot.greendao.daotest.RelationEntity)

Example 33 with TestEntity

use of org.greenrobot.greendao.daotest.TestEntity in project greenDAO by greenrobot.

the class RxDaoTest method testDeleteByKeyInTxList.

public void testDeleteByKeyInTxList() {
    TestEntity foo = insertEntity("foo");
    TestEntity bar = insertEntity("bar");
    List<Long> list = new ArrayList<>();
    list.add(foo.getId());
    list.add(bar.getId());
    assertDeleted(rxDao.deleteByKeyInTx(list));
}
Also used : TestEntity(org.greenrobot.greendao.daotest.TestEntity) ArrayList(java.util.ArrayList)

Example 34 with TestEntity

use of org.greenrobot.greendao.daotest.TestEntity in project greenDAO by greenrobot.

the class TransactionTest method testUpdateTxFailed.

public void testUpdateTxFailed() {
    String sql = "CREATE UNIQUE INDEX test_simple_string_unique ON " + TestEntityDao.TABLENAME + "(" + Properties.SimpleString.columnName + ")";
    dao.getDatabase().execSQL(sql);
    ArrayList<TestEntity> entities = insert(2);
    TestEntity entity1 = entities.get(0);
    String valueBeforeUpdate = entity1.getSimpleString();
    entity1.setSimpleString("unique");
    entities.get(1).setSimpleString("unique");
    try {
        dao.updateInTx(entities);
        fail("Should have thrown");
    } catch (RuntimeException e) {
    // OK
    }
    dao.refresh(entity1);
    assertEquals(valueBeforeUpdate, entity1.getSimpleString());
}
Also used : TestEntity(org.greenrobot.greendao.daotest.TestEntity)

Example 35 with TestEntity

use of org.greenrobot.greendao.daotest.TestEntity in project greenDAO by greenrobot.

the class LazyListTest method testIterator.

protected void testIterator(ArrayList<TestEntity> list, LazyList<TestEntity> listLazy, boolean uncached) {
    ListIterator<TestEntity> iterator = listLazy.listIterator();
    try {
        iterator.previous();
        fail("previous should throw here");
    } catch (NoSuchElementException expected) {
    // OK
    }
    int size = list.size();
    for (int i = 0; i < size; i++) {
        assertTrue(iterator.hasNext());
        assertEquals(i > 0, iterator.hasPrevious());
        assertEquals(i, iterator.nextIndex());
        assertEquals(i - 1, iterator.previousIndex());
        if (i > 0) {
            TestEntity entityPrevious = list.get(i - 1);
            assertEquals(entityPrevious.getId(), iterator.previous().getId());
            iterator.next();
        }
        TestEntity entity = list.get(i);
        assertNull(listLazy.peek(i));
        TestEntity lazyEntity = iterator.next();
        if (uncached) {
            assertNull(listLazy.peek(i));
        } else {
            assertNotNull(listLazy.peek(i));
        }
        assertEquals(entity.getId(), lazyEntity.getId());
    }
    assertFalse(iterator.hasNext());
    try {
        iterator.next();
        fail("next should throw here");
    } catch (NoSuchElementException expected) {
    // OK
    }
}
Also used : TestEntity(org.greenrobot.greendao.daotest.TestEntity) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

TestEntity (org.greenrobot.greendao.daotest.TestEntity)72 ArrayList (java.util.ArrayList)16 RelationEntity (org.greenrobot.greendao.daotest.RelationEntity)4 List (java.util.List)3 DaoException (org.greenrobot.greendao.DaoException)2 TestSubscriber (rx.observers.TestSubscriber)2 Field (java.lang.reflect.Field)1 Method (java.lang.reflect.Method)1 Date (java.util.Date)1 Map (java.util.Map)1 NoSuchElementException (java.util.NoSuchElementException)1 WhereCondition (org.greenrobot.greendao.query.WhereCondition)1 Subscription (rx.Subscription)1