2019-10-18 23:21:02 +02:00
//
// T e s t G e t P a g e d S t r e a m I d s S e r v i c e . s w i f t
// A c c o u n t T e s t s
//
// C r e a t e d b y K i e l G i l l a r d o n 2 9 / 1 0 / 1 9 .
// C o p y r i g h t © 2 0 1 9 R a n c h e r o S o f t w a r e , L L C . A l l r i g h t s r e s e r v e d .
//
import XCTest
@ testable import Account
final class TestGetPagedStreamIdsService : FeedlyGetStreamIdsService {
var parameterTester : ( ( FeedlyResourceId , String ? , Date ? , Bool ? ) -> ( ) ) ?
var getStreamIdsExpectation : XCTestExpectation ?
var pages = [ String : FeedlyStreamIds ] ( )
func addAtLeastOnePage ( for resource : FeedlyResourceId , continuations : [ String ] , numberOfEntriesPerPage count : Int ) {
pages = [ String : FeedlyStreamIds ] ( minimumCapacity : continuations . count + 1 )
// A c o n t i n u a t i o n i s a n i d e n t i f i e r f o r t h e n e x t p a g e .
// T h e f i r s t p a g e h a s a n i l i d e n t i f i e r .
// T h e l a s t p a g e h a s n o n e x t p a g e , s o t h e n e x t c o n t i n u a t i o n v a l u e f o r t h a t p a g e i s n i l .
// T h e r e f o r e , e a c h p a g e n e e d s t o k n o w t h e i d e n t i f i e r o f t h e n e x t p a g e .
for index in - 1. . < continuations . count {
let nextIndex = index + 1
let continuation : String ? = nextIndex < continuations . count ? continuations [ nextIndex ] : nil
let page = makeStreamIds ( for : resource , continuation : continuation , between : 0. . < count )
let key = TestGetPagedStreamIdsService . getPagingKey ( for : resource , continuation : index < 0 ? nil : continuations [ index ] )
pages [ key ] = page
}
}
private func makeStreamIds ( for resource : FeedlyResourceId , continuation : String ? , between range : Range < Int > ) -> FeedlyStreamIds {
let entryIds = range . map { _ in UUID ( ) . uuidString }
let stream = FeedlyStreamIds ( continuation : continuation , ids : entryIds )
return stream
}
static func getPagingKey ( for stream : FeedlyResourceId , continuation : String ? ) -> String {
return " \( stream . id ) @ \( continuation ? ? " " ) "
}
2019-12-15 01:14:55 +01:00
func getStreamIds ( for resource : FeedlyResourceId , continuation : String ? , newerThan : Date ? , unreadOnly : Bool ? , completion : @ escaping ( Result < FeedlyStreamIds , Error > ) -> ( ) ) {
2019-10-18 23:21:02 +02:00
let key = TestGetPagedStreamIdsService . getPagingKey ( for : resource , continuation : continuation )
guard let page = pages [ key ] else {
XCTFail ( " Missing page for \( resource . id ) and continuation \( String ( describing : continuation ) ) . Test may time out because the completion will not be called. " )
return
}
parameterTester ? ( resource , continuation , newerThan , unreadOnly )
DispatchQueue . main . async {
2019-12-15 01:14:55 +01:00
completion ( . success ( page ) )
2019-10-18 23:21:02 +02:00
self . getStreamIdsExpectation ? . fulfill ( )
}
}
}