@ -8,7 +8,7 @@ import XCTest
class ConversationSearcherTest : XCTestCase {
class ConversationSearcherTest : XCTestCase {
// M a r k : D e p e n d e n c i e s
// MARK: - D e p e n d e n c i e s
var searcher : ConversationSearcher {
var searcher : ConversationSearcher {
return ConversationSearcher . shared
return ConversationSearcher . shared
}
}
@ -17,68 +17,152 @@ class ConversationSearcherTest: XCTestCase {
return OWSPrimaryStorage . shared ( ) . dbReadWriteConnection
return OWSPrimaryStorage . shared ( ) . dbReadWriteConnection
}
}
// M a r k : T e s t L i f e C y c l e
// MARK: - T e s t L i f e C y c l e
override func setUp ( ) {
override func setUp ( ) {
super . setUp ( )
super . setUp ( )
FullTextSearchFinder . syncRegisterDatabaseExtension ( storage : OWSPrimaryStorage . shared ( ) )
FullTextSearchFinder . syncRegisterDatabaseExtension ( storage : OWSPrimaryStorage . shared ( ) )
}
// M a r k : T e s t s
func testSearchByGroupName ( ) {
TSContactThread . removeAllObjectsInCollection ( )
TSGroupThread . removeAllObjectsInCollection ( )
TSGroupThread . removeAllObjectsInCollection ( )
var bookClubThread : ThreadViewModel !
var snackClubThread : ThreadViewModel !
self . dbConnection . readWrite { transaction in
self . dbConnection . readWrite { transaction in
let bookModel = TSGroupModel ( title : " Book Club " , memberIds : [ ] , image : nil , groupId : Randomness . generateRandomBytes ( 16 ) )
let bookModel = TSGroupModel ( title : " Book Club " , memberIds : [ ] , image : nil , groupId : Randomness . generateRandomBytes ( 16 ) )
let bookClubGroupThread = TSGroupThread . getOrCreateThread ( with : bookModel , transaction : transaction )
let bookClubGroupThread = TSGroupThread . getOrCreateThread ( with : bookModel , transaction : transaction )
bookClubThread = ThreadViewModel ( thread : bookClubGroupThread , transaction : transaction )
self . bookClubThread = ThreadViewModel ( thread : bookClubGroupThread , transaction : transaction )
let snackModel = TSGroupModel ( title : " Snack Club " , memberIds : [ ] , image : nil , groupId : Randomness . generateRandomBytes ( 16 ) )
let snackModel = TSGroupModel ( title : " Snack Club " , memberIds : [ ] , image : nil , groupId : Randomness . generateRandomBytes ( 16 ) )
let snackClubGroupThread = TSGroupThread . getOrCreateThread ( with : snackModel , transaction : transaction )
let snackClubGroupThread = TSGroupThread . getOrCreateThread ( with : snackModel , transaction : transaction )
snackClubThread = ThreadViewModel ( thread : snackClubGroupThread , transaction : transaction )
self . snackClubThread = ThreadViewModel ( thread : snackClubGroupThread , transaction : transaction )
let aliceContactThread = TSContactThread . getOrCreateThread ( withContactId : " +12345678900 " , transaction : transaction )
self . aliceThread = ThreadViewModel ( thread : aliceContactThread , transaction : transaction )
let bobContactThread = TSContactThread . getOrCreateThread ( withContactId : " +49030183000 " , transaction : transaction )
self . bobThread = ThreadViewModel ( thread : bobContactThread , transaction : transaction )
}
}
}
// MARK: - F i x t u r e s
var bookClubThread : ThreadViewModel !
var snackClubThread : ThreadViewModel !
var aliceThread : ThreadViewModel !
var bobThread : ThreadViewModel !
// MARK: T e s t s
func testSearchByGroupName ( ) {
var resultSet : SearchResultSet = . empty
// N o M a t c h
// N o M a t c h
let noMatch = resultSet ( searchText : " asdasdasd " )
resultSet = getR esultSet( searchText : " asdasdasd " )
XCTAssert ( noMatch . conversations . isEmpty )
XCTAssert ( resultSet . conversations . isEmpty )
// P a r t i a l M a t c h
// P a r t i a l M a t c h
let bookMatch = resultSet ( searchText : " Book " )
resultSet = getR esultSet( searchText : " Book " )
XCTAssert ( bookMatch . conversations . count = = 1 )
XCTAssert ( resultSet . conversations . count = = 1 )
if let foundThread : ThreadViewModel = bookMatch . conversations . first ? . thread {
if let foundThread : ThreadViewModel = resultSet . conversations . first ? . thread {
XCTAssertEqual ( bookClubThread , foundThread )
XCTAssertEqual ( bookClubThread , foundThread )
} else {
} else {
XCTFail ( " no thread found " )
XCTFail ( " no thread found " )
}
}
let snackMatch = resultSet ( searchText : " Snack " )
resultSet = getR esultSet( searchText : " Snack " )
XCTAssert ( snackMatch . conversations . count = = 1 )
XCTAssert ( resultSet . conversations . count = = 1 )
if let foundThread : ThreadViewModel = snackMatch . conversations . first ? . thread {
if let foundThread : ThreadViewModel = resultSet . conversations . first ? . thread {
XCTAssertEqual ( snackClubThread , foundThread )
XCTAssertEqual ( snackClubThread , foundThread )
} else {
} else {
XCTFail ( " no thread found " )
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
// M u l t i p l e P a r t i a l M a t c h e s
let multipleMatch = r esultSet( searchText : " Club " )
resultSet = getR esultSet( searchText : " Club " )
XCTAssert ( multipleMatch . conversations . count = = 2 )
XCTAssert Equal( 2 , resultSet . conversations . count )
XCTAssert ( multipleMatch . conversations . map { $0 . thread } . contains ( bookClubThread ) )
XCTAssert ( resultSet . conversations . map { $0 . thread } . contains ( bookClubThread ) )
XCTAssert ( multipleMatch . conversations . map { $0 . thread } . contains ( snackClubThread ) )
XCTAssert ( resultSet . conversations . map { $0 . thread } . contains ( snackClubThread ) )
// M a t c h N a m e E x a c t l y
// M a t c h N a m e E x a c t l y
let exactMatch = resultSet ( searchText : " Book Club " )
resultSet = getResultSet ( searchText : " Book Club " )
XCTAssert ( exactMatch . conversations . count = = 1 )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( bookClubThread , exactMatch . conversations . first ! . thread )
XCTAssertEqual ( bookClubThread , resultSet . conversations . first ! . thread )
}
func testSearchContactByNumber ( ) {
var resultSet : SearchResultSet = . empty
// N o m a t c h
resultSet = getResultSet ( searchText : " +5551239999 " )
XCTAssertEqual ( 0 , resultSet . conversations . count )
// E x a c t m a t c h
resultSet = getResultSet ( searchText : " +12345678900 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
// P a r t i a l m a t c h
resultSet = getResultSet ( searchText : " +123456 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
// P r e f i x e s
resultSet = getResultSet ( searchText : " 12345678900 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
resultSet = getResultSet ( searchText : " 49 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( bobThread , resultSet . conversations . first ? . thread )
resultSet = getResultSet ( searchText : " 1-234-56 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
resultSet = getResultSet ( searchText : " 123456 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
resultSet = getResultSet ( searchText : " 1.234.56 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
}
// T O D O
func pending_testSearchContactByNumber ( ) {
var resultSet : SearchResultSet = . empty
// P h o n e N u m b e r f o r m a t t i n g s h o u l d b e f o r g i v i n g
resultSet = getResultSet ( searchText : " 234.56 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
resultSet = getResultSet ( searchText : " 234 56 " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
}
func testSearchContactByName ( ) {
var resultSet : SearchResultSet = . empty
resultSet = getResultSet ( searchText : " Alice " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( aliceThread , resultSet . conversations . first ? . thread )
resultSet = getResultSet ( searchText : " Bob " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( bobThread , resultSet . conversations . first ? . thread )
resultSet = getResultSet ( searchText : " Barker " )
XCTAssertEqual ( 1 , resultSet . conversations . count )
XCTAssertEqual ( bobThread , resultSet . conversations . first ? . thread )
}
}
// M a r k : H e l p e r s
// M a r k : H e l p e r s
private func resultSet ( searchText : String ) -> SearchResultSet {
private func getR esultSet( searchText : String ) -> SearchResultSet {
var results : SearchResultSet !
var results : SearchResultSet !
self . dbConnection . read { transaction in
self . dbConnection . read { transaction in
results = self . searcher . results ( searchText : searchText , transaction : transaction )
results = self . searcher . results ( searchText : searchText , transaction : transaction )