Skip to content

Commit

Permalink
test: add new db unit test case and fix old one
Browse files Browse the repository at this point in the history
  • Loading branch information
1abhishekpandey committed Jul 30, 2023
1 parent 7e7deec commit c6b5419
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 6 deletions.
1 change: 1 addition & 0 deletions Sources/Classes/Headers/Public/RSDBMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
@property (nonatomic) NSMutableArray<NSString *>* messages;
@property (nonatomic) NSMutableArray<NSString *>* messageIds;
@property (nonatomic) NSMutableArray<NSNumber *>* statusList;
@property (nonatomic) NSMutableArray<NSNumber *>* dmProcessed;
@end

NS_ASSUME_NONNULL_END
4 changes: 4 additions & 0 deletions Sources/Classes/RSDBPersistentManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ - (RSDBMessage *) getEventsFromDB :(NSString*) querySQLString {
NSMutableArray<NSString *> *messageIds = [[NSMutableArray alloc] init];
NSMutableArray<NSString *> *messages = [[NSMutableArray alloc] init];
NSMutableArray<NSNumber *>* statusList = [[NSMutableArray alloc] init];
NSMutableArray<NSNumber *>* dmProcessedList = [[NSMutableArray alloc] init];

@synchronized (self) {
sqlite3_stmt *queryStmt = nil;
Expand All @@ -242,9 +243,11 @@ - (RSDBMessage *) getEventsFromDB :(NSString*) querySQLString {
const unsigned char* queryResultCol1 = sqlite3_column_text(queryStmt, 1);
NSString *message = [[NSString alloc] initWithUTF8String:(char *)queryResultCol1];
int status = sqlite3_column_int(queryStmt,3);
int dmProcessed = sqlite3_column_int(queryStmt, 4);
[messageIds addObject:[[NSString alloc] initWithFormat:@"%d", messageId]];
[messages addObject:message];
[statusList addObject:[NSNumber numberWithInt:status]];
[dmProcessedList addObject:[NSNumber numberWithInt:dmProcessed]];
}
} else {
[RSLogger logError:@"RSDBPersistentManager: getEventsFromDB: Failed to fetch events from DB"];
Expand All @@ -255,6 +258,7 @@ - (RSDBMessage *) getEventsFromDB :(NSString*) querySQLString {
dbMessage.messageIds = messageIds;
dbMessage.messages = messages;
dbMessage.statusList = statusList;
dbMessage.dmProcessed = dmProcessedList;
return dbMessage;
}

Expand Down
127 changes: 121 additions & 6 deletions Tests/DBPersistentManagerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ class DBPersistentManagerTests: XCTestCase {
let ROWID_2 = 2 as NSNumber
let ROWID_3 = 3 as NSNumber

let COL_STATUS = "status";
let COL_DM_PROCESSED = "dm_processed";

override func setUpWithError() throws {
dbPersistentManager = RSDBPersistentManager()
deleteEventsTable()
Expand All @@ -54,7 +57,7 @@ class DBPersistentManagerTests: XCTestCase {
deleteEventsToDestinationIdMappingTable()
}

func testMigration() throws {
func testMigrationFromV1ToV2() throws {
// creating events table with version 1 i.e prior to migration
dbPersistentManager.createEventsTable(withVersion:1)
// inserting message1 into the table prior to migration
Expand All @@ -64,12 +67,12 @@ class DBPersistentManagerTests: XCTestCase {
// checking if both the messages got saved to the db successfully prior to migration
XCTAssert(dbPersistentManager.fetchAllEventsFromDB(forMode: ALL).messageIds.count==2)
// verifying that the status column is missing from the events table
XCTAssert(!dbPersistentManager.checkIfStatusColumnExists())
dbPersistentManager.performMigration()
XCTAssert(!dbPersistentManager.checkIfColumnExists(COL_STATUS))
dbPersistentManager.performMigration(COL_STATUS)
// inserting message3 into the table after the migration
dbPersistentManager.saveEvent(MESSAGE_3)
// verifying if the status column exists in the event table after migration
XCTAssert(dbPersistentManager.checkIfStatusColumnExists())
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_STATUS))
// verifying if both the messages exist in the table even after migration
let rsDbMessage: RSDBMessage = dbPersistentManager.fetchAllEventsFromDB(forMode: ALL)
XCTAssert(rsDbMessage.messageIds.count==3)
Expand All @@ -78,15 +81,85 @@ class DBPersistentManagerTests: XCTestCase {
XCTAssert(rsDbMessage.statusList[2] as? Int == 0)
}

func testMigrationFromV1ToV3() throws {
// creating events table with version 1 i.e prior to migration
dbPersistentManager.createEventsTable(withVersion:1)
// inserting message1 into the table prior to migration
dbPersistentManager.saveEvent(MESSAGE_1)
// inserting message2 into the table prior to migration
dbPersistentManager.saveEvent(MESSAGE_2)
// checking if both the messages got saved to the db successfully prior to migration
XCTAssert(dbPersistentManager.fetchAllEventsFromDB(forMode: ALL).messageIds.count==2)
// verifying that the status column is missing from the events table
XCTAssert(!dbPersistentManager.checkIfColumnExists(COL_STATUS))
dbPersistentManager.performMigration(COL_STATUS)
// verifying that the dm_processed column is missing from the events table
XCTAssert(!dbPersistentManager.checkIfColumnExists(COL_DM_PROCESSED))
dbPersistentManager.performMigration(COL_DM_PROCESSED)
// inserting message3 into the table after the migration
dbPersistentManager.saveEvent(MESSAGE_3)
// verifying if the status column exists in the event table after migration
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_STATUS))
// verifying if the dm_processed column exists in the event table after migration
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_DM_PROCESSED))
// verifying if events made prior to migration their status is marked as DEVICE_MODE_PROCESSING_DONE. For new messages it should be 0
let rsDbMessage: RSDBMessage = dbPersistentManager.fetchAllEventsFromDB(forMode: ALL)
XCTAssert(rsDbMessage.messageIds.count==3)
XCTAssert(rsDbMessage.statusList[0] as? Int == 1)
XCTAssert(rsDbMessage.statusList[1] as? Int == 1)
XCTAssert(rsDbMessage.statusList[2] as? Int == 0)
// verifying if events made prior to migration their dm_processed is marked as DM_PROCESSED_DONE. For new messages it should be 0
XCTAssert(rsDbMessage.dmProcessed[0] as? Int == 1)
XCTAssert(rsDbMessage.dmProcessed[1] as? Int == 1)
XCTAssert(rsDbMessage.dmProcessed[2] as? Int == 0)
}

func testMigrationFromV2ToV3() throws {
// creating events table with version 2 i.e prior to migration
dbPersistentManager.createEventsTable(withVersion:2)
// inserting message1 into the table prior to migration
dbPersistentManager.saveEvent(MESSAGE_1)
// inserting message2 into the table prior to migration
dbPersistentManager.saveEvent(MESSAGE_2)
// checking if both the messages got saved to the db successfully prior to migration
XCTAssert(dbPersistentManager.fetchAllEventsFromDB(forMode: ALL).messageIds.count==2)
// verifying that the status column exist in the events table
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_STATUS))
// verifying that the dm_processed column is missing from the events table
XCTAssert(!dbPersistentManager.checkIfColumnExists(COL_DM_PROCESSED))
dbPersistentManager.performMigration(COL_DM_PROCESSED)
// inserting message3 into the table after the migration
dbPersistentManager.saveEvent(MESSAGE_3)
// verifying if the dm_processed column exists in the event table after migration
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_DM_PROCESSED))
// verifying if events made prior to migration, status is marked as DEVICE_MODE_PROCESSING_DONE. For new messages it should be 0
let rsDbMessage: RSDBMessage = dbPersistentManager.fetchAllEventsFromDB(forMode: ALL)
XCTAssert(rsDbMessage.messageIds.count==3)
XCTAssert(rsDbMessage.statusList[0] as? Int == 1)
XCTAssert(rsDbMessage.statusList[1] as? Int == 1)
XCTAssert(rsDbMessage.statusList[2] as? Int == 0)
// verifying if events made prior to migration, dm_processed is marked as DM_PROCESSED_DONE. For new messages it should be 0
XCTAssert(rsDbMessage.dmProcessed[0] as? Int == 1)
XCTAssert(rsDbMessage.dmProcessed[1] as? Int == 1)
XCTAssert(rsDbMessage.dmProcessed[2] as? Int == 0)
}

func testCreateEventsTableWithVersion () throws {
dbPersistentManager.createEventsTable(withVersion: 3)
XCTAssert(getCount(sqlString: getCheckIfTableExistCommand(tableName: "events")) > 0)
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_STATUS) == true)
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_DM_PROCESSED) == true)
deleteEventsTable()
dbPersistentManager.createEventsTable(withVersion: 2)
XCTAssert(getCount(sqlString: getCheckIfTableExistCommand(tableName: "events")) > 0)
XCTAssert(dbPersistentManager.checkIfStatusColumnExists() == true)
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_STATUS) == true)
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_DM_PROCESSED) == false)
deleteEventsTable()
XCTAssert(getCount(sqlString: getCheckIfTableExistCommand(tableName: "events")) == 0)
dbPersistentManager.createEventsTable(withVersion: 1)
XCTAssert(getCount(sqlString: getCheckIfTableExistCommand(tableName: "events")) > 0)
XCTAssert(dbPersistentManager.checkIfStatusColumnExists() == false)
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_STATUS) == false)
XCTAssert(dbPersistentManager.checkIfColumnExists(COL_DM_PROCESSED) == false)
}

func testSaveEvent() throws {
Expand Down Expand Up @@ -182,6 +255,48 @@ class DBPersistentManagerTests: XCTestCase {
XCTAssertEqual(dbPersistentManager.fetchAllEventsFromDB(forMode: ALL).messageIds.count, 0)
}

func testMarkDeviceModeTransformationAndProcessedDone() throws {
dbPersistentManager.createEventsTable(withVersion: 3)
for _ in 1...3 {
dbPersistentManager.saveEvent(MESSAGE_1)
}
for i in 1...3 {
dbPersistentManager.markDeviceModeTransformationAndProcessedDone(i as NSNumber)
}
// verifying if status is marked as DEVICE_MODE_PROCESSING_DONE or not
let rsDbMessage: RSDBMessage = dbPersistentManager.fetchAllEventsFromDB(forMode: ALL)
XCTAssert(rsDbMessage.messageIds.count==3)
XCTAssert(rsDbMessage.statusList[0] as? Int == 1)
XCTAssert(rsDbMessage.statusList[1] as? Int == 1)
XCTAssert(rsDbMessage.statusList[2] as? Int == 1)
// verifying if dm_processed is marked as DM_PROCESSED_DONE done or not
XCTAssert(rsDbMessage.messageIds.count==3)
XCTAssert(rsDbMessage.dmProcessed[0] as? Int == 1)
XCTAssert(rsDbMessage.dmProcessed[1] as? Int == 1)
XCTAssert(rsDbMessage.dmProcessed[2] as? Int == 1)
}

func testMarkDeviceModeProcessedDone() throws {
dbPersistentManager.createEventsTable(withVersion: 3)
for _ in 1...3 {
dbPersistentManager.saveEvent(MESSAGE_1)
}
for i in 1...3 {
dbPersistentManager.markDeviceModeProcessedDone(i as NSNumber)
}
// verifying if status remains 0
let rsDbMessage: RSDBMessage = dbPersistentManager.fetchAllEventsFromDB(forMode: ALL)
XCTAssert(rsDbMessage.messageIds.count==3)
XCTAssert(rsDbMessage.statusList[0] as? Int == 0)
XCTAssert(rsDbMessage.statusList[1] as? Int == 0)
XCTAssert(rsDbMessage.statusList[2] as? Int == 0)
// verifying if dm_processed is marked as DM_PROCESSED_DONE done or not
XCTAssert(rsDbMessage.messageIds.count==3)
XCTAssert(rsDbMessage.dmProcessed[0] as? Int == 1)
XCTAssert(rsDbMessage.dmProcessed[1] as? Int == 1)
XCTAssert(rsDbMessage.dmProcessed[2] as? Int == 1)
}


func deleteEventsTable() {
executeSQL(sqlString: "drop table if exists events;")
Expand Down

0 comments on commit c6b5419

Please sign in to comment.