Skip to content

Commit 4ed3258

Browse files
authored
Add AI generated tests for ConfigDBManager (#14285)
1 parent c0f1c3c commit 4ed3258

File tree

3 files changed

+487
-45
lines changed

3 files changed

+487
-45
lines changed

FirebaseRemoteConfig/SwiftNew/ConfigDBManager.swift

Lines changed: 14 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ open class ConfigDBManager: NSObject {
5252
/// Shared Singleton Instance
5353
@objc public static let sharedInstance = ConfigDBManager()
5454

55-
private let databaseActor: DatabaseActor
56-
57-
@objc public var isNewDatabase: Bool = false
55+
let databaseActor: DatabaseActor
56+
@objc public var isNewDatabase: Bool
5857

5958
@objc public init(dbPath: String = remoteConfigPathForDatabase()) {
6059
databaseActor = DatabaseActor(dbPath: dbPath)
60+
isNewDatabase = !FileManager.default.fileExists(atPath: dbPath)
6161
super.init()
6262
}
6363

@@ -306,51 +306,31 @@ open class ConfigDBManager: NSObject {
306306
func deleteRecord(fromMainTableWithNamespace namespace: String,
307307
bundleIdentifier: String,
308308
fromSource source: DBSource) {
309-
let params = [bundleIdentifier, namespace]
310-
let sql =
311-
if source == .default {
312-
"DELETE FROM main_default WHERE bundle_identifier = ? and namespace = ?"
313-
} else if source == .active {
314-
"DELETE FROM main_active WHERE bundle_identifier = ? and namespace = ?"
315-
} else {
316-
"DELETE FROM main WHERE bundle_identifier = ? and namespace = ?"
317-
}
318309
Task {
319-
await self.databaseActor.executeQuery(sql, withParams: params)
310+
await self.databaseActor
311+
.deleteRecord(
312+
fromMainTableWithNamespace: namespace,
313+
bundleIdentifier: bundleIdentifier,
314+
fromSource: source
315+
)
320316
}
321317
}
322318

323319
@objc public
324320
func deleteRecord(withBundleIdentifier bundleIdentifier: String,
325321
namespace: String) {
326-
let sql = "DELETE FROM fetch_metadata_v2 WHERE bundle_identifier = ? and namespace = ?"
327-
let params = [bundleIdentifier, namespace]
328-
Task {
329-
await self.databaseActor.executeQuery(sql, withParams: params)
330-
}
331-
}
332-
333-
@objc public
334-
func deleteAllRecords(fromTableWithSource source: DBSource) {
335-
let sql =
336-
if source == .default {
337-
"DELETE FROM main_default"
338-
} else if source == .active {
339-
"DELETE FROM main_active"
340-
} else {
341-
"DELETE FROM main"
342-
}
343322
Task {
344-
await self.databaseActor.executeQuery(sql)
323+
await self.databaseActor.deleteRecord(
324+
withBundleIdentifier: bundleIdentifier,
325+
namespace: namespace
326+
)
345327
}
346328
}
347329

348330
@objc public
349331
func deleteExperimentTable(forKey key: String) {
350-
let params = [key]
351-
let sql = "DELETE FROM experiment WHERE key = ?"
352332
Task {
353-
await self.databaseActor.executeQuery(sql, withParams: params)
333+
await self.databaseActor.deleteExperimentTable(forKey: key)
354334
}
355335
}
356336

FirebaseRemoteConfig/SwiftNew/DatabaseActor.swift

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ private let RCNDatabaseName = "RemoteConfig.sqlite3"
2323
// Actor for database operations
2424
actor DatabaseActor {
2525
private var database: OpaquePointer?
26-
private var isNewDatabase: Bool = false
27-
private let dbPath: String
26+
let dbPath: String
2827

2928
init(dbPath: String) {
3029
self.dbPath = dbPath
@@ -220,8 +219,8 @@ actor DatabaseActor {
220219
return logError(withSQL: sql, finalizeStatement: statement, returnValue: false)
221220
}
222221
if bindText(statement, 1, key) != SQLITE_OK ||
223-
sqlite3_bind_blob(statement, 2, (value as NSData).bytes, Int32(value.count), nil) != SQLITE_OK
224-
{
222+
sqlite3_bind_blob(statement, 2, (value as NSData).bytes, Int32(value.count), nil) !=
223+
SQLITE_OK {
225224
return logError(withSQL: sql, finalizeStatement: statement, returnValue: false)
226225
}
227226
if sqlite3_step(statement) != SQLITE_DONE {
@@ -537,7 +536,7 @@ actor DatabaseActor {
537536
return results
538537
}
539538

540-
func loadRolloutTable(fromKey key: String) -> [[String: Any]] {
539+
func loadRolloutTable(fromKey key: String) -> [[String: Sendable]] {
541540
let sql = "SELECT value FROM rollout WHERE key = ?"
542541
var statement: OpaquePointer?
543542
if sqlite3_prepare_v2(database, sql, -1, &statement, nil) != SQLITE_OK {
@@ -559,11 +558,11 @@ actor DatabaseActor {
559558
if let data = results.first {
560559
// Convert from NSData to NSArray
561560
if let rollout = try? JSONSerialization
562-
.jsonObject(with: data, options: []) as? [[String: Any]] {
561+
.jsonObject(with: data, options: []) as? [[String: Sendable]] {
563562
return rollout
564563
} else {
565564
RCLog.error("I-RCN000011",
566-
"Failed to convert NSData to NSAarry for Rollout Metadata")
565+
"Failed to convert NSData to NSArray for Rollout Metadata")
567566
}
568567
}
569568

@@ -659,7 +658,6 @@ actor DatabaseActor {
659658
}
660659
let fileManager = FileManager.default
661660
if !fileManager.fileExists(atPath: filePath) {
662-
isNewDatabase = true
663661
do {
664662
try fileManager.createDirectory(
665663
atPath: URL(fileURLWithPath: filePath).deletingLastPathComponent().path,
@@ -764,11 +762,57 @@ actor DatabaseActor {
764762
executeQuery(createRollout)
765763
}
766764

767-
func removeDatabase(atPath path: String) {
765+
// MARK: - Delete
766+
767+
func deleteRecord(fromMainTableWithNamespace namespace: String,
768+
bundleIdentifier: String,
769+
fromSource source: DBSource) {
770+
let params = [bundleIdentifier, namespace]
771+
let sql =
772+
if source == .default {
773+
"DELETE FROM main_default WHERE bundle_identifier = ? and namespace = ?"
774+
} else if source == .active {
775+
"DELETE FROM main_active WHERE bundle_identifier = ? and namespace = ?"
776+
} else {
777+
"DELETE FROM main WHERE bundle_identifier = ? and namespace = ?"
778+
}
779+
executeQuery(sql, withParams: params)
780+
}
781+
782+
func deleteRecord(withBundleIdentifier bundleIdentifier: String,
783+
namespace: String) {
784+
let sql = "DELETE FROM fetch_metadata_v2 WHERE bundle_identifier = ? and namespace = ?"
785+
let params = [bundleIdentifier, namespace]
786+
executeQuery(sql, withParams: params)
787+
}
788+
789+
func deleteAllRecords(fromTableWithSource source: DBSource) {
790+
let sql =
791+
if source == .default {
792+
"DELETE FROM main_default"
793+
} else if source == .active {
794+
"DELETE FROM main_active"
795+
} else {
796+
"DELETE FROM main"
797+
}
798+
executeQuery(sql)
799+
}
800+
801+
func deleteExperimentTable(forKey key: String) {
802+
let params = [key]
803+
let sql = "DELETE FROM experiment WHERE key = ?"
804+
executeQuery(sql, withParams: params)
805+
}
806+
807+
func closeDatabase(atPath path: String) {
768808
if sqlite3_close(database) != SQLITE_OK {
769809
logDatabaseError()
770810
}
771811
database = nil
812+
}
813+
814+
func removeDatabase(atPath path: String) {
815+
closeDatabase(atPath: path)
772816

773817
do {
774818
try FileManager.default.removeItem(atPath: path)
@@ -778,17 +822,24 @@ actor DatabaseActor {
778822
}
779823
}
780824

825+
@discardableResult
781826
func executeQuery(_ sql: String) -> Bool {
782827
var error: UnsafeMutablePointer<Int8>?
783828
if sqlite3_exec(database, sql, nil, nil, &error) != SQLITE_OK {
784-
RCLog.error("I-RCN000012",
785-
"Failed to execute query with error \(error!).")
829+
if let error {
830+
RCLog.error("I-RCN000012",
831+
"Failed to execute query with error \(error).")
832+
} else {
833+
RCLog.error("I-RCN000012",
834+
"Failed to execute query with no error.")
835+
}
786836
sqlite3_free(error)
787837
return false
788838
}
789839
return true
790840
}
791841

842+
@discardableResult
792843
func executeQuery(_ sql: String, withParams params: [String]) -> Bool {
793844
var statement: OpaquePointer? = nil
794845
defer { sqlite3_finalize(statement) }

0 commit comments

Comments
 (0)