Skip to content

Commit 985d232

Browse files
crgee1rubikzube
andauthored
[CH12527] Add Integration Tests (#106)
* Add integration tests * Add url to browse request * Update data types and tests * Fixes * Fixes * Fixes * Get all test to pass * Test updates * Tweaks * Moved file * Headers * Test fixes * Updates * Fixes * Purchase * Lint * Tweaks Co-authored-by: Zubin Tiku <[email protected]>
1 parent 2795b89 commit 985d232

29 files changed

+354
-149
lines changed

.swiftlint.auto.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ disabled_rules:
1111
- identifier_name
1212
- number_separator
1313
- unused_closure_parameter
14+
- type_name
1415
opt_in_rules:
1516
- control_statement

.swiftlint.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ disabled_rules:
1111
- identifier_name
1212
- number_separator
1313
- unused_closure_parameter
14+
- type_name
1415
opt_in_rules:
1516
- anyobject_protocol
1617
- array_init

AutocompleteClient.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
0828954E256C5A85009A00BC /* CIOAutocompleteResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08289543256C5A85009A00BC /* CIOAutocompleteResult.swift */; };
2121
08465ECA2161C96900AEBD76 /* TrackSearchResultClickRequestBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08465EC92161C96900AEBD76 /* TrackSearchResultClickRequestBuilder.swift */; };
2222
08465ECC2161D0B100AEBD76 /* TrackSessionStartRequestBuilder.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08465ECB2161D0B100AEBD76 /* TrackSessionStartRequestBuilder.swift */; };
23+
0855BA4A25B00D5F00CC35F9 /* TrackingTaskResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0855BA4925B00D5F00CC35F9 /* TrackingTaskResponse.swift */; };
24+
0855BA4E25B010CC00CC35F9 /* ConstructorIOIntegrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0855BA4D25B010CC00CC35F9 /* ConstructorIOIntegrationTests.swift */; };
2325
08689F0121897914000D3565 /* ConstructorIOUserIDTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 08689F0021897914000D3565 /* ConstructorIOUserIDTests.swift */; };
2426
086D611923636E6600347771 /* CIOABTestCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 086D611823636E6600347771 /* CIOABTestCell.swift */; };
2527
0874A32E216BCE2700812CDC /* ConstructorIOABTestCellTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0874A32D216BCE2700812CDC /* ConstructorIOABTestCellTests.swift */; };
@@ -295,6 +297,8 @@
295297
08289543256C5A85009A00BC /* CIOAutocompleteResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CIOAutocompleteResult.swift; sourceTree = "<group>"; };
296298
08465EC92161C96900AEBD76 /* TrackSearchResultClickRequestBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrackSearchResultClickRequestBuilder.swift; sourceTree = "<group>"; };
297299
08465ECB2161D0B100AEBD76 /* TrackSessionStartRequestBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrackSessionStartRequestBuilder.swift; sourceTree = "<group>"; };
300+
0855BA4925B00D5F00CC35F9 /* TrackingTaskResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrackingTaskResponse.swift; sourceTree = "<group>"; };
301+
0855BA4D25B010CC00CC35F9 /* ConstructorIOIntegrationTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConstructorIOIntegrationTests.swift; sourceTree = "<group>"; };
298302
08689F0021897914000D3565 /* ConstructorIOUserIDTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConstructorIOUserIDTests.swift; sourceTree = "<group>"; };
299303
086D611823636E6600347771 /* CIOABTestCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CIOABTestCell.swift; sourceTree = "<group>"; };
300304
0874A32D216BCE2700812CDC /* ConstructorIOABTestCellTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConstructorIOABTestCellTests.swift; sourceTree = "<group>"; };
@@ -751,6 +755,7 @@
751755
isa = PBXGroup;
752756
children = (
753757
08A7E4102575FA15000FA02F /* ConstructorIOTrackInputFocusTests.swift */,
758+
0855BA4D25B010CC00CC35F9 /* ConstructorIOIntegrationTests.swift */,
754759
08A7E4142575FA95000FA02F /* ConstructorIOTrackAutocompleteSelectTests.swift */,
755760
08A7E4112575FA15000FA02F /* ConstructorIOTrackSearchSubmitTests.swift */,
756761
087F4BDB228B100D0058B467 /* ConstructorIOTrackSearchResultClickTests.swift */,
@@ -1227,6 +1232,7 @@
12271232
F63808F71F5D497200C3B322 /* NetworkResponse.swift */,
12281233
F65B9E5721469CA200101617 /* SearchTaskResponse.swift */,
12291234
0885D83F257189B70038AE96 /* BrowseTaskResponse.swift */,
1235+
0855BA4925B00D5F00CC35F9 /* TrackingTaskResponse.swift */,
12301236
);
12311237
path = Response;
12321238
sourceTree = "<group>";
@@ -2221,6 +2227,7 @@
22212227
F638090D1F5E880900C3B322 /* CIOHighlighter.swift in Sources */,
22222228
F6548834208633E20044E403 /* NSAttributedString+Build.swift in Sources */,
22232229
0885D83925717EFF0038AE96 /* CIOBrowseResponse.swift in Sources */,
2230+
0855BA4A25B00D5F00CC35F9 /* TrackingTaskResponse.swift in Sources */,
22242231
088F7D27210FA43C005B9FB4 /* CurrentTimeDateProvider.swift in Sources */,
22252232
F65BBCC22151621A007FEB22 /* IDGenerator.swift in Sources */,
22262233
F6B462821F6BACE80082C20B /* ResponseParserDelegate.swift in Sources */,
@@ -2307,6 +2314,7 @@
23072314
F62510D3205690670040E3DF /* ClosureDateProvider.swift in Sources */,
23082315
F674157B204D489B004F313B /* MockResponseParserDelegate.swift in Sources */,
23092316
F6852556213BCE9E00A27FAA /* MockCommons.swift in Sources */,
2317+
0855BA4E25B010CC00CC35F9 /* ConstructorIOIntegrationTests.swift in Sources */,
23102318
);
23112319
runOnlyForDeploymentPostprocessing = 0;
23122320
};

AutocompleteClient/Constants/Constants.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ struct Constants {
117117
static let apiKey = "key"
118118
static let baseURLString = "https://ac.cnstrc.com"
119119
static let expectedStatusCode = 204
120-
static let autocompleteSection = "autocomplete_section"
120+
static let autocompleteSection = "section"
121121
static let searchTerm = "term"
122122
static let trigger = "tr"
123123
static let triggerType = "click"
@@ -171,13 +171,14 @@ struct Constants {
171171
}
172172

173173
struct TrackPurchase {
174-
static let format = "%@/autocomplete/TERM_UNKNOWN/purchase"
174+
static let format = "%@/v2/behavioral_action/purchase"
175175
}
176176

177177
struct Logging {
178178
private static let prefix = "[ConstructorIO]:"
179179
private static let format: (_ message: String) -> String = { message in return "\(Logging.prefix) \(message)" }
180180
static let performURLRequest: (_ request: URLRequest) -> String = { request in return Logging.format("Performing URL Request \(request)") }
181+
static let recieveURLResponse: (_ response: HTTPURLResponse) -> String = { response in return Logging.format("Recieved URL Response \(response.statusCode)") }
181182
}
182183

183184
struct Session {

AutocompleteClient/FW/API/Network/Client/URLSessionNetworkClient.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ class URLSessionNetworkClient: NetworkClient {
1212

1313
func execute(_ request: URLRequest, completionHandler: @escaping (_ response: NetworkResponse) -> Void) {
1414
let task = URLSession.shared.dataTask(with: request) { data, response, error in
15+
1516
// Check for errors
1617
if let error = error {
1718
let err: Error = CIOError(rawValue: (error as NSError).code) ?? error
@@ -25,6 +26,8 @@ class URLSessionNetworkClient: NetworkClient {
2526
return
2627
}
2728

29+
ConstructorIO.logger.log(Constants.Logging.recieveURLResponse(httpResponse))
30+
2831
// Check if response code corresponds to a ConstructorIOError
2932
if let constructorError = CIOError(rawValue: httpResponse.statusCode) {
3033
completionHandler(NetworkResponse(error: constructorError))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//
2+
// TrackingTaskResponse.swift
3+
// ConstructorAutocomplete
4+
//
5+
// Copyright © Constructor.io. All rights reserved.
6+
// http://constructor.io/
7+
//
8+
9+
import UIKit
10+
11+
public class TrackingTaskResponse: TaskResponse<Data, Error> {}

AutocompleteClient/FW/Logic/Request/Builder/QueryItemCollection.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,24 @@ struct QueryItemCollection {
5959
})
6060
}
6161

62-
func all() -> [String: String] {
62+
func allAsDictionary() -> [String: Any] {
6363
let flattenedArray = self.queryItems.values.reduce([]) { res, next -> [URLQueryItem] in
6464
return res + next.reduce([], { res, next in return res + [next] })
6565
}
66-
var dict: [String: String] = [:]
66+
var dict: [String: Any] = [:]
6767
flattenedArray.forEach { item in
6868
if item.value != nil {
69-
dict[item.name] = String(item.value!)
69+
dict[item.name] = item.value
7070
}
7171
}
72+
// swiftlint:disable force_cast
73+
if dict["s"] != nil {
74+
dict["s"] = Int64(dict["s"] as! String)
75+
}
76+
if dict["_dt"] != nil {
77+
dict["_dt"] = Int64(dict["_dt"] as! String)
78+
}
79+
// swiftlint:enable force_cast
7280
return dict
7381
}
7482
}

AutocompleteClient/FW/Logic/Request/Builder/RequestBuilder.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,17 @@ class RequestBuilder {
6868
allQueryItems.add((urlComponents.queryItems?.first)!)
6969
}
7070

71-
urlComponents.queryItems = allQueryItems.all()
71+
urlComponents.queryItems = self.trackData!.queryItems(baseItems: allQueryItems.all())
7272

7373
let url = urlComponents.url!
74+
let httpBody = self.trackData!.httpBody(baseParams: allQueryItems.allAsDictionary())
75+
7476
var request = URLRequest(url: url)
75-
request.httpBody = self.trackData!.httpBody(baseParams: allQueryItems.all())
77+
if httpBody != nil {
78+
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
79+
request.addValue("application/json", forHTTPHeaderField: "Accept")
80+
request.httpBody = httpBody
81+
}
7682
request.httpMethod = self.trackData!.httpMethod()
7783

7884
return request

AutocompleteClient/FW/Logic/Request/CIORequestData.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ protocol CIORequestData {
1414

1515
func url(with baseURL: String) -> String
1616

17+
func queryItems(baseItems: [URLQueryItem]) -> [URLQueryItem]
18+
1719
func httpMethod() -> String
1820

19-
func httpBody(baseParams: [String: String]) -> Data?
21+
func httpBody(baseParams: [String: Any]) -> Data?
2022
}
2123

2224
extension CIORequestData {
@@ -26,7 +28,12 @@ extension CIORequestData {
2628
}
2729

2830
// default body is null
29-
func httpBody(baseParams: [String: String]) -> Data? {
31+
func httpBody(baseParams: [String: Any]) -> Data? {
3032
return nil
3133
}
34+
35+
// default query items is all of them
36+
func queryItems(baseItems: [URLQueryItem]) -> [URLQueryItem] {
37+
return baseItems
38+
}
3239
}

AutocompleteClient/FW/Logic/Request/CIOTrackBrowseResultClickData.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,15 @@ struct CIOTrackBrowseResultClickData: CIORequestData {
3939
return "POST"
4040
}
4141

42-
func httpBody(baseParams: [String: String]) -> Data? {
42+
func httpBody(baseParams: [String: Any]) -> Data? {
4343
var dict = [
4444
"filter_name": self.filterName,
4545
"filter_value": self.filterValue,
4646
"item_id": self.customerID
47-
]
47+
] as [String : Any]
48+
4849
if self.resultPositionOnPage != nil {
49-
dict["result_position_on_page"] = String(self.resultPositionOnPage!)
50+
dict["result_position_on_page"] = Int(self.resultPositionOnPage!)
5051
}
5152
if self.sectionName != nil {
5253
dict["section"] = self.sectionName
@@ -56,6 +57,7 @@ struct CIOTrackBrowseResultClickData: CIORequestData {
5657
}
5758

5859
dict.merge(baseParams) { current, _ in current }
59-
return try? JSONEncoder().encode(dict)
60+
61+
return try? JSONSerialization.data(withJSONObject: dict)
6062
}
6163
}

0 commit comments

Comments
 (0)