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 C o n t e n t 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 8 / 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 TestGetPagedStreamContentsService : FeedlyGetStreamContentsService {
var parameterTester : ( ( FeedlyResourceId , String ? , Date ? , Bool ? ) -> ( ) ) ?
var getStreamContentsExpectation : XCTestExpectation ?
var pages = [ String : FeedlyStream ] ( )
func addAtLeastOnePage ( for resource : FeedlyResourceId , continuations : [ String ] , numberOfEntriesPerPage count : Int ) {
pages = [ String : FeedlyStream ] ( 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 = makeStreamContents ( for : resource , continuation : continuation , between : 0. . < count )
let key = TestGetPagedStreamContentsService . getPagingKey ( for : resource , continuation : index < 0 ? nil : continuations [ index ] )
pages [ key ] = page
}
}
private func makeStreamContents ( for resource : FeedlyResourceId , continuation : String ? , between range : Range < Int > ) -> FeedlyStream {
let entries = range . map { index -> FeedlyEntry in
let content = FeedlyEntry . Content ( content : " Content \( index ) " ,
direction : . leftToRight )
let origin = FeedlyOrigin ( title : " Origin \( index ) " ,
streamId : resource . id ,
htmlUrl : " http://localhost/feedly/origin/ \( index ) " )
return FeedlyEntry ( id : " /articles/ \( index ) " ,
title : " Article \( index ) " ,
content : content ,
summary : content ,
author : nil ,
2019-12-05 08:54:47 +01:00
crawled : Date ( ) ,
recrawled : nil ,
2019-10-18 23:21:02 +02:00
origin : origin ,
canonical : nil ,
alternate : nil ,
unread : true ,
tags : nil ,
categories : nil ,
enclosure : nil )
}
let stream = FeedlyStream ( id : resource . id , updated : nil , continuation : continuation , items : entries )
return stream
}
static func getPagingKey ( for stream : FeedlyResourceId , continuation : String ? ) -> String {
return " \( stream . id ) @ \( continuation ? ? " " ) "
}
2019-12-15 01:14:55 +01:00
func getStreamContents ( for resource : FeedlyResourceId , continuation : String ? , newerThan : Date ? , unreadOnly : Bool ? , completion : @ escaping ( Result < FeedlyStream , Error > ) -> ( ) ) {
2019-10-18 23:21:02 +02:00
let key = TestGetPagedStreamContentsService . 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 . getStreamContentsExpectation ? . fulfill ( )
}
}
}