Skip to content

Commit

Permalink
Logging message test case added for duplicated key
Browse files Browse the repository at this point in the history
  • Loading branch information
muzahidul-opti committed Nov 28, 2023
1 parent 57438e7 commit d760c6c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Sources/Optimizely/OptimizelyConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ struct OptimizelyConfigImp: OptimizelyConfig {
var attributes: [OptimizelyAttribute] = []
var audiences: [OptimizelyAudience] = []
var events: [OptimizelyEvent] = []
private var logger = OPTLoggerFactory.getLogger()
var logger = OPTLoggerFactory.getLogger()

init(projectConfig: ProjectConfig) {
guard let project = projectConfig.project else { return }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase {
let datafile = OTUtils.loadJSONDatafile("optimizely_config_datafile")!

self.optimizely = OptimizelyClient(sdkKey: "12345",
logger: TestLogger(),
userProfileService: OTUtils.createClearUserProfileService())
try! self.optimizely.start(datafile: datafile)
}
Expand Down Expand Up @@ -307,13 +308,13 @@ class OptimizelyClientTests_OptimizelyConfig: XCTestCase {
let projectConfig = ProjectConfig()
projectConfig.project = model

optimizely = OptimizelyClient(
sdkKey: "demo_key"
)

optimizely.config = projectConfig

let optimizelyExpMap: [String: OptimizelyExperiment] = try! optimizely.getOptimizelyConfig().experimentsMap

let optiConfig = try! optimizely.getOptimizelyConfig()
let optimizelyExpMap: [String: OptimizelyExperiment] = optiConfig.experimentsMap

let logger = (optiConfig as! OptimizelyConfigImp).logger as! TestLogger
XCTAssertEqual(logger.getMessages(.warning), ["Duplicate experiment keys found in datafile: duplicate_key"])

XCTAssertEqual(optimizelyExpMap.count, 1)
XCTAssertEqual(optimizelyExpMap["duplicate_key"]?.id, "10005")
Expand Down Expand Up @@ -426,3 +427,41 @@ extension OptimizelyEvent {
}
}

// MARK: - Mock Loggers

fileprivate class TestLogger: OPTLogger {
private static var _logLevel: OptimizelyLogLevel?
public static var logLevel: OptimizelyLogLevel {
get {
return _logLevel ?? .info
}
set (newLevel) {
_logLevel = newLevel
}
}

required public init() {
clearMessages()
}

func log(level: OptimizelyLogLevel, message: String) {
logMessages[level.rawValue].append(message)
}

// Utils

var logMessages = [[String]]()

var logCount: Int {
return logMessages.reduce(0) { $0 + $1.count }
}

func getMessages(_ level: OptimizelyLogLevel) -> [String] {
return logMessages[level.rawValue]
}

func clearMessages() {
logMessages = [[String]](repeating: [], count: OptimizelyLogLevel.debug.rawValue + 1)
}

}

0 comments on commit d760c6c

Please sign in to comment.