|
16 | 16 | package net.robotmedia.billing.model;
|
17 | 17 |
|
18 | 18 | import android.database.Cursor;
|
| 19 | +import android.database.sqlite.SQLiteDatabase; |
19 | 20 | import android.test.AndroidTestCase;
|
20 | 21 | import android.test.suitebuilder.annotation.SmallTest;
|
21 | 22 |
|
@@ -130,4 +131,71 @@ public void testQueryTransactionsStringPurchaseState() throws Exception {
|
130 | 131 | cursor2.close();
|
131 | 132 | }
|
132 | 133 |
|
| 134 | + @SmallTest |
| 135 | + public void testDatabaseHelperConstructor() throws Exception { |
| 136 | + BillingDB.DatabaseHelper helper = new BillingDB.DatabaseHelper(this.getContext()); |
| 137 | + assertEquals(helper.getReadableDatabase().getVersion(), BillingDB.DATABASE_VERSION); |
| 138 | + } |
| 139 | + |
| 140 | + private void testColumn(SQLiteDatabase db, String column, String expectedType, boolean result) { |
| 141 | + Cursor cursor = db.rawQuery("PRAGMA table_info(" + BillingDB.TABLE_TRANSACTIONS + ")", null); |
| 142 | + boolean found = false; |
| 143 | + while (cursor.moveToNext()) { |
| 144 | + final String name = cursor.getString(1); |
| 145 | + if (name.equals(column)) { |
| 146 | + final String type = cursor.getString(2); |
| 147 | + assertEquals(type, expectedType); |
| 148 | + found = true; |
| 149 | + } |
| 150 | + } |
| 151 | + cursor.close(); |
| 152 | + assertEquals(found, result); |
| 153 | + } |
| 154 | + |
| 155 | + @SmallTest |
| 156 | + public void testDatabaseHelperOnCreate() throws Exception { |
| 157 | + BillingDB.DatabaseHelper helper = new BillingDB.DatabaseHelper(this.getContext()); |
| 158 | + SQLiteDatabase db = SQLiteDatabase.create(null); |
| 159 | + helper.onCreate(db); |
| 160 | + Cursor cursor = db.query("sqlite_master", new String[] {"name"}, "type='table' AND name='" + BillingDB.TABLE_TRANSACTIONS + "'", null, null, null, null); |
| 161 | + assertTrue(cursor.getCount() > 0); |
| 162 | + cursor.close(); |
| 163 | + testColumn(db, BillingDB.COLUMN__ID, "TEXT", true); |
| 164 | + testColumn(db, BillingDB.COLUMN_PRODUCT_ID, "TEXT", true); |
| 165 | + testColumn(db, BillingDB.COLUMN_STATE, "TEXT", true); |
| 166 | + testColumn(db, BillingDB.COLUMN_PURCHASE_TIME, "TEXT", true); |
| 167 | + testColumn(db, BillingDB.COLUMN_DEVELOPER_PAYLOAD, "TEXT", true); |
| 168 | + testColumn(db, BillingDB.COLUMN_SIGNED_DATA, "TEXT", true); |
| 169 | + testColumn(db, BillingDB.COLUMN_SIGNATURE, "TEXT", true); |
| 170 | + } |
| 171 | + |
| 172 | + @SmallTest |
| 173 | + public void testDatabaseHelperOnUpgradeCurrentVersion() throws Exception { |
| 174 | + BillingDB.DatabaseHelper helper = new BillingDB.DatabaseHelper(this.getContext()); |
| 175 | + SQLiteDatabase db = helper.getWritableDatabase(); |
| 176 | + helper.onUpgrade(db, BillingDB.DATABASE_VERSION, BillingDB.DATABASE_VERSION); |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | + private SQLiteDatabase createVersion1Database() { |
| 181 | + final SQLiteDatabase db = SQLiteDatabase.create(null); |
| 182 | + db.execSQL("CREATE TABLE " + BillingDB.TABLE_TRANSACTIONS + "(" + |
| 183 | + BillingDB.COLUMN__ID + " TEXT PRIMARY KEY, " + |
| 184 | + BillingDB.COLUMN_PRODUCT_ID + " INTEGER, " + |
| 185 | + BillingDB.COLUMN_STATE + " TEXT, " + |
| 186 | + BillingDB.COLUMN_PURCHASE_TIME + " TEXT, " + |
| 187 | + BillingDB.COLUMN_DEVELOPER_PAYLOAD + " INTEGER)"); |
| 188 | + return db; |
| 189 | + } |
| 190 | + |
| 191 | + @SmallTest |
| 192 | + public void testDatabaseHelperOnUpgradeVersion1To2() throws Exception { |
| 193 | + BillingDB.DatabaseHelper helper = new BillingDB.DatabaseHelper(this.getContext()); |
| 194 | + SQLiteDatabase db = createVersion1Database(); |
| 195 | + testColumn(db, BillingDB.COLUMN_SIGNED_DATA, "TEXT", false); |
| 196 | + testColumn(db, BillingDB.COLUMN_SIGNATURE, "TEXT", false); |
| 197 | + helper.onUpgrade(db, 1, 2); |
| 198 | + testColumn(db, BillingDB.COLUMN_SIGNED_DATA, "TEXT", true); |
| 199 | + testColumn(db, BillingDB.COLUMN_SIGNATURE, "TEXT", true); |
| 200 | + } |
133 | 201 | }
|
0 commit comments