|
| 1 | +// |
| 2 | +// Copyright (c) 2015 Algolia |
| 3 | +// http://www.algolia.com/ |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in |
| 13 | +// all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +// THE SOFTWARE. |
| 22 | +// |
| 23 | + |
| 24 | +import XCTest |
| 25 | +import AlgoliaSearch |
| 26 | +import Alamofire |
| 27 | + |
| 28 | +class ClientTests: XCTestCase { |
| 29 | + let expectationTimeout: NSTimeInterval = 100 |
| 30 | + |
| 31 | + var client: Client! |
| 32 | + var index: Index! |
| 33 | + |
| 34 | + override func setUp() { |
| 35 | + super.setUp() |
| 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) |
| 48 | + } |
| 49 | + |
| 50 | + override func tearDown() { |
| 51 | + 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) |
| 60 | + } |
| 61 | + |
| 62 | + func testListIndexes() { |
| 63 | + let expectation = expectationWithDescription("testListIndexes") |
| 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.client.listIndexes({ (JSON, error) -> Void in |
| 79 | + if let error = error { |
| 80 | + XCTFail("Error during listIndexes: \(error)") |
| 81 | + } else { |
| 82 | + let items = JSON!["items"] as [[String: AnyObject]] |
| 83 | + |
| 84 | + var find = false |
| 85 | + for item in items { |
| 86 | + if (item["name"] as String) == self.index.indexName { |
| 87 | + find = true |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + XCTAssertTrue(find, "List indexes failed") |
| 92 | + } |
| 93 | + |
| 94 | + expectation.fulfill() |
| 95 | + }) |
| 96 | + } |
| 97 | + }) |
| 98 | + } |
| 99 | + }) |
| 100 | + |
| 101 | + waitForExpectationsWithTimeout(expectationTimeout, handler: nil) |
| 102 | + } |
| 103 | + |
| 104 | + func testMoveIndex() { |
| 105 | + let expecation = expectationWithDescription("testMoveIndex") |
| 106 | + let object = ["city": "San Francisco", "objectID": "a/go/?à"] |
| 107 | + |
| 108 | + index.addObject(object, block: { (JSON, error) -> Void in |
| 109 | + if let error = error { |
| 110 | + XCTFail("Error during addObject: \(error)") |
| 111 | + expecation.fulfill() |
| 112 | + } else { |
| 113 | + self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in |
| 114 | + if let error = error { |
| 115 | + XCTFail("Error during waitTask: \(error)") |
| 116 | + expecation.fulfill() |
| 117 | + } else { |
| 118 | + XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed") |
| 119 | + |
| 120 | + self.client.moveIndex(self.index.indexName, dstIndexName: "algol?à-swift2", block: { (JSON, error) -> Void in |
| 121 | + if let error = error { |
| 122 | + XCTFail("Error during moveIndex: \(error)") |
| 123 | + expecation.fulfill() |
| 124 | + } else { |
| 125 | + self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in |
| 126 | + if let error = error { |
| 127 | + XCTFail("Error during waitTask: \(error)") |
| 128 | + expecation.fulfill() |
| 129 | + } else { |
| 130 | + XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed") |
| 131 | + |
| 132 | + let dstIndex = self.client.getIndex("algol?à-swift2") |
| 133 | + dstIndex.search(Query(), block: { (JSON, error) -> Void in |
| 134 | + if let error = error { |
| 135 | + XCTFail("Error during search: \(error)") |
| 136 | + } else { |
| 137 | + let nbHits = JSON!["nbHits"] as Int |
| 138 | + XCTAssertEqual(nbHits, 1, "Wrong number of object in the index") |
| 139 | + } |
| 140 | + |
| 141 | + expecation.fulfill() |
| 142 | + }) |
| 143 | + } |
| 144 | + }) |
| 145 | + } |
| 146 | + }) |
| 147 | + } |
| 148 | + }) |
| 149 | + } |
| 150 | + }) |
| 151 | + |
| 152 | + waitForExpectationsWithTimeout(expectationTimeout, handler: nil) |
| 153 | + |
| 154 | + let deleteExpectation = expectationWithDescription("Delete index") |
| 155 | + client.deleteIndex("algol?à-swift2", block: { (JSON, error) -> Void in |
| 156 | + XCTAssertNil(error, "Error during deleteIndex: \(error?.description)") |
| 157 | + deleteExpectation.fulfill() |
| 158 | + }) |
| 159 | + |
| 160 | + waitForExpectationsWithTimeout(expectationTimeout, handler: nil) |
| 161 | + } |
| 162 | + |
| 163 | + func testCopyIndex() { |
| 164 | + let expecation = expectationWithDescription("testCopyIndex") |
| 165 | + let srcIndexExpectation = expectationWithDescription("srcIndex") |
| 166 | + let dstIndexExpectation = expectationWithDescription("dstIndex") |
| 167 | + |
| 168 | + let object = ["city": "San Francisco", "objectID": "a/go/?à"] |
| 169 | + |
| 170 | + index.addObject(object, block: { (JSON, error) -> Void in |
| 171 | + if let error = error { |
| 172 | + XCTFail("Error during addObject: \(error)") |
| 173 | + expecation.fulfill() |
| 174 | + } else { |
| 175 | + self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in |
| 176 | + if let error = error { |
| 177 | + XCTFail("Error during waitTask: \(error)") |
| 178 | + expecation.fulfill() |
| 179 | + } else { |
| 180 | + XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed") |
| 181 | + |
| 182 | + self.client.copyIndex(self.index.indexName, dstIndexName: "algol?à-swift2", block: { (JSON, error) -> Void in |
| 183 | + if let error = error { |
| 184 | + XCTFail("Error during copyIndex: \(error)") |
| 185 | + expecation.fulfill() |
| 186 | + } else { |
| 187 | + self.index.waitTask(JSON!["taskID"] as Int, block: { (JSON, error) -> Void in |
| 188 | + if let error = error { |
| 189 | + XCTFail("Error during waitTask: \(error)") |
| 190 | + expecation.fulfill() |
| 191 | + } else { |
| 192 | + XCTAssertEqual(JSON!["status"] as String, "published", "Wait task failed") |
| 193 | + expecation.fulfill() |
| 194 | + |
| 195 | + self.index.search(Query(), block: { (JSON, error) -> Void in |
| 196 | + if let error = error { |
| 197 | + XCTFail("Error during search: \(error)") |
| 198 | + } else { |
| 199 | + let nbHits = JSON!["nbHits"] as Int |
| 200 | + XCTAssertEqual(nbHits, 1, "Wrong number of object in the index") |
| 201 | + } |
| 202 | + |
| 203 | + srcIndexExpectation.fulfill() |
| 204 | + }) |
| 205 | + |
| 206 | + let dstIndex = self.client.getIndex("algol?à-swift2") |
| 207 | + dstIndex.search(Query(), block: { (JSON, error) -> Void in |
| 208 | + if let error = error { |
| 209 | + XCTFail("Error during search: \(error)") |
| 210 | + } else { |
| 211 | + let nbHits = JSON!["nbHits"] as Int |
| 212 | + XCTAssertEqual(nbHits, 1, "Wrong number of object in the index") |
| 213 | + } |
| 214 | + |
| 215 | + dstIndexExpectation.fulfill() |
| 216 | + }) |
| 217 | + } |
| 218 | + }) |
| 219 | + } |
| 220 | + }) |
| 221 | + } |
| 222 | + }) |
| 223 | + } |
| 224 | + }) |
| 225 | + |
| 226 | + waitForExpectationsWithTimeout(expectationTimeout, handler: nil) |
| 227 | + |
| 228 | + let deleteExpectation = expectationWithDescription("Delete index") |
| 229 | + client.deleteIndex("algol?à-swift2", block: { (JSON, error) -> Void in |
| 230 | + XCTAssertNil(error, "Error during deleteIndex: \(error?.description)") |
| 231 | + deleteExpectation.fulfill() |
| 232 | + }) |
| 233 | + |
| 234 | + waitForExpectationsWithTimeout(expectationTimeout, handler: nil) |
| 235 | + } |
| 236 | +} |
0 commit comments