Wed, 31 Dec 2014 07:22:50 +0100
Correct previous dual key logic pending first delivery installment.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | package org.mozilla.gecko.background.healthreport; |
michael@0 | 5 | |
michael@0 | 6 | import org.mozilla.gecko.background.helpers.DBHelpers; |
michael@0 | 7 | import org.mozilla.gecko.background.helpers.FakeProfileTestCase; |
michael@0 | 8 | |
michael@0 | 9 | import android.content.ContentValues; |
michael@0 | 10 | import android.database.Cursor; |
michael@0 | 11 | import android.database.sqlite.SQLiteDatabase; |
michael@0 | 12 | import android.database.sqlite.SQLiteException; |
michael@0 | 13 | |
michael@0 | 14 | public class TestHealthReportSQLiteOpenHelper extends FakeProfileTestCase { |
michael@0 | 15 | private MockHealthReportSQLiteOpenHelper helper; |
michael@0 | 16 | |
michael@0 | 17 | @Override |
michael@0 | 18 | protected void setUp() throws Exception { |
michael@0 | 19 | super.setUp(); |
michael@0 | 20 | helper = null; |
michael@0 | 21 | } |
michael@0 | 22 | |
michael@0 | 23 | @Override |
michael@0 | 24 | protected void tearDown() throws Exception { |
michael@0 | 25 | if (helper != null) { |
michael@0 | 26 | helper.close(); |
michael@0 | 27 | helper = null; |
michael@0 | 28 | } |
michael@0 | 29 | super.tearDown(); |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | private MockHealthReportSQLiteOpenHelper createHelper(String name) { |
michael@0 | 33 | return new MockHealthReportSQLiteOpenHelper(context, fakeProfileDirectory, name); |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | private MockHealthReportSQLiteOpenHelper createHelper(String name, int version) { |
michael@0 | 37 | return new MockHealthReportSQLiteOpenHelper(context, fakeProfileDirectory, name, version); |
michael@0 | 38 | } |
michael@0 | 39 | |
michael@0 | 40 | public void testOpening() { |
michael@0 | 41 | helper = createHelper("health.db"); |
michael@0 | 42 | SQLiteDatabase db = helper.getWritableDatabase(); |
michael@0 | 43 | assertTrue(db.isOpen()); |
michael@0 | 44 | db.beginTransaction(); |
michael@0 | 45 | db.setTransactionSuccessful(); |
michael@0 | 46 | db.endTransaction(); |
michael@0 | 47 | helper.close(); |
michael@0 | 48 | assertFalse(db.isOpen()); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | private void assertEmptyTable(SQLiteDatabase db, String table, String column) { |
michael@0 | 52 | Cursor c = db.query(table, new String[] { column }, |
michael@0 | 53 | null, null, null, null, null); |
michael@0 | 54 | assertNotNull(c); |
michael@0 | 55 | try { |
michael@0 | 56 | assertFalse(c.moveToFirst()); |
michael@0 | 57 | } finally { |
michael@0 | 58 | c.close(); |
michael@0 | 59 | } |
michael@0 | 60 | } |
michael@0 | 61 | |
michael@0 | 62 | public void testInit() { |
michael@0 | 63 | helper = createHelper("health-" + System.currentTimeMillis() + ".db"); |
michael@0 | 64 | SQLiteDatabase db = helper.getWritableDatabase(); |
michael@0 | 65 | assertTrue(db.isOpen()); |
michael@0 | 66 | |
michael@0 | 67 | db.beginTransaction(); |
michael@0 | 68 | try { |
michael@0 | 69 | // DB starts empty with correct tables. |
michael@0 | 70 | assertEmptyTable(db, "fields", "name"); |
michael@0 | 71 | assertEmptyTable(db, "measurements", "name"); |
michael@0 | 72 | assertEmptyTable(db, "events_textual", "field"); |
michael@0 | 73 | assertEmptyTable(db, "events_integer", "field"); |
michael@0 | 74 | assertEmptyTable(db, "events", "field"); |
michael@0 | 75 | |
michael@0 | 76 | // Throws for tables that don't exist. |
michael@0 | 77 | try { |
michael@0 | 78 | assertEmptyTable(db, "foobarbaz", "name"); |
michael@0 | 79 | } catch (SQLiteException e) { |
michael@0 | 80 | // Expected. |
michael@0 | 81 | } |
michael@0 | 82 | db.setTransactionSuccessful(); |
michael@0 | 83 | } finally { |
michael@0 | 84 | db.endTransaction(); |
michael@0 | 85 | } |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | public void testUpgradeDatabaseFrom4To5() throws Exception { |
michael@0 | 89 | final String dbName = "health-4To5.db"; |
michael@0 | 90 | helper = createHelper(dbName, 4); |
michael@0 | 91 | SQLiteDatabase db = helper.getWritableDatabase(); |
michael@0 | 92 | db.beginTransaction(); |
michael@0 | 93 | try { |
michael@0 | 94 | db.execSQL("PRAGMA foreign_keys=OFF;"); |
michael@0 | 95 | |
michael@0 | 96 | // Despite being referenced, this addon should be deleted because it is NULL. |
michael@0 | 97 | ContentValues v = new ContentValues(); |
michael@0 | 98 | v.put("body", (String) null); |
michael@0 | 99 | final long orphanedAddonID = db.insert("addons", null, v); |
michael@0 | 100 | v.put("body", "addon"); |
michael@0 | 101 | final long addonID = db.insert("addons", null, v); |
michael@0 | 102 | |
michael@0 | 103 | // environments -> addons |
michael@0 | 104 | v = new ContentValues(); |
michael@0 | 105 | v.put("hash", "orphanedEnv"); |
michael@0 | 106 | v.put("addonsID", orphanedAddonID); |
michael@0 | 107 | final long orphanedEnvID = db.insert("environments", null, v); |
michael@0 | 108 | v.put("hash", "env"); |
michael@0 | 109 | v.put("addonsID", addonID); |
michael@0 | 110 | final long envID = db.insert("environments", null, v); |
michael@0 | 111 | |
michael@0 | 112 | v = new ContentValues(); |
michael@0 | 113 | v.put("name", "measurement"); |
michael@0 | 114 | v.put("version", 1); |
michael@0 | 115 | final long measurementID = db.insert("measurements", null, v); |
michael@0 | 116 | |
michael@0 | 117 | // fields -> measurements |
michael@0 | 118 | v = new ContentValues(); |
michael@0 | 119 | v.put("name", "orphanedField"); |
michael@0 | 120 | v.put("measurement", DBHelpers.getNonExistentID(db, "measurements")); |
michael@0 | 121 | final long orphanedFieldID = db.insert("fields", null, v); |
michael@0 | 122 | v.put("name", "field"); |
michael@0 | 123 | v.put("measurement", measurementID); |
michael@0 | 124 | final long fieldID = db.insert("fields", null, v); |
michael@0 | 125 | |
michael@0 | 126 | // events -> environments, fields |
michael@0 | 127 | final String[] eventTables = {"events_integer", "events_textual"}; |
michael@0 | 128 | for (String table : eventTables) { |
michael@0 | 129 | v = new ContentValues(); |
michael@0 | 130 | v.put("env", envID); |
michael@0 | 131 | v.put("field", fieldID); |
michael@0 | 132 | db.insert(table, null, v); |
michael@0 | 133 | |
michael@0 | 134 | v.put("env", orphanedEnvID); |
michael@0 | 135 | v.put("field", fieldID); |
michael@0 | 136 | db.insert(table, null, v); |
michael@0 | 137 | |
michael@0 | 138 | v.put("env", envID); |
michael@0 | 139 | v.put("field", orphanedFieldID); |
michael@0 | 140 | db.insert(table, null, v); |
michael@0 | 141 | |
michael@0 | 142 | v.put("env", orphanedEnvID); |
michael@0 | 143 | v.put("field", orphanedFieldID); |
michael@0 | 144 | db.insert(table, null, v); |
michael@0 | 145 | } |
michael@0 | 146 | |
michael@0 | 147 | db.setTransactionSuccessful(); |
michael@0 | 148 | } finally { |
michael@0 | 149 | db.endTransaction(); |
michael@0 | 150 | helper.close(); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | // Upgrade. |
michael@0 | 154 | helper = createHelper(dbName, 5); |
michael@0 | 155 | // Despite only reading from it, open a writable database so we can better replicate what |
michael@0 | 156 | // might happen in production (most notably, this should enable foreign keys). |
michael@0 | 157 | db = helper.getWritableDatabase(); |
michael@0 | 158 | |
michael@0 | 159 | assertEquals(1, DBHelpers.getRowCount(db, "addons")); |
michael@0 | 160 | assertEquals(1, DBHelpers.getRowCount(db, "measurements")); |
michael@0 | 161 | assertEquals(1, DBHelpers.getRowCount(db, "fields")); |
michael@0 | 162 | assertEquals(1, DBHelpers.getRowCount(db, "events_integer")); |
michael@0 | 163 | assertEquals(1, DBHelpers.getRowCount(db, "events_textual")); |
michael@0 | 164 | } |
michael@0 | 165 | } |