
package novoda.lib.sqliteprovider.util;

import android.database.sqlite.SQLiteDatabase;
import android.test.*;

import novoda.lib.sqliteprovider.sqlite.IDatabaseMetaInfo.SQLiteType;

import java.util.List;
import java.util.Map;

public class DBUtilsTest extends AndroidTestCase {

    private static final String CREATE_TABLES = "CREATE TABLE t1(id INTEGER, name TEXT, r REAL);\n";
    private static final String CREATE_2_TABLES = "CREATE TABLE T(id INTEGER);\nCREATE TABLE T2(id INTEGER);\n";
    private static final String CREATE_2_TABLES_WITH_FOREIGN_KEY = "CREATE TABLE t(id INTEGER);\nCREATE TABLE t2(id INTEGER, t_id integer);\n";
    private static final String CREATE_TABLE_WITH_CONSTRAIN = "CREATE TABLE t(id INTEGER, const TEXT UNIQUE NOT NULL);";

    @Override
    protected void setUp() throws Exception {
        setContext(new RenamingDelegatingContext(getContext(), "_test_"));
        super.setUp();
    }

    @Override
    protected void tearDown() throws Exception {
        for (String db : getContext().databaseList()) {
            getContext().deleteDatabase(db);
        }
        super.tearDown();
    }

    public void testGetDBName() throws Exception {
        android.database.DatabaseUtils.createDbFromSqlStatements(getContext(), "testing.db", 1, CREATE_2_TABLES);
        SQLiteDatabase db = getContext().openOrCreateDatabase("testing.db", 0, null);
        List<String> tables = DBUtils.getTables(db);
        MoreAsserts.assertContentsInAnyOrder(tables, "T", "T2");
    }

    public void testGetForeignKey() throws Exception {
        android.database.DatabaseUtils.createDbFromSqlStatements(getContext(), "testing.db", 1,
                CREATE_2_TABLES_WITH_FOREIGN_KEY);
        SQLiteDatabase db = getContext().openOrCreateDatabase("testing.db", 0, null);
        List<String> ft = DBUtils.getForeignTables(db, "t2");
        MoreAsserts.assertContentsInAnyOrder(ft, "t");
    }

    public void testGettingFieldsMap() throws Exception {
        android.database.DatabaseUtils.createDbFromSqlStatements(getContext(), "testing.db", 1, CREATE_TABLES);
        SQLiteDatabase db = getContext().openOrCreateDatabase("testing.db", 0, null);
        Map<String, SQLiteType> ft = DBUtils.getFields(db, "t1");
        MoreAsserts.assertContentsInAnyOrder(ft.keySet(), "id", "name", "r");
    }

    public void testGettingProjectionMap() throws Exception {
        android.database.DatabaseUtils.createDbFromSqlStatements(getContext(), "testing.db", 1, CREATE_2_TABLES_WITH_FOREIGN_KEY);
        SQLiteDatabase db = getContext().openOrCreateDatabase("testing.db", 0, null);
        Map<String, String> ft = DBUtils.getProjectionMap(db, "t", "t2");
        assertEquals("t.id", ft.get("id"));
        assertEquals("t2.id", ft.get("t2.id"));
    }

    public void testGettingUniqueConstrains() throws Exception {
        android.database.DatabaseUtils.createDbFromSqlStatements(getContext(), "testing.db", 1, CREATE_TABLE_WITH_CONSTRAIN);

        SQLiteDatabase db = getContext().openOrCreateDatabase("testing.db", 0, null);
        List<String> constrains = DBUtils.getUniqueConstrains(db, "t");
        MoreAsserts.assertContentsInAnyOrder(constrains, "const");
    }

    public void testGettingUniqueConstrainsIsEmpty() throws Exception {
        android.database.DatabaseUtils.createDbFromSqlStatements(getContext(), "testing.db", 1, CREATE_TABLES);
        SQLiteDatabase db = getContext().openOrCreateDatabase("testing.db", 0, null);
        List<String> constrains = DBUtils.getUniqueConstrains(db, "t");
        MoreAsserts.assertEmpty(constrains);
    }
}

@@ -623,15 +623,15 @@
 _id 
-integer
+INTEGER
 );%5Cn
@@ -1657,24 +1657,8 @@
 , 1,
-%0A               
  CRE
@@ -2685,16 +2685,24 @@
 ls(%22t.id
+ AS t_id
 %22, ft.ge
@@ -2704,16 +2704,18 @@
 ft.get(%22
+t_
 id%22));%0A 
@@ -2740,16 +2740,25 @@
 s(%22t2.id
+ AS t2_id
 %22, ft.ge
@@ -2762,17 +2762,17 @@
 .get(%22t2
-.
+_
 id%22));%0A 

Fix for test getting the projection map. Guess it had been updated but the test hadnt since mvn wasnt running them