@@ -26,25 +26,180 @@ import AlgoliaSearch
2626import Alamofire
2727
2828class 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