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

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

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

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

@RunWith(AndroidJUnit4.class)
public class CursorUtilsIT {
    private TestDao getDao() {
        return TestDao.getInstance(InstrumentationRegistry.getContext());
    }

    @Test
    public void testGetStringDefaultValue() throws Exception {
        final TestDao dao = getDao();
        final String defValue = "default";

        // create a couple objects to test
        dao.insert(new Root(1, null));
        dao.insert(new Root(2, ""));
        dao.insert(new Root(3, "3"));

        // test default when field isn't present
        Cursor c = dao.getCursor(Query.select(Root.class).projection(RootTable.COLUMN_ID).orderBy(RootTable.COLUMN_ID));
        c.moveToPosition(-1);
        while (c.moveToNext()) {
            assertThat(CursorUtils.getString(c, RootTable.COLUMN_TEST, defValue), is(defValue));
        }
        c.close();

        // test default when field is present
        c = dao.getCursor(Query.select(Root.class).orderBy(RootTable.COLUMN_ID));
        c.moveToPosition(0);
        assertThat(CursorUtils.getLong(c, RootTable.COLUMN_ID), is(1L));
        assertThat("null column value should return default value",
                   CursorUtils.getString(c, RootTable.COLUMN_TEST, defValue), is(defValue));
        c.moveToPosition(1);
        assertThat(CursorUtils.getLong(c, RootTable.COLUMN_ID), is(2L));
        assertThat(CursorUtils.getString(c, RootTable.COLUMN_TEST, defValue), is(not(defValue)));
        c.moveToPosition(2);
        assertThat(CursorUtils.getLong(c, RootTable.COLUMN_ID), is(3L));
        assertThat(CursorUtils.getString(c, RootTable.COLUMN_TEST, defValue), is(not(defValue)));
        c.close();
    }

    @Test
    public void verifyGetLongDefaultValue() throws Exception {
        final TestDao dao = getDao();
        final long defValue = 9;

        // create a few objects
        dao.insert(new Root(1, null));
        dao.insert(new Root(2, "2"));

        // test default when field isn't present
        Cursor c = dao.getCursor(Query.select(Root.class).projection(RootTable.COLUMN_ID).orderBy(RootTable.COLUMN_ID));
        c.moveToPosition(-1);
        while (c.moveToNext()) {
            assertThat(CursorUtils.getLong(c, RootTable.COLUMN_TEST, defValue), is(defValue));
        }
        c.close();

        // test default when field is present
        c = dao.getCursor(Query.select(Root.class).orderBy(RootTable.COLUMN_ID));
        c.moveToPosition(0);
        assertThat(CursorUtils.getLong(c, RootTable.COLUMN_ID), is(1L));
        assertThat("null column value should return default value",
                   CursorUtils.getLong(c, RootTable.COLUMN_TEST, defValue), is(defValue));
        c.moveToPosition(1);
        assertThat(CursorUtils.getLong(c, RootTable.COLUMN_ID), is(2L));
        assertThat(CursorUtils.getLong(c, RootTable.COLUMN_TEST, defValue), is(not(defValue)));
        c.close();
    }

    @After
    public void reset() throws Exception {
        getDao().reset();
    }
}

@@ -399,16 +399,41 @@
 .After;%0A
+import org.junit.Ignore;%0A
 import o
@@ -3497,24 +3497,806 @@
 e();%0A    %7D%0A%0A
+    // XXX: currently disabled because we don't support invalid values being returned from the database. We really%0A    // XXX: should support this at some point though.%0A    @Test%0A    @Ignore%0A    public void verifyGetLongInvalidValue() throws Exception %7B%0A        final TestDao dao = getDao();%0A        final long defValue = 9;%0A%0A        // create a test object%0A        dao.insert(new Root(1, %22a%22));%0A%0A        final Cursor c = dao.getCursor(Query.select(Root.class).orderBy(RootTable.COLUMN_ID));%0A        c.moveToPosition(0);%0A        assertThat(CursorUtils.getLong(c, RootTable.COLUMN_ID), is(1L));%0A        assertThat(%22invalid column value should return default value%22,%0A                   CursorUtils.getLong(c, RootTable.COLUMN_TEST, defValue), is(defValue));%0A        c.close();%0A    %7D%0A%0A
     @After%0A 

write test for unsupported functionality. At some point we should support this functionality though.