Skip to content

Commit a26d274

Browse files
committed
Test DatabaseHelper
1 parent c0ac93e commit a26d274

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

AndroidBillingLibraryTest/src/net/robotmedia/billing/model/BillingDBTest.java

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package net.robotmedia.billing.model;
1717

1818
import android.database.Cursor;
19+
import android.database.sqlite.SQLiteDatabase;
1920
import android.test.AndroidTestCase;
2021
import android.test.suitebuilder.annotation.SmallTest;
2122

@@ -130,4 +131,71 @@ public void testQueryTransactionsStringPurchaseState() throws Exception {
130131
cursor2.close();
131132
}
132133

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+
}
133201
}

0 commit comments

Comments
 (0)