Skip to content

Commit d368e03

Browse files
jaeoptthomaszurkan-optimizely
authored andcommitted
add tests for Data Model coverage (#218)
* ignore tests from code coverage * add tests to cover experimentFeatureMap * add tests to cover EventForDispatch * add tests for AttributeValue coverage * add tests for UserAttribute coverage * add tests for DataModel coverage * Fixed file location issue for ConditionLeaf and AttributeValue files. * recover tests into coverage * changes per review comments * fix per review comments
1 parent b2478c6 commit d368e03

14 files changed

+607
-90
lines changed

.slather.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ coverage_service: coveralls
22
xcodeproj: OptimizelySDK/OptimizelySwiftSDK.xcodeproj
33
scheme: OptimizelySwiftSDK-iOS
44
workspace: OptimizelySDK.xcworkspace
5-
configuration: Release
5+
configuration: Release

OptimizelySDK/Data Model/Audience/AttributeValue.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,12 @@ enum AttributeValue: Codable, Equatable, CustomStringConvertible {
7676
}
7777

7878
// NOTE: keep {Double, Float} before Int checking for testing consistency
79+
// Int values are all filtered as Double, so no separate parsing for int
7980
if let value = try? container.decode(Double.self) {
8081
self = .double(value)
8182
return
8283
}
8384

84-
if let value = try? container.decode(Int64.self) {
85-
self = .int(value)
86-
return
87-
}
88-
8985
if let value = try? container.decode(Bool.self) {
9086
self = .bool(value)
9187
return

OptimizelySDK/Data Model/Audience/ConditionLeaf.swift

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,9 @@ enum ConditionLeaf: Codable, Equatable {
2626
if let value = try? container.decode(String.self) {
2727
self = .audienceId(value)
2828
return
29-
}
30-
31-
do {
32-
let value = try container.decode(UserAttribute.self)
29+
} else if let value = try? container.decode(UserAttribute.self) {
3330
self = .attribute(value)
3431
return
35-
} catch {
36-
3732
}
3833

3934
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: [], debugDescription: "Failed to decode ConditionLeaf"))

OptimizelySDK/Data Model/Audience/UserAttribute.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct UserAttribute: Codable, Equatable {
5858

5959
return ConditionMatch(rawValue: rawMatch)
6060
}
61-
61+
6262
// MARK: - init
6363

6464
init(from decoder: Decoder) throws {

OptimizelySDK/Data Model/ProjectConfig.swift

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -95,34 +95,19 @@ class ProjectConfig {
9595
}
9696

9797
convenience init(datafile: String) throws {
98-
guard let data = datafile.data(using: .utf8) else {
99-
throw OptimizelyError.dataFileInvalid
100-
}
101-
102-
try self.init(datafile: data)
98+
try self.init(datafile: Data(datafile.utf8))
10399
}
104100

105101
init() {
106102
}
107103

108-
class func dateFromString(dateString: String) -> NSDate {
109-
let dateFormatter = DateFormatter()
110-
let enUSPosixLocale = NSLocale(localeIdentifier: "en_US_POSIX")
111-
dateFormatter.locale = enUSPosixLocale as Locale
112-
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
113-
return dateFormatter.date(from: dateString)! as NSDate
114-
}
115104
}
116105

117106
extension ProjectConfig {
118107
private func whitelistUser(userId: String, experimentId: String, variationId: String) {
119-
if var dic = whitelistUsers[userId] {
120-
dic[experimentId] = variationId
121-
} else {
122-
var dic = [String: String]()
123-
dic[experimentId] = variationId
124-
whitelistUsers[userId] = dic
125-
}
108+
var dic = whitelistUsers[userId] ?? [String: String]()
109+
dic[experimentId] = variationId
110+
whitelistUsers[userId] = dic
126111
}
127112

128113
private func removeFromWhitelist(userId: String, experimentId: String) {
@@ -132,10 +117,10 @@ extension ProjectConfig {
132117
private func getWhitelistedVariationId(userId: String, experimentId: String) -> String? {
133118
if var dic = whitelistUsers[userId] {
134119
return dic[experimentId]
135-
} else {
136-
logger.d(.userHasNoForcedVariation(userId))
137-
return nil
138120
}
121+
122+
logger.d(.userHasNoForcedVariation(userId))
123+
return nil
139124
}
140125

141126
private func isValidVersion(version: String) -> Bool {
@@ -252,14 +237,14 @@ extension ProjectConfig {
252237
if let variation = experiment.getVariation(id: id) {
253238
logger.d(.userHasForcedVariation(userId, experiment.key, variation.key))
254239
return variation
255-
} else {
256-
logger.d(.userHasForcedVariationButInvalid(userId, experiment.key))
257-
return nil
258240
}
259-
} else {
260-
logger.d(.userHasNoForcedVariationForExperiment(userId, experiment.key))
241+
242+
logger.d(.userHasForcedVariationButInvalid(userId, experiment.key))
261243
return nil
262244
}
245+
246+
logger.d(.userHasNoForcedVariationForExperiment(userId, experiment.key))
247+
return nil
263248
}
264249

265250
/**
@@ -284,7 +269,7 @@ extension ProjectConfig {
284269
return false
285270
}
286271

287-
guard let variation = experiment.variations.filter({$0.key == variationKey }).first else {
272+
guard let variation = experiment.getVariation(key: variationKey) else {
288273
logger.e(.variationKeyInvalid(experimentKey, variationKey))
289274
return false
290275
}

0 commit comments

Comments
 (0)