Home | History | Annotate | Download | only in shadows
      1 package org.robolectric.shadows;
      2 
      3 import static org.assertj.core.api.Assertions.assertThat;
      4 
      5 import android.content.ContentValues;
      6 import android.database.Cursor;
      7 import android.database.sqlite.SQLiteDatabase;
      8 import android.database.sqlite.SQLiteQueryBuilder;
      9 import org.junit.After;
     10 import org.junit.Before;
     11 import org.junit.Test;
     12 import org.junit.runner.RunWith;
     13 import org.robolectric.RobolectricTestRunner;
     14 
     15 @RunWith(RobolectricTestRunner.class)
     16 public class SQLiteQueryBuilderTest {
     17 
     18   private static final String TABLE_NAME = "sqlBuilderTest";
     19   private static final String COL_VALUE = "valueCol";
     20   private static final String COL_GROUP = "groupCol";
     21 
     22   private SQLiteDatabase database;
     23   private SQLiteQueryBuilder builder;
     24 
     25   private long firstRecordId;
     26 
     27   @Before
     28   public void setUp() throws Exception {
     29     database = SQLiteDatabase.create(null);
     30 
     31     database.execSQL("create table " + TABLE_NAME + " ("
     32         + COL_VALUE + " TEXT, "
     33         + COL_GROUP + " INTEGER"
     34         + ")");
     35 
     36     ContentValues values = new ContentValues();
     37     values.put(COL_VALUE, "record1");
     38     values.put(COL_GROUP, 1);
     39     firstRecordId = database.insert(TABLE_NAME, null, values);
     40     assertThat(firstRecordId).isGreaterThan(0);
     41 
     42     values.clear();
     43     values.put(COL_VALUE, "record2");
     44     values.put(COL_GROUP, 1);
     45     long secondRecordId = database.insert(TABLE_NAME, null, values);
     46     assertThat(secondRecordId).isGreaterThan(0).isNotEqualTo(firstRecordId);
     47 
     48     values.clear();
     49     values.put(COL_VALUE, "won't be selected");
     50     values.put(COL_GROUP, 2);
     51     database.insert(TABLE_NAME, null, values);
     52 
     53     builder = new SQLiteQueryBuilder();
     54     builder.setTables(TABLE_NAME);
     55     builder.appendWhere(COL_VALUE + " <> ");
     56     builder.appendWhereEscapeString("won't be selected");
     57   }
     58 
     59   @After
     60   public void tearDown() {
     61     database.close();
     62   }
     63 
     64   @Test
     65   public void shouldBeAbleToMakeQueries() {
     66     Cursor cursor = builder.query(database, new String[] {"rowid"}, null, null, null, null, null);
     67     assertThat(cursor.getCount()).isEqualTo(2);
     68   }
     69 
     70   @Test
     71   public void shouldBeAbleToMakeQueriesWithSelection() {
     72     Cursor cursor = builder.query(database, new String[] {"rowid"}, COL_VALUE + "=?", new String[] {"record1"}, null, null, null);
     73     assertThat(cursor.getCount()).isEqualTo(1);
     74     assertThat(cursor.moveToNext()).isTrue();
     75     assertThat(cursor.getLong(0)).isEqualTo(firstRecordId);
     76   }
     77 
     78   @Test
     79   public void shouldBeAbleToMakeQueriesWithGrouping() {
     80     Cursor cursor = builder.query(database, new String[] {"rowid"}, null, null, COL_GROUP, null, null);
     81     assertThat(cursor.getCount()).isEqualTo(1);
     82   }
     83 
     84 }
     85