@ -1,11 +1,92 @@
//
//
// C o p y r i g h t ( c ) 2 0 1 7 O p e n W h i s p e r S y s t e m s . A l l r i g h t s r e s e r v e d .
// C o p y r i g h t ( c ) 2 0 1 8 O p e n W h i s p e r S y s t e m s . A l l r i g h t s r e s e r v e d .
//
//
import XCTest
import XCTest
@ testable import Signal
@ testable import Signal
@ testable import SignalMessaging
@ testable import SignalMessaging
class ConversationSearcherTest : XCTestCase {
// M a r k : D e p e n d e n c i e s
var searcher : ConversationSearcher {
return ConversationSearcher . shared
}
var dbConnection : YapDatabaseConnection {
return OWSPrimaryStorage . shared ( ) . dbReadWriteConnection
}
// M a r k : T e s t L i f e C y c l e
override func setUp ( ) {
super . setUp ( )
ConversationFullTextSearchFinder . syncRegisterDatabaseExtension ( storage : OWSPrimaryStorage . shared ( ) )
}
// M a r k : T e s t s
func testSearchByGroupName ( ) {
TSGroupThread . removeAllObjectsInCollection ( )
var bookClubThread : ThreadViewModel !
var snackClubThread : ThreadViewModel !
self . dbConnection . readWrite { transaction in
let bookModel = TSGroupModel ( title : " Book Club " , memberIds : [ ] , image : nil , groupId : Randomness . generateRandomBytes ( 16 ) )
let bookClubGroupThread = TSGroupThread . getOrCreateThread ( with : bookModel , transaction : transaction )
bookClubThread = ThreadViewModel ( thread : bookClubGroupThread , transaction : transaction )
let snackModel = TSGroupModel ( title : " Snack Club " , memberIds : [ ] , image : nil , groupId : Randomness . generateRandomBytes ( 16 ) )
let snackClubGroupThread = TSGroupThread . getOrCreateThread ( with : snackModel , transaction : transaction )
snackClubThread = ThreadViewModel ( thread : snackClubGroupThread , transaction : transaction )
}
// N o M a t c h
let noMatch = results ( searchText : " asdasdasd " )
XCTAssert ( noMatch . conversations . isEmpty )
// P a r t i a l M a t c h
let bookMatch = results ( searchText : " Book " )
XCTAssert ( bookMatch . conversations . count = = 1 )
if let foundThread : ThreadViewModel = bookMatch . conversations . first ? . thread {
XCTAssertEqual ( bookClubThread , foundThread )
} else {
XCTFail ( " no thread found " )
}
let snackMatch = results ( searchText : " Snack " )
XCTAssert ( snackMatch . conversations . count = = 1 )
if let foundThread : ThreadViewModel = snackMatch . conversations . first ? . thread {
XCTAssertEqual ( snackClubThread , foundThread )
} else {
XCTFail ( " no thread found " )
}
// M u l t i p l e P a r t i a l M a t c h e s
let multipleMatch = results ( searchText : " Club " )
XCTAssert ( multipleMatch . conversations . count = = 2 )
XCTAssert ( multipleMatch . conversations . map { $0 . thread } . contains ( bookClubThread ) )
XCTAssert ( multipleMatch . conversations . map { $0 . thread } . contains ( snackClubThread ) )
// M a t c h N a m e E x a c t l y
let exactMatch = results ( searchText : " Book Club " )
XCTAssert ( exactMatch . conversations . count = = 1 )
XCTAssertEqual ( bookClubThread , exactMatch . conversations . first ! . thread )
}
// M a r k : H e l p e r s
private func results ( searchText : String ) -> ConversationSearchResults {
var results : ConversationSearchResults !
self . dbConnection . read { transaction in
results = self . searcher . results ( searchText : searchText , transaction : transaction )
}
return results
}
}
class SearcherTest : XCTestCase {
class SearcherTest : XCTestCase {
struct TestCharacter {
struct TestCharacter {
@ -62,18 +143,18 @@ class SearcherTest: XCTestCase {
}
}
func testFormattingChars ( ) {
func testFormattingChars ( ) {
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " 323 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " 323 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " 1-323-555-5555 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " 1-323-555-5555 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " 13235555555 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " 13235555555 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " +1-323 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " +1-323 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " Liza +1-323 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " Liza +1-323 " ) )
// S a n i t y c h e c k , m a t c h b o t h b y n a m e s
// S a n i t y c h e c k , m a t c h b o t h b y n a m e s
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " Liza " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " Liza " ) )
XCTAssert ( searcher . matches ( item : regularLizaveta , query : " Liza " ) )
XCTAssert ( searcher . matches ( item : regularLizaveta , query : " Liza " ) )
// D i s a m b i g u a t e t h e t w o L i z a ' s b y a r e a c o d e
// D i s a m b i g u a t e t h e t w o L i z a ' s b y a r e a c o d e
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " Liza 323 " ) )
XCTAssert ( searcher . matches ( item : stinkingLizaveta , query : " Liza 323 " ) )
XCTAssertFalse ( searcher . matches ( item : regularLizaveta , query : " Liza 323 " ) )
XCTAssertFalse ( searcher . matches ( item : regularLizaveta , query : " Liza 323 " ) )
}
}
}
}