Android Open Source - android-sqlite-helper Create Table Test






From Project

Back to project page android-sqlite-helper.

License

The source code is released under:

Apache License

If you think the Android project android-sqlite-helper listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.ichigotake.sqlitehelper.ddl;
// w w  w.  ja  v  a2 s  .  c  o m
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

import junit.framework.Assert;

import net.ichigotake.sqlitehelper.DatabaseHelper;
import net.ichigotake.sqlitehelper.MockConfiguration;
import net.ichigotake.sqlitehelper.MockTable;
import net.ichigotake.sqlitehelper.schema.Index;
import net.ichigotake.sqlitehelper.schema.Table;
import net.ichigotake.sqlitehelper.schema.TableSchema;
import net.ichigotake.sqlitehelper.schema.UniqueField;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class CreateTableTest {
    
    private Table mock() {
        return new MockTableForCreateTable();
    }
    
    private DatabaseHelper sqliteHelper() {
        return new DatabaseHelper(Robolectric.application, new MockConfiguration());
    }
    
    @Test
    public void testBuildQueryAsCreateTableIfNotExists() {
        DatabaseHelper sqlite = sqliteHelper();
        CreateTable createTable = new CreateTable(
                sqlite.getReadableDatabase(), mock().getTableSchema());
        String expected = "CREATE TABLE IF NOT EXISTS mock_for_create_table (" +
                "_id INTEGER PRIMARY KEY," +
                "item_name TEXT," +
                "item_type TEXT," +
                "category_id INTEGER," +
                "category_name TEXT," +
                "UNIQUE (category_id)," +
                "UNIQUE (item_name,item_type)" +
                ")";
        Assert.assertEquals(expected, createTable.buildQueryAsCreateTableIfNotExists());
    }

    @Test
    public void testBuildUniqueQuery() {
        DatabaseHelper sqlite = sqliteHelper();
        CreateTable createTable = new CreateTable(
                sqlite.getReadableDatabase(), mock().getTableSchema());
        UniqueField sample = new UniqueField(MockTable.Field.CATEGORY_ID);
        Assert.assertEquals("UNIQUE (category_id)", createTable.buildQueryAsUnique(sample));
    }
    
    @Test
    public void testCreateTable() {
        SQLiteDatabase database = sqliteHelper().getWritableDatabase();
        CreateTable createTable = new CreateTable(database, mock().getTableSchema());
        {
            Exception got = null;
            try {
                database.rawQuery("SELECT * FROM mock_for_create_table", new String[]{});
            } catch (SQLiteException e) {
                got = e;
            }
            Assert.assertTrue(got != null);
        }
        database.execSQL(createTable.buildQueryAsCreateTableIfNotExists());
        {
            database.execSQL("SELECT * FROM mock_for_create_table", new String[]{});
            Assert.assertTrue(true);
        }
    }

    @Test
    public void testCreateIndex() {
        TableSchema schema = mock().getTableSchema();
        SQLiteDatabase database = sqliteHelper().getWritableDatabase();
        CreateTable createTable = new CreateTable(database, schema);

        Assert.assertTrue(!indexExists(database, schema));
        createTable.createTableIfNotExists();
        Assert.assertTrue(indexExists(database, schema));
    }
    
    private boolean indexExists(SQLiteDatabase database, TableSchema schema) {
        if (schema.getIndexes().isEmpty()) {
            throw new IllegalStateException("Indexes is empty");
        }
        CreateIndex createIndex = new CreateIndex(database, schema);
        for (Index index : schema.getIndexes()) {
            try {
                String query = createIndex.buildCreateIndexClause(index)
                        .replace("IF NOT EXISTS ", "");
                database.execSQL(query);
            } catch (SQLiteException e) {
                if (!e.getCause().getMessage().contains("already exists]")) {
                    return false;
                }
            }
        }
        return true;
    }

}

class MockTableForCreateTable extends MockTable {
    
    @Override
    public String getTableName() {
        return "mock_for_create_table";
        
    }
    
}




Java Source Code List

net.ichigotake.sqlitehelper.Configuration.java
net.ichigotake.sqlitehelper.DatabaseHelperTest.java
net.ichigotake.sqlitehelper.DatabaseHelper.java
net.ichigotake.sqlitehelper.MigrationCallback.java
net.ichigotake.sqlitehelper.MigrationHelper.java
net.ichigotake.sqlitehelper.MockConfiguration.java
net.ichigotake.sqlitehelper.MockTable.java
net.ichigotake.sqlitehelper.NoMigrationCallback.java
net.ichigotake.sqlitehelper.TableCursor.java
net.ichigotake.sqlitehelper.ddl.AlterTableTest.java
net.ichigotake.sqlitehelper.ddl.AlterTable.java
net.ichigotake.sqlitehelper.ddl.CreateIndexTest.java
net.ichigotake.sqlitehelper.ddl.CreateIndex.java
net.ichigotake.sqlitehelper.ddl.CreateTableTest.java
net.ichigotake.sqlitehelper.ddl.CreateTable.java
net.ichigotake.sqlitehelper.dml.Order.java
net.ichigotake.sqlitehelper.dml.SelectTest.java
net.ichigotake.sqlitehelper.dml.Select.java
net.ichigotake.sqlitehelper.dml.WhereTest.java
net.ichigotake.sqlitehelper.dml.Where.java
net.ichigotake.sqlitehelper.schema.DeletableTable.java
net.ichigotake.sqlitehelper.schema.FieldAttribute.java
net.ichigotake.sqlitehelper.schema.Index.java
net.ichigotake.sqlitehelper.schema.InsertableTable.java
net.ichigotake.sqlitehelper.schema.TableFieldType.java
net.ichigotake.sqlitehelper.schema.TableField.java
net.ichigotake.sqlitehelper.schema.TableSchemaBuilder.java
net.ichigotake.sqlitehelper.schema.TableSchema.java
net.ichigotake.sqlitehelper.schema.Table.java
net.ichigotake.sqlitehelper.schema.UniqueField.java
net.ichigotake.sqlitehelper.schema.UpdatableTable.java