Skip to content

Commit 423607e

Browse files
authored
Merge pull request #165 from optimizely/moreUnitTests
(chore): add unit tests to push coverage up
2 parents e0cb996 + b8396e0 commit 423607e

File tree

4 files changed

+140
-0
lines changed

4 files changed

+140
-0
lines changed

OptimizelySDK/OptimizelySwiftSDK.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
0B4E11FA21EE8FF400D5B370 /* OPTDataStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B4E11F821EE8FF400D5B370 /* OPTDataStore.swift */; };
6060
0B4E11FC21EE90F600D5B370 /* DataStoreUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B4E11FB21EE90F600D5B370 /* DataStoreUserDefaults.swift */; };
6161
0B4E11FD21EE90F600D5B370 /* DataStoreUserDefaults.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B4E11FB21EE90F600D5B370 /* DataStoreUserDefaults.swift */; };
62+
0B519AA12273ACD500D909D2 /* DataStoreTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B519AA02273ACD500D909D2 /* DataStoreTests.swift */; };
6263
0B626D612242B98E00E4F7DA /* NotificationCenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B626D602242B98D00E4F7DA /* NotificationCenterTests.swift */; };
6364
0B626D622242B98E00E4F7DA /* NotificationCenterTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B626D602242B98D00E4F7DA /* NotificationCenterTests.swift */; };
6465
0B77D04022331D1E005AA83F /* OptimizelyManager+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B77D03F22331D1E005AA83F /* OptimizelyManager+Extension.swift */; };
@@ -1190,6 +1191,7 @@
11901191
0B4AEA102228A6430047B75D /* unsupported_datafile.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = unsupported_datafile.json; sourceTree = "<group>"; };
11911192
0B4E11F821EE8FF400D5B370 /* OPTDataStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OPTDataStore.swift; sourceTree = "<group>"; };
11921193
0B4E11FB21EE90F600D5B370 /* DataStoreUserDefaults.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreUserDefaults.swift; sourceTree = "<group>"; };
1194+
0B519AA02273ACD500D909D2 /* DataStoreTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreTests.swift; sourceTree = "<group>"; };
11931195
0B54908A21C9602900BE04C5 /* DefaultNotificationCenter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DefaultNotificationCenter.swift; sourceTree = "<group>"; };
11941196
0B5ADA2521CC62CD0049BC2C /* DataStoreQueueStack.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataStoreQueueStack.swift; sourceTree = "<group>"; };
11951197
0B5ADA2A21CC739E0049BC2C /* EventForDispatch+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "EventForDispatch+Extension.swift"; sourceTree = "<group>"; };
@@ -1762,6 +1764,7 @@
17621764
0BC47BD52242F7EF00E5C2CD /* DatafileHandlerTests.swift */,
17631765
0BC47BE42243012E00E5C2CD /* MurmurTests.swift */,
17641766
C79F9D852251F58F002B0BC9 /* DecisionListenerTests.swift */,
1767+
0B519AA02273ACD500D909D2 /* DataStoreTests.swift */,
17651768
);
17661769
path = "OptimizelyTests-Common";
17671770
sourceTree = "<group>";
@@ -3008,6 +3011,7 @@
30083011
6EA425C92218E74D00B074B5 /* Audience.swift in Sources */,
30093012
6EA425C62218E74D00B074B5 /* OPTDataStore.swift in Sources */,
30103013
6EA425BD2218E74D00B074B5 /* DefaultDecisionService.swift in Sources */,
3014+
0B519AA12273ACD500D909D2 /* DataStoreTests.swift in Sources */,
30113015
6EA425D02218E74D00B074B5 /* Attribute.swift in Sources */,
30123016
6E0AFBBF2257C49800BC7665 /* BucketTests_Base.swift in Sources */,
30133017
6EA425B32218E74D00B074B5 /* DefaultUserProfileService.swift in Sources */,

OptimizelySDK/OptimizelySwiftSDK.xcodeproj/xcshareddata/xcschemes/OptimizelySwiftSDK-iOS.xcscheme

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
buildConfiguration = "Debug"
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
29+
codeCoverageEnabled = "YES"
2930
onlyGenerateCoverageForSpecifiedTargets = "YES"
3031
shouldUseLaunchSchemeArgsEnv = "YES">
3132
<CodeCoverageTargets>

OptimizelySDK/OptimizelyTests/OptimizelyTests-APIs/OptimizelyManagerTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,33 @@ class OptimizelyManagerTests: XCTestCase {
130130
let experiment:Experiment? = optimizely?.config?.getExperiment(key: "ab_running_exp_audience_combo_empty_conditions")
131131

132132
XCTAssertNotNil(experiment)
133+
134+
let ex2 = optimizely?.config?.getExperiment(id: experiment!.id)
135+
136+
XCTAssert(experiment == ex2)
137+
138+
XCTAssert(ex2!.id == optimizely?.config?.getExperimentId(key: experiment!.key))
139+
140+
141+
let event = optimizely?.config?.project.events.first
142+
143+
let evId = optimizely?.config?.getEventId(key: event!.key)
144+
145+
XCTAssert(event!.id == evId)
133146

134147
let variation = experiment?.getVariation(key: "all_traffic_variation")
148+
135149
XCTAssertNotNil(variation)
150+
151+
let var2 = ex2?.getVariation(id: variation!.id)
152+
153+
XCTAssert(variation == var2)
154+
155+
let audience = optimizely?.config?.project.audiences.first
156+
157+
let aud2 = optimizely?.config?.getAudience(id: audience!.id)
158+
159+
XCTAssert(audience == aud2)
136160
}
137161

138162
// func testPerformanceInitialize() {
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
/****************************************************************************
3+
* Copyright 2019, Optimizely, Inc. and contributors *
4+
* *
5+
* Licensed under the Apache License, Version 2.0 (the "License"); *
6+
* you may not use this file except in compliance with the License. *
7+
* You may obtain a copy of the License at *
8+
* *
9+
* http://www.apache.org/licenses/LICENSE-2.0 *
10+
* *
11+
* Unless required by applicable law or agreed to in writing, software *
12+
* distributed under the License is distributed on an "AS IS" BASIS, *
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
14+
* See the License for the specific language governing permissions and *
15+
* limitations under the License. *
16+
***************************************************************************/
17+
18+
19+
import XCTest
20+
21+
class DataStoreTests: XCTestCase {
22+
23+
override func setUp() {
24+
// Put setup code here. This method is called before the invocation of each test method in the class.
25+
if let url = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
26+
if (!FileManager.default.fileExists(atPath: url.path)) {
27+
do {
28+
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: false, attributes: nil)
29+
}
30+
catch {
31+
print(error)
32+
}
33+
34+
}
35+
}
36+
37+
}
38+
39+
override func tearDown() {
40+
// Put teardown code here. This method is called after the invocation of each test method in the class.
41+
}
42+
43+
func testMemoryStore() {
44+
// This is an example of a functional test case.
45+
// Use XCTAssert and related functions to verify your tests produce the correct results.
46+
47+
let datastore = DataStoreMemory<String>(storeName: "testingDataStoreMemory")
48+
49+
datastore.saveItem(forKey: "testString", value: "value")
50+
51+
let value = datastore.getItem(forKey: "testString") as! String
52+
53+
XCTAssert(value == "value")
54+
55+
datastore.unsubscribe()
56+
57+
datastore.subscribe()
58+
59+
datastore.load(forKey: "testingDataStoreMemory")
60+
61+
let v2 = datastore.getItem(forKey: "testString") as! String
62+
63+
XCTAssert(v2 == value)
64+
}
65+
66+
func testFileStore() {
67+
// This is an example of a functional test case.
68+
// Use XCTAssert and related functions to verify your tests produce the correct results.
69+
70+
let datastore = DataStoreFile<[String]>(storeName: "testingDataStoreFile")
71+
72+
datastore.saveItem(forKey: "testString", value: ["value"])
73+
74+
let vj = datastore.getItem(forKey: "testString") as! [String]
75+
76+
XCTAssert(vj.first == "value")
77+
78+
}
79+
80+
func testUserDefaults() {
81+
// This is an example of a functional test case.
82+
// Use XCTAssert and related functions to verify your tests produce the correct results.
83+
84+
let datastore = DataStoreMemory<String>(storeName: "testingDataStoreUserDefaults")
85+
86+
datastore.saveItem(forKey: "testString", value: "value")
87+
88+
let value = datastore.getItem(forKey: "testString") as! String
89+
90+
XCTAssert(value == "value")
91+
92+
datastore.unsubscribe()
93+
94+
datastore.subscribe()
95+
96+
datastore.load(forKey: "testingDataStoreUserDefaults")
97+
98+
let v2 = datastore.getItem(forKey: "testString") as! String
99+
100+
XCTAssert(v2 == value)
101+
102+
}
103+
104+
func testPerformanceExample() {
105+
// This is an example of a performance test case.
106+
self.measure {
107+
// Put the code you want to measure the time of here.
108+
}
109+
}
110+
111+
}

0 commit comments

Comments
 (0)