2
2
import XCTest
3
3
import Foundation
4
4
5
- // swiftlint:disable force_unwrapping
6
- // swiftlint:disable force_try
7
5
private let books : [ Book ] = [
8
6
Book ( id: 123 , title: " Pride and Prejudice " , comment: " A great book " , genres: [ " Classic Regency nove " ] ) ,
9
7
Book ( id: 456 , title: " Le Petit Prince " , comment: " A french book " , genres: [ " Novel " ] ) ,
@@ -15,20 +13,31 @@ private let books: [Book] = [
15
13
Book ( id: 1844 , title: " A Moreninha " , comment: " A Book from Joaquim Manuel de Macedo " , genres: [ " Novel " ] )
16
14
]
17
15
16
+ // swiftlint:disable force_unwrapping
17
+ // swiftlint:disable force_try
18
+ private let nestedBooks : [ NestedBook ] = [
19
+ NestedBook ( id: 123 , title: " Pride and Prejudice " , info: InfoNested ( comment: " A great book " , reviewNb: 100 ) , genres: [ " Classic Regency nove " ] ) ,
20
+ NestedBook ( id: 456 , title: " Le Petit Prince " , info: InfoNested ( comment: " A french book " , reviewNb: 100 ) , genres: [ " Novel " ] ) ,
21
+ NestedBook ( id: 2 , title: " Le Rouge et le Noir " , info: InfoNested ( comment: " Another french book " , reviewNb: 100 ) , genres: [ " Bildungsroman " ] )
22
+ ]
23
+
18
24
class SearchTests : XCTestCase {
19
25
private var client : MeiliSearch !
20
26
private var index : Indexes !
27
+ private var nestedIndex : Indexes !
21
28
private var session : URLSessionProtocol !
22
29
private let uid : String = " books_test "
30
+ private let nested_uid : String = " nested_books_test "
23
31
24
32
// MARK: Setup
25
33
26
34
override func setUp( ) {
27
35
super. setUp ( )
28
36
29
- session = URLSession ( configuration: . ephemeral)
30
- client = try ! MeiliSearch ( host: " http://localhost:7700 " , apiKey: " masterKey " , session: session)
37
+ session = URLSession ( configuration: . ephemeral)
38
+ client = try ! MeiliSearch ( host: " http://localhost:7700 " , apiKey: " masterKey " , session: session)
31
39
index = self . client. index ( self . uid)
40
+ nestedIndex = self . client. index ( self . nested_uid)
32
41
33
42
let addDocExpectation = XCTestExpectation ( description: " Add documents " )
34
43
@@ -43,10 +52,21 @@ class SearchTests: XCTestCase {
43
52
}
44
53
}
45
54
self . wait ( for: [ addDocExpectation] , timeout: TESTS_TIME_OUT)
55
+ let addNestedDocExpectation = XCTestExpectation ( description: " Add documents " )
56
+ addDocuments ( client: self . client, uid: self . nested_uid, dataset: nestedBooks, primaryKey: nil ) { result in
57
+ switch result {
58
+ case . success:
59
+ addNestedDocExpectation. fulfill ( )
60
+ case . failure( let error) :
61
+ dump ( error)
62
+ XCTFail ( " Failed to create index " )
63
+ addNestedDocExpectation. fulfill ( )
64
+ }
65
+ }
66
+ self . wait ( for: [ addNestedDocExpectation] , timeout: TESTS_TIME_OUT)
46
67
}
47
68
48
69
// MARK: Basic search
49
-
50
70
func testBasicSearch( ) {
51
71
let expectation = XCTestExpectation ( description: " Search for Books with query " )
52
72
@@ -76,6 +96,32 @@ class SearchTests: XCTestCase {
76
96
self . wait ( for: [ expectation] , timeout: TESTS_TIME_OUT)
77
97
}
78
98
99
+ // MARK: Nested search
100
+ func testNestedSearch( ) {
101
+ let expectation = XCTestExpectation ( description: " Search in Nested Books " )
102
+
103
+ typealias MeiliResult = Result < SearchResult < NestedBook > , Swift . Error >
104
+ let query = " A french book "
105
+
106
+ self . nestedIndex. search ( SearchParameters ( query: query) ) { ( result: MeiliResult ) in
107
+ switch result {
108
+ case . success( let response) :
109
+ if response. hits. count > 0 {
110
+ XCTAssertEqual ( " A french book " , response. hits [ 0 ] . info. comment)
111
+ } else {
112
+ XCTFail ( " Failed to find hits in the response " )
113
+ }
114
+ expectation. fulfill ( )
115
+ case . failure( let error) :
116
+ dump ( error)
117
+ XCTFail ( " Failed to search in nested books " )
118
+ expectation. fulfill ( )
119
+ }
120
+ }
121
+
122
+ self . wait ( for: [ expectation] , timeout: TESTS_TIME_OUT)
123
+ }
124
+
79
125
func testBasicSearchWithNoQuery( ) {
80
126
let expectation = XCTestExpectation ( description: " Search for Books without query " )
81
127
@@ -87,7 +133,7 @@ class SearchTests: XCTestCase {
87
133
XCTAssertEqual ( " " , response. query)
88
134
XCTAssertEqual ( 20 , response. limit)
89
135
XCTAssertEqual ( books. count, response. hits. count)
90
- XCTAssertEqual ( " Alice In Wonderland " , response. hits [ 0 ] . title)
136
+ XCTAssertEqual ( " Pride and Prejudice " , response. hits [ 0 ] . title)
91
137
expectation. fulfill ( )
92
138
case . failure( let error) :
93
139
dump ( error)
@@ -326,7 +372,7 @@ class SearchTests: XCTestCase {
326
372
XCTAssertEqual ( documents. limit, limit)
327
373
XCTAssertEqual ( documents. hits. count, 1 )
328
374
let book : Book = documents. hits [ 0 ]
329
- XCTAssertEqual ( " Manuel de Macedo " , book. formatted!. comment!)
375
+ XCTAssertEqual ( " …Joaquim Manuel de Macedo" , book. formatted!. comment!)
330
376
expectation. fulfill ( )
331
377
case . failure( let error) :
332
378
print ( error)
@@ -338,6 +384,34 @@ class SearchTests: XCTestCase {
338
384
self . wait ( for: [ expectation] , timeout: TESTS_TIME_OUT)
339
385
}
340
386
387
+ // MARK: Crop Marker
388
+
389
+ func testSearchCropMarker( ) {
390
+ let expectation = XCTestExpectation ( description: " Search for Books with a custom crop marker " )
391
+
392
+ typealias MeiliResult = Result < SearchResult < Book > , Swift . Error >
393
+ let query = " Manuel "
394
+ let attributesToCrop = [ " comment " ]
395
+ let cropLength = 2
396
+ let cropMarker = " (ꈍᴗꈍ) "
397
+ let searchParameters = SearchParameters ( query: query, attributesToCrop: attributesToCrop, cropLength: cropLength, cropMarker: cropMarker)
398
+
399
+ self . index. search ( searchParameters) { ( result: MeiliResult ) in
400
+ switch result {
401
+ case . success( let documents) :
402
+ let book : Book = documents. hits [ 0 ]
403
+ XCTAssertEqual ( " (ꈍᴗꈍ)Joaquim Manuel(ꈍᴗꈍ) " , book. formatted!. comment!)
404
+ expectation. fulfill ( )
405
+ case . failure( let error) :
406
+ print ( error)
407
+ XCTFail ( " Failed to search with a custom crop marker " )
408
+ expectation. fulfill ( )
409
+ }
410
+ }
411
+
412
+ self . wait ( for: [ expectation] , timeout: TESTS_TIME_OUT)
413
+ }
414
+
341
415
// MARK: Crop length
342
416
343
417
func testSearchCropLength( ) {
@@ -357,7 +431,7 @@ class SearchTests: XCTestCase {
357
431
XCTAssertEqual ( documents. hits. count, 2 )
358
432
359
433
let moreninhaBook : Book = documents. hits. first ( where: { book in book. id == 1844 } ) !
360
- XCTAssertEqual ( " A Book from " , moreninhaBook. formatted!. comment!)
434
+ XCTAssertEqual ( " A Book from Joaquim Manuel… " , moreninhaBook. formatted!. comment!)
361
435
expectation. fulfill ( )
362
436
case . failure( let error) :
363
437
dump ( error)
@@ -438,6 +512,34 @@ class SearchTests: XCTestCase {
438
512
self . wait ( for: [ expectation] , timeout: TESTS_TIME_OUT)
439
513
}
440
514
515
+ // MARK: Attributes to highlight
516
+
517
+ func testSearchPrePostHighlightTags( ) {
518
+ let expectation = XCTestExpectation ( description: " Search for Books using custom pre and post highlight tags " )
519
+
520
+ typealias MeiliResult = Result < SearchResult < Book > , Swift . Error >
521
+ let query = " Joaquim Manuel de Macedo "
522
+ let attributesToHighlight = [ " comment " ]
523
+ let highlightPreTag = " (⊃。•́‿•̀。)⊃ "
524
+ let highlightPostTag = " ⊂(´• ω •`⊂) "
525
+ let parameters = SearchParameters ( query: query, attributesToHighlight: attributesToHighlight, highlightPreTag: highlightPreTag, highlightPostTag: highlightPostTag)
526
+
527
+ self . index. search ( parameters) { ( result: MeiliResult ) in
528
+ switch result {
529
+ case . success( let documents) :
530
+ let book = documents. hits [ 0 ]
531
+ XCTAssertTrue ( book. formatted!. comment!. contains ( " (⊃。•́‿•̀。)⊃ Joaquim ⊂(´• ω •`⊂) (⊃。•́‿•̀。)⊃ Manuel ⊂(´• ω •`⊂) (⊃。•́‿•̀。)⊃ de ⊂(´• ω •`⊂) (⊃。•́‿•̀。)⊃ Macedo ⊂(´• ω •`⊂) " ) )
532
+ expectation. fulfill ( )
533
+ case . failure( let error) :
534
+ dump ( error)
535
+ XCTFail ( " Failed to search using custom pre and post highlight tags " )
536
+ expectation. fulfill ( )
537
+ }
538
+ }
539
+
540
+ self . wait ( for: [ expectation] , timeout: TESTS_TIME_OUT)
541
+ }
542
+
441
543
// MARK: Attributes to retrieve
442
544
443
545
func testSearchAttributesToRetrieve( ) {
@@ -757,7 +859,6 @@ class SearchTests: XCTestCase {
757
859
XCTAssertEqual ( documents. query, query)
758
860
XCTAssertEqual ( documents. limit, limit)
759
861
XCTAssertEqual ( documents. hits. count, 0 )
760
-
761
862
let facetsDistribution = documents. facetsDistribution!
762
863
XCTAssertEqual ( [ " genres " : [ : ] ] , facetsDistribution)
763
864
0 commit comments