Skip to content

Commit cfb2d3e

Browse files
committed
fix JSON body + add some unit test
1 parent d170de0 commit cfb2d3e

File tree

4 files changed

+177
-20
lines changed

4 files changed

+177
-20
lines changed

Source/Client.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ public class Client {
101101
}
102102
self.hostnames.shuffle()
103103

104-
if let dsnHost = dsnHost {
105-
self.hostnames.insert(dsnHost, atIndex: 0)
106-
} else {
107-
self.hostnames.insert("\(appID)-dsn.algolia.net", atIndex: 0)
104+
if dsn {
105+
if let dsnHost = dsnHost {
106+
self.hostnames.insert(dsnHost, atIndex: 0)
107+
} else {
108+
self.hostnames.insert("\(appID)-dsn.algolia.net", atIndex: 0)
109+
}
108110
}
109111

110112
let version = NSBundle(identifier: "com.algolia.AlgoliaSearch")!.infoDictionary!["CFBundleShortVersionString"] as String
@@ -351,13 +353,13 @@ public class Client {
351353
func performHTTPQuery(path: String, method: Alamofire.Method, body: [String: AnyObject]?, index: Int = 0, block: CompletionHandler? = nil) {
352354
assert(index < hostnames.count, "\(index) < \(hostnames.count) !")
353355

354-
let request = Alamofire.request(method, "https://\(hostnames[index])/\(path)", parameters: body).responseJSON {
356+
let request = Alamofire.request(method, "https://\(hostnames[index])/\(path)", parameters: body, encoding: .JSON).responseJSON {
355357
(request, response, data, error) -> Void in
356358
if let statusCode = response?.statusCode {
357359
if let block = block {
358360
switch(statusCode) {
359361
case 200, 201:
360-
block(JSON: data, error: nil)
362+
block(JSON: (data as [String: AnyObject]), error: nil)
361363
case 400:
362364
block(JSON: nil, error: NSError(domain: "Bad request argument", code: 400, userInfo: nil))
363365
case 403:

Source/Index.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,10 @@ public class Index {
206206
/// All server task are asynchronous and you can check with this method that the task is published.
207207
///
208208
/// :param: taskID The ID of the task returned by server
209-
public func waitTask(taskID: String, block: CompletionHandler) {
210-
let path = "1/indexes/\(urlEncodedIndexName)/task/\(taskID.urlEncode())"
209+
public func waitTask(taskID: Int, block: CompletionHandler) {
210+
let path = "1/indexes/\(urlEncodedIndexName)/task/\(taskID)"
211211
client.performHTTPQuery(path, method: .GET, body: nil, block: { (JSON, error) -> Void in
212-
if let JSON = JSON as? [String: AnyObject] {
212+
if let JSON = JSON {
213213
if (JSON["status"] as? String) == "published" {
214214
block(JSON: JSON, error: nil)
215215
} else {

Source/Type.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import Foundation
2525

26-
public typealias CompletionHandler = (JSON: AnyObject?, error: NSError?) -> Void
26+
public typealias CompletionHandler = (JSON: [String: AnyObject]?, error: NSError?) -> Void
2727

2828
struct RingBuffer<T>: SequenceType {
2929
typealias Generator = IndexingGenerator<Array<T>>

Tests/AlgoliaSearchTests.swift

Lines changed: 165 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,180 @@ import AlgoliaSearch
2626
import Alamofire
2727

2828
class AlgoliaSearchTests: XCTestCase {
29+
let expectationTimeout: NSTimeInterval = 100
30+
2931
var client: Client!
32+
var index: Index!
3033

3134
override func setUp() {
3235
super.setUp()
33-
client = AlgoliaSearch.Client(appID: "***REMOVED***", apiKey: "***REMOVED***")
36+
let appID = NSProcessInfo.processInfo().environment["ALGOLIA_APPLICATION_ID"] as String
37+
let apiKey = NSProcessInfo.processInfo().environment["ALGOLIA_API_KEY"] as String
38+
client = AlgoliaSearch.Client(appID: appID, apiKey: apiKey)
39+
index = client.getIndex("algol?à-swift")
40+
41+
let expectation = expectationWithDescription("Delete index")
42+
client.deleteIndex(index.indexName, block: { (JSON, error) -> Void in
43+
XCTAssertNil(error, "Error during deleteIndex: \(error?.description)")
44+
expectation.fulfill()
45+
})
46+
47+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
3448
}
3549

3650
override func tearDown() {
37-
// Put teardown code here. This method is called after the invocation of each test method in the class.
3851
super.tearDown()
52+
53+
let expectation = expectationWithDescription("Delete index")
54+
client.deleteIndex(index.indexName, block: { (JSON, error) -> Void in
55+
XCTAssertNil(error, "Error during deleteIndex: \(error?.description)")
56+
expectation.fulfill()
57+
})
58+
59+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
3960
}
4061

41-
func testListIndexes() {
42-
let expectation = expectationWithDescription("List indexes")
43-
client.listIndexes { (JSON, error) -> Void in
44-
expectation.fulfill()
45-
XCTAssertNil(error, error?.localizedDescription ?? "Error")
46-
}
62+
func testAdd() {
63+
let expectation = expectationWithDescription("testAdd")
64+
let object = ["city": "San Francisco", "objectID": "a/go/?à"]
65+
66+
index.addObject(object, block: { (JSON, error) -> Void in
67+
if let error = error {
68+
XCTFail("Error during addObject: \(error)")
69+
expectation.fulfill()
70+
} else {
71+
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
72+
if let error = error {
73+
XCTFail("Error during waitTask: \(error)")
74+
expectation.fulfill()
75+
} else {
76+
XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed")
77+
78+
self.index.search(Query(), block: { (JSON, error) -> Void in
79+
if let error = error {
80+
XCTFail("Error during search: \(error)")
81+
} else {
82+
let nbHits = JSON!["nbHits"] as Int
83+
XCTAssertEqual(nbHits, 1, "Wrong number of object in the index")
84+
}
85+
86+
expectation.fulfill()
87+
})
88+
}
89+
})
90+
}
91+
})
92+
93+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
94+
}
95+
96+
func testAddWithObjectID() {
97+
let expectation = expectationWithDescription("testAddWithObjectID")
98+
let object = ["city": "San José"]
99+
let objectID = "a/go/?à-2"
100+
101+
index.addObject(object, withID: objectID, block: { (JSON, error) -> Void in
102+
if let error = error {
103+
XCTFail("Error during addObject: \(error)")
104+
expectation.fulfill()
105+
} else {
106+
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
107+
if let error = error {
108+
XCTFail("Error during waitTask: \(error)")
109+
expectation.fulfill()
110+
} else {
111+
XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed")
112+
113+
self.index.getObject(objectID, block: { (JSON, error) -> Void in
114+
if let error = error {
115+
XCTFail("Error during getObject: \(error)")
116+
} else {
117+
let city = JSON!["city"] as String
118+
XCTAssertEqual(city, object["city"]!, "Get object return a bad object")
119+
}
120+
121+
expectation.fulfill()
122+
})
123+
}
124+
})
125+
}
126+
})
127+
128+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
129+
}
130+
131+
func testDelete() {
132+
let expectation = expectationWithDescription("testDelete")
133+
let object = ["city": "Las Vegas", "objectID": "a/go/?à-3"]
134+
135+
index.addObject(object, block: { (JSON, error) -> Void in
136+
if let error = error {
137+
XCTFail("Error during addObject: \(error)")
138+
expectation.fulfill()
139+
} else {
140+
self.index.deleteObject(object["objectID"]!, block: { (JSON, error) -> Void in
141+
if let error = error {
142+
XCTFail("Error during deleteObject: \(error)")
143+
expectation.fulfill()
144+
} else {
145+
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
146+
if let error = error {
147+
XCTFail("Error during waitTask: \(error)")
148+
expectation.fulfill()
149+
} else {
150+
XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed")
151+
152+
self.index.search(Query(), block: { (JSON, error) -> Void in
153+
if let error = error {
154+
XCTFail("Error during search")
155+
} else {
156+
let nbHits = JSON!["nbHits"] as Int
157+
XCTAssertEqual(nbHits, 0, "Wrong number of object in the index")
158+
}
159+
160+
expectation.fulfill()
161+
})
162+
}
163+
})
164+
}
165+
})
166+
}
167+
})
168+
169+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
170+
}
171+
172+
func testGet() {
173+
let expectation = expectationWithDescription("testGet")
174+
let object = ["city": "Los Angeles", "objectID": "a/go/?à-4"]
175+
176+
index.addObject(object, block: { (JSON, error) -> Void in
177+
if let error = error {
178+
XCTFail("Error during addObject: \(error)")
179+
expectation.fulfill()
180+
} else {
181+
self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in
182+
if let error = error {
183+
XCTFail("Error during waitTask: \(error)")
184+
expectation.fulfill()
185+
} else {
186+
XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed")
187+
188+
self.index.getObject(object["objectID"]!, block: { (JSON, error) -> Void in
189+
if let error = error {
190+
XCTFail("Error during getObject: \(error)")
191+
} else {
192+
let city = JSON!["city"] as String
193+
XCTAssertEqual(city, object["city"]!, "Get object return a bad object")
194+
}
195+
196+
expectation.fulfill()
197+
})
198+
}
199+
})
200+
}
201+
})
47202

48-
waitForExpectationsWithTimeout(100, handler: nil)
203+
waitForExpectationsWithTimeout(expectationTimeout, handler: nil)
49204
}
50-
}
205+
}

0 commit comments

Comments
 (0)