aboutsummaryrefslogtreecommitdiff
path: root/v1/src/test/java/com/xtremelabs/robolectric/shadows/DatabaseTestBase.java
blob: a951774f1702ade2546e44a879d580141e66ed24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
package com.xtremelabs.robolectric.shadows;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.xtremelabs.robolectric.Robolectric;
import com.xtremelabs.robolectric.WithTestDefaultsRunner;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import static com.xtremelabs.robolectric.Robolectric.shadowOf;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.*;


public abstract class DatabaseTestBase {
    protected SQLiteDatabase database;
    protected ShadowSQLiteDatabase shDatabase;

    @Before
    public void setUp() throws Exception {
        database = SQLiteDatabase.openDatabase("path", null, 0);
        shDatabase = Robolectric.shadowOf(database);
        database.execSQL("CREATE TABLE table_name (\n" +
                "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                "  first_column VARCHAR(255),\n" +
                "  second_column BINARY,\n" +
                "  name VARCHAR(255),\n" +
                "  big_int INTEGER\n" +
                ");");

        database.execSQL("CREATE TABLE rawtable (\n" +
                "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                "  first_column VARCHAR(255),\n" +
                "  second_column BINARY,\n" +
                "  name VARCHAR(255),\n" +
                "  big_int INTEGER\n" +
                ");");

        database.execSQL("CREATE TABLE exectable (\n" +
                "  id INTEGER PRIMARY KEY AUTOINCREMENT,\n" +
                "  first_column VARCHAR(255),\n" +
                "  second_column BINARY,\n" +
                "  name VARCHAR(255),\n" +
                "  big_int INTEGER\n" +
                ");");

        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};

        ContentValues values = new ContentValues();

        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);

        database.insert("rawtable", null, values);
        ////////////////////////////////////////////////
        String stringColumnValue2 = "column_value2";
        byte[] byteColumnValue2 = new byte[]{4, 5, 6};
        ContentValues values2 = new ContentValues();

        values2.put("first_column", stringColumnValue2);
        values2.put("second_column", byteColumnValue2);

        database.insert("rawtable", null, values2);
    }


    @After
    public void tearDown() throws Exception {
        database.close();
    }

    @Test()
    public void testInsertAndQuery() throws Exception {
        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};

        ContentValues values = new ContentValues();

        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);

        database.insert("table_name", null, values);

        Cursor cursor = database.query("table_name", new String[]{"second_column", "first_column"}, null, null, null, null, null);

        assertThat(cursor.moveToFirst(), equalTo(true));

        byte[] byteValueFromDatabase = cursor.getBlob(0);
        String stringValueFromDatabase = cursor.getString(1);

        assertThat(stringValueFromDatabase, equalTo(stringColumnValue));
        assertThat(byteValueFromDatabase, equalTo(byteColumnValue));
    }

    @Test
    public void testInsertAndRawQuery() throws Exception {
        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};

        ContentValues values = new ContentValues();

        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);

        database.insert("table_name", null, values);

        Cursor cursor = database.rawQuery("select second_column, first_column from table_name", null);

        assertThat(cursor.moveToFirst(), equalTo(true));

        byte[] byteValueFromDatabase = cursor.getBlob(0);
        String stringValueFromDatabase = cursor.getString(1);

        assertThat(stringValueFromDatabase, equalTo(stringColumnValue));
        assertThat(byteValueFromDatabase, equalTo(byteColumnValue));
    }
    
    @Test(expected = android.database.SQLException.class)
    public void testInsertOrThrowWithSQLException() {
        shDatabase.setThrowOnInsert(true);
        database.insertOrThrow("table_name", null, new ContentValues());
    }
    
    @Test
    public void testInsertOrThrow() {
        String stringColumnValue = "column_value";
        byte[] byteColumnValue = new byte[]{1, 2, 3};
        ContentValues values = new ContentValues();
        values.put("first_column", stringColumnValue);
        values.put("second_column", byteColumnValue);
        database.insertOrThrow("table_name", null, values);
        
        Cursor cursor = database.rawQuery("select second_column, first_column from table_name", null);
        assertThat(cursor.moveToFirst(), equalTo(true));
        byte[] byteValueFromDatabase = cursor.getBlob(0);
        String stringValueFromDatabase = cursor.getString(1);
        assertThat(stringValueFromDatabase, equalTo(stringColumnValue));
        assertThat(byteValueFromDatabase, equalTo(byteColumnValue));
    }

    @Test(expected = IllegalArgumentException.class)
    public void testRawQueryThrowsIndex0NullException() throws Exception {
        database.rawQuery("select second_column, first_column from rawtable WHERE `id` = ?", new String[]{null});
    }

    @Test(expected = IllegalArgumentException.class)
    public void testRawQueryThrowsIndex0NullException2() throws Exception {
        database.rawQuery("select second_column, first_column from rawtable", new String[]{null});
    }

    @Test
    public void testRawQueryCount() throws Exception {
        Cursor cursor = database.rawQuery("select second_column, first_column from rawtable WHERE `id` = ?", new String[]{"1"});
        assertThat(cursor.getCount(), equalTo(1));
    }

    @Test
    public void testRawQueryCount2() throws Exception {
        Cursor cursor = database.rawQuery("select second_column, first_column from rawtable", null);
        assertThat(cursor.getCount(), equalTo(2));
    }

    @Test
    public void testRawQueryCount3() throws Exception {
        Cursor cursor = database.rawQuery("select second_column, first_column from rawtable", new String[]{});
        assertThat(cursor.getCount(), equalTo(2));
    }
    /*
     * Reason why testRawQueryCount4() and testRawQueryCount5() expects exceptions even though exceptions are not found in Android.
     * 
     * The code in Android acts inconsistently under API version 2.1_r1 (and perhaps other APIs)..
     * What happens is that rawQuery() remembers the selectionArgs of previous queries, 
     * and uses them if no selectionArgs are given in subsequent queries. 
     * If they were never given selectionArgs THEN they return empty cursors.
     *
     *
	 * if you run {
	 * 		db.rawQuery("select * from exercise WHERE name = ?",null); //this returns an empty cursor
	 *      db.rawQuery("select * from exercise WHERE name = ?",new String[]{}); //this returns an empty cursor
	 * }
	 *
	 * but if you run {
	 *		db.rawQuery("select * from exercise WHERE name = ?",new String[]{"Leg Press"}); //this returns 1 exercise named "Leg Press"
	 *		db.rawQuery("select * from exercise WHERE name = ?",null); //this too returns 1 exercise named "Leg Press"
	 *		db.rawQuery("select * from exercise WHERE name = ?",new String[]{}); //this too returns 1 exercise named "Leg Press"
	 * }
	 *
	 * so SQLite + Android work inconsistently (it maintains state that it should not)
	 * whereas H2 just throws an exception for not supplying the selectionArgs 
	 *
	 * So the question is should Robolectric:
	 * 1) throw an exception, the way H2 does.
	 * 2) return an empty Cursor.
	 * 3) mimic Android\SQLite precisely and return inconsistent results based on previous state
	 * 
	 * Returning an empty cursor all the time would be bad
	 * because Android doesn't always return an empty cursor.
	 * But just mimicing Android would not be helpful,
	 * since it would be less than obvious where the problem is coming from.
	 * One should just avoid ever calling a statement without selectionArgs (when one has a ? placeholder),
	 * so it is best to throw an Exception to let the programmer know that this isn't going to turn out well if they try to run it under Android.
	 * Because we are running in the context of a test we do not have to mimic Android precisely (if it is more helpful not to!), we just need to help
	 * the testing programmer figure out what is going on.
	 */

    @Test(expected = Exception.class)
    public void testRawQueryCount4() throws Exception {
        //Android and SQLite don't normally throw an exception here. See above explanation as to why Robolectric should.
        Cursor cursor = database.rawQuery("select second_column, first_column from rawtable WHERE `id` = ?", null);
    }

    @Test(expected = Exception.class)
    public void testRawQueryCount5() throws Exception {
        //Android and SQLite don't normally throw an exception here. See above explanation as to why Robolectric should.
        Cursor cursor = database.rawQuery("select second_column, first_column from rawtable WHERE `id` = ?", new String[]{});
    }

    @Test(expected = android.database.sqlite.SQLiteException.class)
    public void testRawQueryCount8() throws Exception {
        Cursor cursor = database.rawQuery("select second_column, first_column from rawtable", new String[]{"1"});
    }

    @Test
    public void testInsertWithException() {
        ContentValues values = new ContentValues();

        assertEquals(-1, database.insert("table_that_doesnt_exist", null, values));
    }


    @Test
    public void testEmptyTable() throws Exception {
        Cursor cursor = database.query("table_name", new String[]{"second_column", "first_column"}, null, null, null, null, null);

        assertThat(cursor.moveToFirst(), equalTo(false));
    }

    @Test
    public void testInsertRowIdGeneration() throws Exception {
        ContentValues values = new ContentValues();
        values.put("name", "Chuck");

        long id = database.insert("table_name", null, values);

        assertThat(id, not(equalTo(0L)));
    }

    @Test
    public void testInsertKeyGeneration() throws Exception {
        ContentValues values = new ContentValues();
        values.put("name", "Chuck");

        long key = database.insertWithOnConflict("table_name", null, values, SQLiteDatabase.CONFLICT_IGNORE);

        assertThat(key, not(equalTo(0L)));
    }

    @Test
    public void testUpdate() throws Exception {
        addChuck();

        assertThat(updateName(1234L, "Buster"), equalTo(1));

        Cursor cursor = database.query("table_name", new String[]{"id", "name"}, null, null, null, null, null);
        assertThat(cursor.moveToFirst(), equalTo(true));
        assertThat(cursor.getCount(), equalTo(1));

        assertIdAndName(cursor, 1234L, "Buster");
    }

    @Test
    public void testUpdateNoMatch() throws Exception {
        addChuck();

        assertThat(updateName(5678L, "Buster"), equalTo(0));

        Cursor cursor = database.query("table_name", new String[]{"id", "name"}, null, null, null, null, null);
        assertThat(cursor.moveToFirst(), equalTo(true));
        assertThat(cursor.getCount(), equalTo(1));

        assertIdAndName(cursor, 1234L, "Chuck");
    }

    @Test
    public void testUpdateAll() throws Exception {
        addChuck();
        addJulie();

        assertThat(updateName("Belvedere"), equalTo(2));

        Cursor cursor = database.query("table_name", new String[]{"id", "name"}, null, null, null, null, null);
        assertThat(cursor.moveToFirst(), equalTo(true));
        assertThat(cursor.getCount(), equalTo(2));

        assertIdAndName(cursor, 1234L, "Belvedere");
        assertThat(cursor.moveToNext(), equalTo(true));

        assertIdAndName(cursor, 1235L, "Belvedere");
        assertThat(cursor.isLast(), equalTo(true));
        assertThat(cursor.moveToNext(), equalTo(false));
        assertThat(cursor.isAfterLast(), equalTo(true));
        assertThat(cursor.moveToNext(), equalTo(false));
    }

    @Test
    public void testDelete() throws Exception {
        addChuck();

        int deleted = database.delete("table_name", "id=1234", null);
        assertThat(deleted, equalTo(1));

        assertEmptyDatabase();
    }

    @Test
    public void testDeleteNoMatch() throws Exception {
        addChuck();

        int deleted = database.delete("table_name", "id=5678", null);
        assertThat(deleted, equalTo(0));

        assertNonEmptyDatabase();
    }

    @Test
    public void testDeleteAll() throws Exception {
        addChuck();
        addJulie();

        int deleted = database.delete("table_name", "1", null);
        assertThat(deleted, equalTo(2));

        assertEmptyDatabase();
    }


    @Test
    public void testExecSQL() throws Exception {
        Statement statement;
        ResultSet resultSet;

        database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');");

        statement = shadowOf(database).getConnection().createStatement();
        resultSet = statement.executeQuery("SELECT COUNT(*) FROM table_name");
        assertThat(resultSet.next(), equalTo(true));
        assertThat(resultSet.getInt(1), equalTo(1));

        statement = shadowOf(database).getConnection().createStatement();
        resultSet = statement.executeQuery("SELECT * FROM table_name");
        assertThat(resultSet.next(), equalTo(true));
        assertThat(resultSet.getInt(1), equalTo(1234));
        assertThat(resultSet.getString(4), equalTo("Chuck"));
    }

    @Test
    public void testExecSQLParams() throws Exception {
        Statement statement;
        ResultSet resultSet;

        database.execSQL("CREATE TABLE `routine` (`id` INTEGER PRIMARY KEY AUTOINCREMENT , `name` VARCHAR , `lastUsed` INTEGER DEFAULT 0 ,  UNIQUE (`name`)) ", new Object[]{});
        database.execSQL("INSERT INTO `routine` (`name` ,`lastUsed` ) VALUES (?,?)", new Object[]{"Leg Press", 0});
        database.execSQL("INSERT INTO `routine` (`name` ,`lastUsed` ) VALUES (?,?)", new Object[]{"Bench Press", 1});

        statement = shadowOf(database).getConnection().createStatement();
        resultSet = statement.executeQuery("SELECT COUNT(*) FROM `routine`");
        assertThat(resultSet.next(), equalTo(true));
        assertThat(resultSet.getInt(1), equalTo(2));

        statement = shadowOf(database).getConnection().createStatement();
        resultSet = statement.executeQuery("SELECT `id`, `name` ,`lastUsed` FROM `routine`");
        assertThat(resultSet.next(), equalTo(true));
        assertThat(resultSet.getInt(1), equalTo(1));
        assertThat(resultSet.getString(2), equalTo("Leg Press"));
        assertThat(resultSet.getInt(3), equalTo(0));
        assertThat(resultSet.next(), equalTo(true));
        assertThat(resultSet.getLong(1), equalTo(2L));
        assertThat(resultSet.getString(2), equalTo("Bench Press"));
        assertThat(resultSet.getInt(3), equalTo(1));
    }

    @Test(expected = android.database.SQLException.class)
    public void testExecSQLException() throws Exception {
        database.execSQL("INSERT INTO table_name;");    // invalid SQL
    }

    @Test(expected = IllegalArgumentException.class)
    public void testExecSQLException2() throws Exception {
        database.execSQL("insert into exectable (first_column) values (?);", null);
    }

    @Test(expected = IllegalArgumentException.class)
    public void testExecSQLException4() throws Exception {
        database.execSQL("insert into exectable (first_column) values ('sdfsfs');", null);
    }

    @Test(expected = Exception.class)
    public void testExecSQLException5() throws Exception {
        //TODO: make this throw android.database.SQLException.class
        database.execSQL("insert into exectable (first_column) values ('kjhk');", new String[]{"xxxx"});
    }

    @Test(expected = Exception.class)
    public void testExecSQLException6() throws Exception {
        //TODO: make this throw android.database.SQLException.class
        database.execSQL("insert into exectable (first_column) values ('kdfd');", new String[]{null});
    }

    @Test
    public void testExecSQL2() throws Exception {
        database.execSQL("insert into exectable (first_column) values ('eff');", new String[]{});
    }

    @Test
    public void testExecSQLInsertNull() throws Exception {
        String name = "nullone";

        database.execSQL("insert into exectable (first_column, name) values (?,?);", new String[]{null, name});

        Cursor cursor = database.rawQuery("select * from exectable WHERE `name` = ?", new String[]{name});
        cursor.moveToFirst();
        int firstIndex = cursor.getColumnIndex("first_column");
        int nameIndex = cursor.getColumnIndex("name");
        assertThat(cursor.getString(nameIndex), equalTo(name));
        assertThat(cursor.getString(firstIndex), equalTo(null));

    }

    @Test(expected = Exception.class)
    public void testExecSQLInsertNullShouldBeException() throws Exception {
        //this inserts null in android, but it when it happens it is likely an error.  H2 throws an exception.  So we'll make Robolectric expect an Exception so that the error can be found.

        database.delete("exectable", null, null);

        Cursor cursor = database.rawQuery("select * from exectable", null);
        cursor.moveToFirst();
        assertThat(cursor.getCount(), equalTo(0));

        database.execSQL("insert into exectable (first_column) values (?);", new String[]{});
        Cursor cursor2 = database.rawQuery("select * from exectable", new String[]{null});
        cursor.moveToFirst();
        assertThat(cursor2.getCount(), equalTo(1));

    }

    @Test
    public void testExecSQLAutoIncrementSQLite() throws Exception {
        database.execSQL("CREATE TABLE auto_table (id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(255));");

        ContentValues values = new ContentValues();
        values.put("name", "Chuck");

        long key = database.insert("auto_table", null, values);
        assertThat(key, not(equalTo(0L)));

        long key2 = database.insert("auto_table", null, values);
        assertThat(key2, not(equalTo(key)));
    }

    @Test(expected = IllegalStateException.class)
    public void testClose() throws Exception {
        database.close();

        database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');");
    }

    @Test
    public void testIsOpen() throws Exception {
        assertThat(database.isOpen(), equalTo(true));
        database.close();
        assertThat(database.isOpen(), equalTo(false));
    }

    @Test
    public void shouldStoreGreatBigHonkinIntegersCorrectly() throws Exception {
        database.execSQL("INSERT INTO table_name(big_int) VALUES(1234567890123456789);");
        Cursor cursor = database.query("table_name", new String[]{"big_int"}, null, null, null, null, null);
        cursor.moveToFirst();
        assertEquals(1234567890123456789L, cursor.getLong(0));
    }

    @Test
    public void testSuccessTransaction() throws SQLException {
        assertThat(shDatabase.isTransactionSuccess(), equalTo(false));
        database.beginTransaction();
        assertThat(shDatabase.isTransactionSuccess(), equalTo(false));
        database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');");
        assertThat(shDatabase.isTransactionSuccess(), equalTo(false));
        database.setTransactionSuccessful();
        assertThat(shDatabase.isTransactionSuccess(), equalTo(true));
        database.endTransaction();
        assertThat(shDatabase.isTransactionSuccess(), equalTo(false));

        Statement statement = shadowOf(database).getConnection().createStatement();
        ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM table_name");
        assertThat(resultSet.next(), equalTo(true));
        assertThat(resultSet.getInt(1), equalTo(1));
    }

    @Test
    public void testFailureTransaction() throws Exception {
        assertThat(shDatabase.isTransactionSuccess(), equalTo(false));
        database.beginTransaction();
        assertThat(shDatabase.isTransactionSuccess(), equalTo(false));

        database.execSQL("INSERT INTO table_name (id, name) VALUES(1234, 'Chuck');");

        Statement statement = shadowOf(database).getConnection().createStatement();
        final String select = "SELECT COUNT(*) FROM table_name";

        ResultSet rs = statement.executeQuery(select);
        assertThat(rs.next(), equalTo(true));
        assertThat(rs.getInt(1), equalTo(1));
        rs.close();

        assertThat(shDatabase.isTransactionSuccess(), equalTo(false));
        database.endTransaction();

        statement = shadowOf(database).getConnection().createStatement();
        rs = statement.executeQuery(select);
        assertThat(rs.next(), equalTo(true));
        assertThat(rs.getInt(1), equalTo(0));

        assertThat(shDatabase.isTransactionSuccess(), equalTo(false));
    }

    @Test
    public void testTransactionAlreadySuccessful() {
        database.beginTransaction();
        database.setTransactionSuccessful();
        try {
            database.setTransactionSuccessful();
            fail("didn't receive the expected IllegalStateException");
        } catch (IllegalStateException e) {
            assertThat(e.getMessage(), equalTo("transaction already successfully"));
        }
    }
    
    @Test
    public void testInTransaction() throws Exception {
    	assertThat( database.inTransaction(), equalTo(false) );
    	database.beginTransaction();
    	assertThat( database.inTransaction(), equalTo(true) );
    	database.endTransaction();
    	assertThat( database.inTransaction(), equalTo(false) );    	
    }

    protected long addChuck() {
        return addPerson(1234L, "Chuck");
    }

    protected long addJulie() {
        return addPerson(1235L, "Julie");
    }

    protected long addPerson(long id, String name) {
        ContentValues values = new ContentValues();
        values.put("id", id);
        values.put("name", name);
        return database.insert("table_name", null, values);
    }

    protected int updateName(long id, String name) {
        ContentValues values = new ContentValues();
        values.put("name", name);
        return database.update("table_name", values, "id=" + id, null);
    }

    protected int updateName(String name) {
        ContentValues values = new ContentValues();
        values.put("name", name);
        return database.update("table_name", values, null, null);
    }

    protected void assertIdAndName(Cursor cursor, long id, String name) {
        long idValueFromDatabase;
        String stringValueFromDatabase;

        idValueFromDatabase = cursor.getLong(0);
        stringValueFromDatabase = cursor.getString(1);
        assertThat(idValueFromDatabase, equalTo(id));
        assertThat(stringValueFromDatabase, equalTo(name));
    }

    protected void assertEmptyDatabase() {
        Cursor cursor = database.query("table_name", new String[]{"id", "name"}, null, null, null, null, null);
        assertThat(cursor.moveToFirst(), equalTo(false));
        assertThat(cursor.isClosed(), equalTo(false));
        assertThat(cursor.getCount(), equalTo(0));
    }

    protected void assertNonEmptyDatabase() {
        Cursor cursor = database.query("table_name", new String[]{"id", "name"}, null, null, null, null, null);
        assertThat(cursor.moveToFirst(), equalTo(true));
        assertThat(cursor.getCount(), not(equalTo(0)));
    }
}