Search in sources :

Example 1 with OSOutcomeEventDB

use of com.onesignal.outcomes.OSOutcomeEventDB in project OneSignal-Android-SDK by OneSignal.

the class TestHelpers method getAllOutcomesRecords.

static List<OSOutcomeEventDB> getAllOutcomesRecords(OneSignalDb db) {
    Cursor cursor = db.query(MockOSOutcomeEventsTable.TABLE_NAME, null, null, null, // group by
    null, // filter by row groups
    null, // sort order, new to old
    null, // limit
    null);
    List<OSOutcomeEventDB> events = new ArrayList<>();
    if (cursor.moveToFirst()) {
        do {
            String name = cursor.getString(cursor.getColumnIndex(MockOSOutcomeEventsTable.COLUMN_NAME_NAME));
            String iamIds = cursor.getString(cursor.getColumnIndex(MockOSOutcomeEventsTable.COLUMN_NAME_IAM_IDS));
            String iamInfluenceTypeString = cursor.getString(cursor.getColumnIndex(MockOSOutcomeEventsTable.COLUMN_NAME_IAM_INFLUENCE_TYPE));
            String notificationIds = cursor.getString(cursor.getColumnIndex(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_IDS));
            String notificationInfluenceTypeString = cursor.getString(cursor.getColumnIndex(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_INFLUENCE_TYPE));
            OSInfluenceType iamInfluenceType = OSInfluenceType.fromString(iamInfluenceTypeString);
            OSInfluenceType notificationInfluenceType = OSInfluenceType.fromString(notificationInfluenceTypeString);
            long timestamp = cursor.getLong(cursor.getColumnIndex(MockOSOutcomeEventsTable.COLUMN_NAME_TIMESTAMP));
            float weight = cursor.getFloat(cursor.getColumnIndex(MockOSOutcomeEventsTable.COLUMN_NAME_WEIGHT));
            try {
                OSOutcomeEventDB event = new OSOutcomeEventDB(iamInfluenceType, notificationInfluenceType, new JSONArray(iamIds != null ? iamIds : "[]"), new JSONArray(notificationIds != null ? notificationIds : "[]"), name, timestamp, weight);
                events.add(event);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } while (cursor.moveToNext());
    }
    cursor.close();
    return events;
}
Also used : OSOutcomeEventDB(com.onesignal.outcomes.OSOutcomeEventDB) OSInfluenceType(com.onesignal.influence.domain.OSInfluenceType) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Cursor(android.database.Cursor)

Example 2 with OSOutcomeEventDB

use of com.onesignal.outcomes.OSOutcomeEventDB in project OneSignal-Android-SDK by OneSignal.

the class DatabaseRunner method shouldUpgradeDbFromV3ToV8OutcomeTable.

@Test
public void shouldUpgradeDbFromV3ToV8OutcomeTable() {
    // 1. Init DB as version 3
    ShadowOneSignalDbHelper.DATABASE_VERSION = 3;
    SQLiteDatabase writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    Cursor cursor = writableDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type ='table' AND name='" + MockOSOutcomeEventsTable.TABLE_NAME + "'", null);
    boolean exist = false;
    if (cursor != null) {
        exist = cursor.getCount() > 0;
        cursor.close();
    }
    // 2. Table must not exist
    assertFalse(exist);
    writableDatabase.setVersion(3);
    writableDatabase.close();
    // 3. Clear the cache of the DB so it reloads the file and next getSQLiteDatabaseWithRetries will auto trigger the update
    ShadowOneSignalDbHelper.restSetStaticFields();
    // 4. Opening the DB will auto trigger the update to DB version 8.
    writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    OSOutcomeEventDB outcomeEventDB = new OSOutcomeEventDB(OSInfluenceType.DIRECT, OSInfluenceType.INDIRECT, "iam_id", "notificationId", "outcome_outcome", 1234, (float) 1234);
    ContentValues outcomeValues = new ContentValues();
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_IDS, outcomeEventDB.getNotificationIds().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_IAM_IDS, outcomeEventDB.getIamIds().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_INFLUENCE_TYPE, outcomeEventDB.getNotificationInfluenceType().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_IAM_INFLUENCE_TYPE, outcomeEventDB.getIamInfluenceType().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_NAME, outcomeEventDB.getName());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_WEIGHT, outcomeEventDB.getWeight());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_TIMESTAMP, outcomeEventDB.getTimestamp());
    writableDatabase.insert(MockOSOutcomeEventsTable.TABLE_NAME, null, outcomeValues);
    List<OSOutcomeEventDB> outcomesSaved = getAllOutcomesRecords(dbHelper);
    assertEquals(1, outcomesSaved.size());
    OSOutcomeEventDB outcomeSaved = outcomesSaved.get(0);
    assertEquals(outcomeEventDB.getName(), outcomeSaved.getName());
    assertEquals(outcomeEventDB.getWeight(), outcomeSaved.getWeight());
    assertEquals(outcomeEventDB.getTimestamp(), outcomeSaved.getTimestamp());
    assertEquals(outcomeEventDB.getNotificationIds(), outcomeSaved.getNotificationIds());
    assertEquals(outcomeEventDB.getNotificationInfluenceType(), outcomeSaved.getNotificationInfluenceType());
    assertEquals(outcomeEventDB.getIamIds(), outcomeSaved.getIamIds());
    assertEquals(outcomeEventDB.getIamInfluenceType(), outcomeSaved.getIamInfluenceType());
}
Also used : ContentValues(android.content.ContentValues) OSOutcomeEventDB(com.onesignal.outcomes.OSOutcomeEventDB) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 3 with OSOutcomeEventDB

use of com.onesignal.outcomes.OSOutcomeEventDB in project OneSignal-Android-SDK by OneSignal.

the class DatabaseRunner method shouldUpgradeDbFromV4ToV8OutcomeTable.

@Test
public void shouldUpgradeDbFromV4ToV8OutcomeTable() {
    // 1. Init DB as version 4
    ShadowOneSignalDbHelper.DATABASE_VERSION = 4;
    SQLiteDatabase writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    // Create table with the schema we had in DB v4
    writableDatabase.execSQL(SQL_CREATE_OUTCOME_REVISION1_ENTRIES);
    Cursor cursor = writableDatabase.query(MockOSOutcomeEventsTable.TABLE_NAME, null, null, null, null, null, null);
    boolean exist = false;
    if (cursor != null) {
        String[] columns = cursor.getColumnNames();
        exist = columns.length == 6;
        cursor.close();
    }
    // 2. Table must not exist
    assertTrue(exist);
    writableDatabase.setVersion(4);
    writableDatabase.close();
    // 3. Clear the cache of the DB so it reloads the file and next getSQLiteDatabaseWithRetries will auto trigger the update
    ShadowOneSignalDbHelper.restSetStaticFields();
    // 4. Opening the DB will auto trigger the update to DB version 8.
    writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    OSOutcomeEventDB outcomeEventDB = new OSOutcomeEventDB(OSInfluenceType.DIRECT, OSInfluenceType.INDIRECT, "iam_id", "notificationId", "outcome_outcome", 1234, (float) 1234);
    ContentValues outcomeValues = new ContentValues();
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_IDS, outcomeEventDB.getNotificationIds().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_IAM_IDS, outcomeEventDB.getIamIds().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_INFLUENCE_TYPE, outcomeEventDB.getNotificationInfluenceType().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_IAM_INFLUENCE_TYPE, outcomeEventDB.getIamInfluenceType().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_NAME, outcomeEventDB.getName());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_WEIGHT, outcomeEventDB.getWeight());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_TIMESTAMP, outcomeEventDB.getTimestamp());
    dbHelper.insert(MockOSOutcomeEventsTable.TABLE_NAME, null, outcomeValues);
    List<OSOutcomeEventDB> outcomesSaved = getAllOutcomesRecords(dbHelper);
    assertEquals(1, outcomesSaved.size());
    OSOutcomeEventDB outcomeSaved = outcomesSaved.get(0);
    assertEquals(outcomeEventDB.getName(), outcomeSaved.getName());
    assertEquals(outcomeEventDB.getWeight(), outcomeSaved.getWeight());
    assertEquals(outcomeEventDB.getTimestamp(), outcomeSaved.getTimestamp());
    assertEquals(outcomeEventDB.getNotificationIds(), outcomeSaved.getNotificationIds());
    assertEquals(outcomeEventDB.getNotificationInfluenceType(), outcomeSaved.getNotificationInfluenceType());
    assertEquals(outcomeEventDB.getIamIds(), outcomeSaved.getIamIds());
    assertEquals(outcomeEventDB.getIamInfluenceType(), outcomeSaved.getIamInfluenceType());
}
Also used : ContentValues(android.content.ContentValues) OSOutcomeEventDB(com.onesignal.outcomes.OSOutcomeEventDB) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 4 with OSOutcomeEventDB

use of com.onesignal.outcomes.OSOutcomeEventDB in project OneSignal-Android-SDK by OneSignal.

the class DatabaseRunner method shouldUpgradeDbFromV3ToV4.

@Test
public void shouldUpgradeDbFromV3ToV4() throws Exception {
    // 1. Init DB as version 3
    ShadowOneSignalDbHelper.DATABASE_VERSION = 3;
    SQLiteDatabase writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    Cursor cursor = writableDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type ='table' AND name='" + MockOSOutcomeEventsTable.TABLE_NAME + "'", null);
    boolean exist = false;
    if (cursor != null) {
        exist = cursor.getCount() > 0;
        cursor.close();
    }
    // 2. Table must not exist
    assertFalse(exist);
    writableDatabase.setVersion(3);
    writableDatabase.close();
    OSOutcomeEvent event = new OSOutcomeEvent(OSInfluenceType.UNATTRIBUTED, new JSONArray().put("notificationId"), "name", 0, 0);
    ContentValues values = new ContentValues();
    values.put(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_INFLUENCE_TYPE, event.getSession().toString().toLowerCase());
    values.put(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_IDS, event.getNotificationIds().toString());
    values.put(MockOSOutcomeEventsTable.COLUMN_NAME_NAME, event.getName());
    values.put(MockOSOutcomeEventsTable.COLUMN_NAME_TIMESTAMP, event.getTimestamp());
    values.put(MockOSOutcomeEventsTable.COLUMN_NAME_WEIGHT, event.getWeight());
    // 3. Clear the cache of the DB so it reloads the file.
    ShadowOneSignalDbHelper.restSetStaticFields();
    ShadowOneSignalDbHelper.ignoreDuplicatedFieldsOnUpgrade = true;
    // 4. Opening the DB will auto trigger the update.
    List<OSOutcomeEvent> events = getAllOutcomesRecordsDBv5(dbHelper);
    assertEquals(events.size(), 0);
    writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    // 5. Table now must exist
    writableDatabase.insert(MockOSOutcomeEventsTable.TABLE_NAME, null, values);
    writableDatabase.close();
    List<OSOutcomeEventDB> outcomeEvents = getAllOutcomesRecords(dbHelper);
    assertEquals(outcomeEvents.size(), 1);
}
Also used : ContentValues(android.content.ContentValues) OSOutcomeEventDB(com.onesignal.outcomes.OSOutcomeEventDB) OSOutcomeEvent(com.onesignal.OSOutcomeEvent) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) JSONArray(org.json.JSONArray) Cursor(android.database.Cursor) Test(org.junit.Test)

Example 5 with OSOutcomeEventDB

use of com.onesignal.outcomes.OSOutcomeEventDB in project OneSignal-Android-SDK by OneSignal.

the class DatabaseRunner method shouldUpgradeDbFromV7ToV8OutcomesTable.

@Test
public void shouldUpgradeDbFromV7ToV8OutcomesTable() {
    // 1. Init DB as version 7
    ShadowOneSignalDbHelper.DATABASE_VERSION = 7;
    SQLiteDatabase writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    // Create table with the schema we had in DB v7
    writableDatabase.execSQL(SQL_CREATE_OUTCOME_REVISION2_ENTRIES);
    writableDatabase.execSQL(SQL_CREATE_UNIQUE_OUTCOME_REVISION1_ENTRIES);
    Cursor cursor = writableDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type ='table' AND name='" + MockOSCachedUniqueOutcomeTable.TABLE_NAME_V2 + "'", null);
    boolean exist = false;
    if (cursor != null) {
        exist = cursor.getCount() > 0;
        cursor.close();
    }
    // 2. Table must not exist
    assertFalse(exist);
    // Set data to check that modification on table keep data
    OSOutcomeEventDB outcomeEventDB = new OSOutcomeEventDB(OSInfluenceType.DIRECT, OSInfluenceType.INDIRECT, "iam_id", "notificationId", "outcome_outcome", 1234, (float) 1234);
    ContentValues outcomeValues = new ContentValues();
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_NOTIFICATION_IDS, outcomeEventDB.getNotificationIds().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_SESSION, outcomeEventDB.getNotificationInfluenceType().toString());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_NAME, outcomeEventDB.getName());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_WEIGHT, outcomeEventDB.getWeight());
    outcomeValues.put(MockOSOutcomeEventsTable.COLUMN_NAME_TIMESTAMP, outcomeEventDB.getTimestamp());
    writableDatabase.insert(MockOSOutcomeEventsTable.TABLE_NAME, null, outcomeValues);
    List<OSOutcomeEvent> outcomesSavedBeforeUpdate = getAllOutcomesRecordsDBv5(dbHelper);
    assertEquals(1, outcomesSavedBeforeUpdate.size());
    writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    writableDatabase.setVersion(7);
    writableDatabase.close();
    // 3. Clear the cache of the DB so it reloads the file and next getSQLiteDatabaseWithRetries will auto trigger the update
    ShadowOneSignalDbHelper.restSetStaticFields();
    ShadowOneSignalDbHelper.ignoreDuplicatedFieldsOnUpgrade = true;
    // 4. Opening the DB will auto trigger the update to DB version 8.
    writableDatabase = dbHelper.getSQLiteDatabaseWithRetries();
    List<OSOutcomeEventDB> outcomesSaved = getAllOutcomesRecords(dbHelper);
    assertEquals(1, outcomesSaved.size());
    OSOutcomeEventDB outcomeSaved = outcomesSaved.get(0);
    assertEquals(outcomeEventDB.getName(), outcomeSaved.getName());
    assertEquals(outcomeEventDB.getWeight(), outcomeSaved.getWeight());
    assertEquals(outcomeEventDB.getTimestamp(), outcomeSaved.getTimestamp());
    assertEquals(outcomeEventDB.getNotificationIds(), outcomeSaved.getNotificationIds());
    assertEquals(outcomeEventDB.getNotificationInfluenceType(), outcomeSaved.getNotificationInfluenceType());
    assertEquals(new JSONArray(), outcomeSaved.getIamIds());
    assertEquals(OSInfluenceType.UNATTRIBUTED, outcomeSaved.getIamInfluenceType());
}
Also used : ContentValues(android.content.ContentValues) OSOutcomeEventDB(com.onesignal.outcomes.OSOutcomeEventDB) OSOutcomeEvent(com.onesignal.OSOutcomeEvent) SQLiteDatabase(android.database.sqlite.SQLiteDatabase) JSONArray(org.json.JSONArray) Cursor(android.database.Cursor) Test(org.junit.Test)

Aggregations

Cursor (android.database.Cursor)5 OSOutcomeEventDB (com.onesignal.outcomes.OSOutcomeEventDB)5 ContentValues (android.content.ContentValues)4 SQLiteDatabase (android.database.sqlite.SQLiteDatabase)4 Test (org.junit.Test)4 JSONArray (org.json.JSONArray)3 OSOutcomeEvent (com.onesignal.OSOutcomeEvent)2 OSInfluenceType (com.onesignal.influence.domain.OSInfluenceType)1 ArrayList (java.util.ArrayList)1 JSONException (org.json.JSONException)1