15
15
import com .onesignal .StaticResetHelper ;
16
16
17
17
import org .json .JSONArray ;
18
+ import org .junit .After ;
18
19
import org .junit .AfterClass ;
19
20
import org .junit .Before ;
20
21
import org .junit .BeforeClass ;
33
34
import static com .test .onesignal .TestHelpers .getAllUniqueOutcomeNotificationRecords ;
34
35
import static junit .framework .Assert .assertEquals ;
35
36
import static junit .framework .Assert .assertFalse ;
37
+ import static junit .framework .Assert .assertTrue ;
36
38
37
39
@ Config (packageName = "com.onesignal.example" ,
38
40
instrumentedPackages = { "com.onesignal" },
@@ -51,14 +53,19 @@ public static void setUpClass() throws Exception {
51
53
StaticResetHelper .saveStaticValues ();
52
54
}
53
55
54
- @ Before // Before each test
56
+ @ Before
55
57
public void beforeEachTest () throws Exception {
56
58
TestHelpers .beforeTestInitAndCleanup ();
57
59
}
58
60
61
+ @ After
62
+ public void afterEachTest () throws Exception {
63
+ TestHelpers .afterTestCleanup ();
64
+ }
65
+
59
66
@ AfterClass
60
67
public static void afterEverything () throws Exception {
61
- StaticResetHelper . restSetStaticFields ();
68
+ TestHelpers . beforeTestInitAndCleanup ();
62
69
}
63
70
64
71
@ Test
@@ -131,13 +138,25 @@ public void shouldUpgradeDbFromV3ToV4() throws Exception {
131
138
assertEquals (outcomeEvents .size (), 1 );
132
139
}
133
140
141
+
142
+ private static final String SQL_CREATE_OUTCOME_REVISION1_ENTRIES =
143
+ "CREATE TABLE outcome (" +
144
+ "_id INTEGER PRIMARY KEY, " +
145
+ "session TEXT," +
146
+ "notification_ids TEXT, " +
147
+ "name TEXT, " +
148
+ "timestamp TIMESTAMP, " +
149
+ "params TEXT " +
150
+ ")" ;
151
+
134
152
@ Test
135
153
public void shouldUpgradeDbFromV4ToV5 () {
136
154
// 1. Init DB as version 4
137
155
ShadowOneSignalDbHelper .DATABASE_VERSION = 4 ;
138
- SQLiteDatabase readableDatabase = OneSignalDbHelper .getInstance (RuntimeEnvironment .application ).getReadableDatabase ();
156
+ SQLiteDatabase writableDatabase = OneSignalDbHelper .getInstance (RuntimeEnvironment .application ).getWritableDatabase ();
157
+ writableDatabase .execSQL (SQL_CREATE_OUTCOME_REVISION1_ENTRIES );
139
158
140
- Cursor cursor = readableDatabase .rawQuery ("SELECT name FROM sqlite_master WHERE type ='table' AND name='" + CachedUniqueOutcomeNotificationTable .TABLE_NAME + "'" , null );
159
+ Cursor cursor = writableDatabase .rawQuery ("SELECT name FROM sqlite_master WHERE type ='table' AND name='" + CachedUniqueOutcomeNotificationTable .TABLE_NAME + "'" , null );
141
160
142
161
boolean exist = false ;
143
162
if (cursor != null ) {
@@ -147,7 +166,7 @@ public void shouldUpgradeDbFromV4ToV5() {
147
166
// 2. Table must not exist
148
167
assertFalse (exist );
149
168
150
- readableDatabase .close ();
169
+ writableDatabase .close ();
151
170
152
171
CachedUniqueOutcomeNotification notification = new CachedUniqueOutcomeNotification ("notificationId" , "outcome" );
153
172
ContentValues values = new ContentValues ();
@@ -163,7 +182,7 @@ public void shouldUpgradeDbFromV4ToV5() {
163
182
164
183
assertEquals (notifications .size (), 0 );
165
184
166
- SQLiteDatabase writableDatabase = OneSignalDbHelper .getInstance (RuntimeEnvironment .application ).getWritableDatabase ();
185
+ writableDatabase = OneSignalDbHelper .getInstance (RuntimeEnvironment .application ).getWritableDatabase ();
167
186
// 5. Table now must exist
168
187
writableDatabase .insert (CachedUniqueOutcomeNotificationTable .TABLE_NAME , null , values );
169
188
writableDatabase .close ();
@@ -172,4 +191,50 @@ public void shouldUpgradeDbFromV4ToV5() {
172
191
173
192
assertEquals (uniqueOutcomeNotifications .size (), 1 );
174
193
}
194
+
195
+ @ Test
196
+ public void shouldUpgradeDbFromV5ToV6 () {
197
+ // 1. Init outcome table as version 5
198
+ SQLiteDatabase writableDatabase = OneSignalDbHelper .getInstance (RuntimeEnvironment .application ).getWritableDatabase ();
199
+
200
+ // Create table with the schema we had in DB v4
201
+ writableDatabase .execSQL (SQL_CREATE_OUTCOME_REVISION1_ENTRIES );
202
+
203
+ // Insert one outcome record so we can test migration keeps it later on
204
+ ContentValues values = new ContentValues ();
205
+ values .put ("name" , "a" );
206
+ writableDatabase .insertOrThrow ("outcome" , null , values );
207
+ writableDatabase .setVersion (5 );
208
+ writableDatabase .close ();
209
+
210
+ // 2. restSetStaticFields so the db reloads and upgrade is done to version 6
211
+ ShadowOneSignalDbHelper .restSetStaticFields ();
212
+ writableDatabase = OneSignalDbHelper .getInstance (RuntimeEnvironment .application ).getWritableDatabase ();
213
+
214
+ // 3. Ensure the upgrade kept our existing record
215
+ Cursor cursor = writableDatabase .query (
216
+ "outcome" ,
217
+ null ,
218
+ null ,
219
+ null ,
220
+ null ,
221
+ null ,
222
+ null
223
+ );
224
+ assertTrue (cursor .moveToFirst ());
225
+ assertEquals ("a" , cursor .getString (cursor .getColumnIndex ("name" )));
226
+
227
+ // 4. Ensure new weight column exists
228
+ values = new ContentValues ();
229
+ values .put ("weight" , 1 );
230
+ long successful = writableDatabase .insert ("outcome" , null , values );
231
+ assertFalse (successful == -1 );
232
+
233
+ // 5. Ensure params column does NOT exists
234
+ values = new ContentValues ();
235
+ values .put ("params" , 1 );
236
+ successful = writableDatabase .insert ("outcome" , null , values );
237
+ writableDatabase .close ();
238
+ assertEquals (-1 , successful );
239
+ }
175
240
}
0 commit comments