-
Notifications
You must be signed in to change notification settings - Fork 16
/
APITest.swift
219 lines (187 loc) · 9.53 KB
/
APITest.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
import CioTracking // do not use `@testable` so we can test functions are made public and not `internal`.
import Foundation
import SharedTests
import XCTest
/**
Contains an example of every public facing SDK function call. This file helps
us prevent introducing breaking changes to the SDK by accident. If a public function
of the SDK is modified, this test class will not successfully compile. By not compiling,
that is a reminder to either fix the compilation and introduce the breaking change or
fix the mistake and not introduce the breaking change in the code base.
*/
class TrackingAPITest: UnitTest {
let dictionaryData: [String: Any] = ["foo": true, "bar": ""]
struct EncodableExample: Encodable {
let foo: String
}
let encodableData = EncodableExample(foo: "")
// Test that public functions are accessible by mocked instances
let mock = CustomerIOInstanceMock()
func test_allPublicStaticPropertiesAvailable() throws {
try skipRunningTest()
_ = CustomerIO.version
}
// This function checks that public functions exist for the SDK and they are callable.
// Maybe we forgot to add a function? Maybe we forgot to make a function `public`?
func test_allPublicTrackingFunctions() throws {
try skipRunningTest()
// Initialize
CustomerIO.initialize(siteId: "", apiKey: "", region: .EU) { (config: inout CioSdkConfig) in
config.autoTrackPushEvents = false
}
// There is another `initialize()` function that's available to Notification Service Extension and not available
// to other targets (such as iOS).
// You should be able to uncomment the initialize() function below and should get compile errors saying that the
// function is not available to iOS.
// CustomerIO.initialize(siteId: "", apiKey: "", region: .EU) { (config: inout CioNotificationServiceExtensionSdkConfig) in }
// Reference some objects that should be public in the Tracking module
let _: Region = .EU
let _: CioLogLevel = .debug
// Public properties exposed to customers
_ = CustomerIO.shared.siteId
_ = CustomerIO.shared.config
// Identify
CustomerIO.shared.identify(identifier: "")
mock.identify(identifier: "")
CustomerIO.shared.identify(identifier: "", body: dictionaryData)
mock.identify(identifier: "", body: dictionaryData)
CustomerIO.shared.identify(identifier: "", body: encodableData)
mock.identify(identifier: "", body: encodableData)
// clear identify
CustomerIO.shared.clearIdentify()
mock.clearIdentify()
// event tracking
CustomerIO.shared.track(name: "")
mock.track(name: "")
CustomerIO.shared.track(name: "", data: dictionaryData)
mock.track(name: "", data: dictionaryData)
CustomerIO.shared.track(name: "", data: encodableData)
mock.track(name: "", data: encodableData)
// screen tracking
CustomerIO.shared.screen(name: "")
mock.screen(name: "")
CustomerIO.shared.screen(name: "", data: dictionaryData)
mock.screen(name: "", data: dictionaryData)
CustomerIO.shared.screen(name: "", data: encodableData)
mock.screen(name: "", data: encodableData)
// register push token
CustomerIO.shared.registerDeviceToken("")
mock.registerDeviceToken("")
// delete push token
CustomerIO.shared.deleteDeviceToken()
mock.deleteDeviceToken()
// track push metric
let metric = Metric.delivered
CustomerIO.shared.trackMetric(deliveryID: "", event: metric, deviceToken: "")
mock.trackMetric(deliveryID: "", event: metric, deviceToken: "")
checkDeviceProfileAttributes()
}
func checkDeviceProfileAttributes() {
// profile attributes
CustomerIO.shared.profileAttributes = dictionaryData
mock.profileAttributes = dictionaryData
// device attributes
CustomerIO.shared.deviceAttributes = dictionaryData
mock.deviceAttributes = dictionaryData
}
func test_allPublicSdkConfigOptions() throws {
try skipRunningTest()
CustomerIO.initialize(siteId: "", apiKey: "", region: .EU) { config in
config.trackingApiUrl = ""
config.autoTrackPushEvents = true
config.backgroundQueueMinNumberOfTasks = 10
config.backgroundQueueSecondsDelay = 10
config.logLevel = .error
config.autoTrackPushEvents = false
config.autoScreenViewBody = { [:] }
config.filterAutoScreenViewEvents = { viewController in
class MyViewController: UIViewController {}
return viewController is MyViewController
}
}
}
// SDK wrappers can configure the SDK from a Map.
// This test is in API tests as the String keys of the Map are public and need to not break for the SDK wrappers.
func test_createSdkConfigFromMap() {
let trackingApiUrl = String.random
let autoTrackPushEvents = false
let backgroundQueueMinNumberOfTasks = 10000
let backgroundQueueSecondsDelay: TimeInterval = 100000
let backgroundQueueExpiredSeconds: TimeInterval = 100000
let logLevel = "info"
let autoTrackScreenViews = true
let autoTrackDeviceAttributes = false
let sdkWrapperSource = "Flutter"
let sdkWrapperVersion = "1000.33333.4444"
let givenParamsFromSdkWrapper: [String: Any] = [
"trackingApiUrl": trackingApiUrl,
"autoTrackPushEvents": autoTrackPushEvents,
"backgroundQueueMinNumberOfTasks": backgroundQueueMinNumberOfTasks,
"backgroundQueueSecondsDelay": backgroundQueueSecondsDelay,
"backgroundQueueExpiredSeconds": backgroundQueueExpiredSeconds,
"logLevel": logLevel,
"autoTrackScreenViews": autoTrackScreenViews,
"autoTrackDeviceAttributes": autoTrackDeviceAttributes,
"source": sdkWrapperSource,
"version": sdkWrapperVersion
]
var actual = CioSdkConfig.Factory.create(siteId: "", apiKey: "", region: .US)
actual.modify(params: givenParamsFromSdkWrapper)
XCTAssertEqual(actual.trackingApiUrl, trackingApiUrl)
XCTAssertEqual(actual.autoTrackPushEvents, autoTrackPushEvents)
XCTAssertEqual(actual.backgroundQueueMinNumberOfTasks, backgroundQueueMinNumberOfTasks)
XCTAssertEqual(actual.backgroundQueueSecondsDelay, backgroundQueueSecondsDelay)
XCTAssertEqual(actual.backgroundQueueExpiredSeconds, backgroundQueueExpiredSeconds)
XCTAssertEqual(actual.logLevel.rawValue, logLevel)
XCTAssertEqual(actual.autoTrackScreenViews, autoTrackScreenViews)
XCTAssertEqual(actual.autoTrackDeviceAttributes, autoTrackDeviceAttributes)
XCTAssertNotNil(actual._sdkWrapperConfig)
}
func test_SdkConfigFromMap_givenWrongKeys_expectDefaults() {
let trackingApiUrl = String.random
let autoTrackPushEvents = false
let backgroundQueueMinNumberOfTasks = 10000
let backgroundQueueSecondsDelay: TimeInterval = 100000
let backgroundQueueExpiredSeconds: TimeInterval = 100000
let logLevel = "info"
let autoTrackScreenViews = true
let autoTrackDeviceAttributes = false
let sdkWrapperSource = "Flutter"
let sdkWrapperVersion = "1000.33333.4444"
let givenParamsFromSdkWrapper: [String: Any] = [
"trackingApiUrlWrong": trackingApiUrl,
"autoTrackPushEventsWrong": autoTrackPushEvents,
"backgroundQueueMinNumberOfTasksWrong": backgroundQueueMinNumberOfTasks,
"backgroundQueueSecondsDelayWrong": backgroundQueueSecondsDelay,
"backgroundQueueExpiredSecondsWrong": backgroundQueueExpiredSeconds,
"logLevelWrong": logLevel,
"autoTrackScreenViewsWrong": autoTrackScreenViews,
"autoTrackDeviceAttributesWrong": autoTrackDeviceAttributes,
"sourceWrong": sdkWrapperSource,
"versionWrong": sdkWrapperVersion
]
var actual = CioSdkConfig.Factory.create(siteId: "", apiKey: "", region: .US)
actual.modify(params: givenParamsFromSdkWrapper)
XCTAssertEqual(actual.trackingApiUrl, Region.US.productionTrackingUrl)
XCTAssertEqual(actual.autoTrackPushEvents, true)
XCTAssertEqual(actual.backgroundQueueMinNumberOfTasks, 10)
XCTAssertEqual(actual.backgroundQueueSecondsDelay, 30)
XCTAssertEqual(actual.backgroundQueueExpiredSeconds, TimeInterval(3 * 86400))
XCTAssertEqual(actual.logLevel.rawValue, CioLogLevel.error.rawValue)
XCTAssertEqual(actual.autoTrackScreenViews, false)
XCTAssertEqual(actual.autoTrackDeviceAttributes, true)
XCTAssertNil(actual._sdkWrapperConfig)
}
func test_SdkConfig_givenNoModification_expectDefaults() {
let actual = CioSdkConfig.Factory.create(siteId: "", apiKey: "", region: .US)
XCTAssertEqual(actual.trackingApiUrl, Region.US.productionTrackingUrl)
XCTAssertEqual(actual.autoTrackPushEvents, true)
XCTAssertEqual(actual.backgroundQueueMinNumberOfTasks, 10)
XCTAssertEqual(actual.backgroundQueueSecondsDelay, 30)
XCTAssertEqual(actual.backgroundQueueExpiredSeconds, TimeInterval(3 * 86400))
XCTAssertEqual(actual.logLevel.rawValue, CioLogLevel.error.rawValue)
XCTAssertEqual(actual.autoTrackScreenViews, false)
XCTAssertEqual(actual.autoTrackDeviceAttributes, true)
XCTAssertNil(actual._sdkWrapperConfig)
}
}