package org.ccci.gto.android.common.db;

import android.database.Cursor;
import android.support.test.runner.AndroidJUnit4;
import android.test.InstrumentationTestCase;

import org.ccci.gto.android.common.db.TestDao.TestContract.RootTable;
import org.ccci.gto.android.common.db.model.Root;
import org.junit.Test;
import org.junit.runner.RunWith;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

@RunWith(AndroidJUnit4.class)
public class AbstractDaoTest extends InstrumentationTestCase {
    private TestDao getDao() {
        return new TestDao(getInstrumentation().getContext());
    }

    @Test
    public void testGetTable() throws Exception {
        final TestDao dao = getDao();
        assertThat(dao.getTable(Root.class), is(RootTable.TABLE_NAME));
    }

    @Test
    public void testGetCursor() {
        final TestDao dao = getDao();
        final Cursor cursor = dao.getCursor(Query.select(Root.class));

        assertThat(cursor.getColumnName(0), is("_id"));
        assertThat(cursor.getColumnName(1), is("test"));
    }

    @Test
    public void testInsert() {
        final TestDao dao = getDao();

        insertRow(dao, 1, "1");

        Root foundRoot = dao.find(Root.class, 1);
        assertNotNull(foundRoot);
        assertThat(foundRoot.test, is("1"));
    }

    private void insertRow(TestDao dao, int id, String test) {
        Root testRoot = new Root();
        testRoot.id = id;
        testRoot.test = test;

        dao.insert(testRoot);
    }

    @Override
    protected void tearDown() throws Exception {
        final TestDao dao = getDao();
        dao.delete(Root.class, RootTable.SQL_WHERE_ANY);
        super.tearDown();
    }
}

@@ -282,16 +282,72 @@
 l.Root;%0A
+import org.ccci.gto.android.common.db.util.CursorUtils;%0A
 import o
@@ -1566,24 +1566,1752 @@
 ot);%0A    %7D%0A%0A
+    @Test%0A    public void testWhere() %7B%0A        final TestDao dao = getDao();%0A%0A        insertRow(dao, 1, %221%22);%0A        insertRow(dao, 2, %222%22);%0A%0A        Cursor cursor = dao.getCursor(%0A            Query.select(Root.class).where(RootTable.SQL_WHERE_PRIMARY_KEY.args(2)));%0A        cursor.moveToFirst();%0A%0A        assertThat(CursorUtils.getString(cursor, RootTable.COLUMN_TEST), is(%222%22));%0A    %7D%0A%0A    @Test%0A    public void testGroupBy() %7B%0A        final TestDao dao = getDao();%0A%0A        insertRow(dao, 1, %221%22);%0A        insertRow(dao, 2, %222%22);%0A        insertRow(dao, 3, %222%22);%0A        insertRow(dao, 4, %222%22);%0A        insertRow(dao, 5, %223%22);%0A%0A        Cursor cursor = dao.getCursor(%0A            Query.select(Root.class).groupBy(RootTable.COLUMN_TEST));%0A%0A        assertThat(cursor.getCount(), is(3));%0A%0A        cursor.moveToFirst();%0A%0A        assertThat(CursorUtils.getString(cursor, RootTable.COLUMN_TEST), is(%221%22));%0A%0A        cursor.moveToNext();%0A%0A        assertThat(CursorUtils.getString(cursor, RootTable.COLUMN_TEST), is(%222%22));%0A%0A        cursor.moveToNext();%0A%0A        assertThat(CursorUtils.getString(cursor, RootTable.COLUMN_TEST), is(%223%22));%0A    %7D%0A%0A    @Test%0A    public void testHaving() %7B%0A        final TestDao dao = getDao();%0A%0A        insertRow(dao, 1, %221%22);%0A        insertRow(dao, 2, %222%22);%0A        insertRow(dao, 3, %222%22);%0A        insertRow(dao, 4, %222%22);%0A        insertRow(dao, 5, %223%22);%0A%0A        Expression max = RootTable.FIELD_ID.max().eq(3);%0A        Cursor cursor = dao.getCursor(%0A                Query.select(Root.class).groupBy(RootTable.COLUMN_ID).having(max));%0A%0A        assertThat(cursor.getCount(), is(1));%0A%0A        cursor.moveToFirst();%0A%0A        assertThat(CursorUtils.getString(cursor, RootTable.COLUMN_TEST), is(%222%22));%0A    %7D%0A%0A
     @Overrid

Add some tests for various queries