2019-10-18 23:21:02 +02:00
|
|
|
//
|
|
|
|
// FeedlySyncStreamContentsOperationTests.swift
|
|
|
|
// AccountTests
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 26/10/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import Account
|
2020-01-16 07:10:06 +01:00
|
|
|
import RSCore
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
class FeedlySyncStreamContentsOperationTests: XCTestCase {
|
|
|
|
|
|
|
|
private var account: Account!
|
|
|
|
private let support = FeedlyTestSupport()
|
|
|
|
|
|
|
|
override func setUp() {
|
|
|
|
super.setUp()
|
|
|
|
account = support.makeTestAccount()
|
|
|
|
}
|
|
|
|
|
|
|
|
override func tearDown() {
|
|
|
|
if let account = account {
|
|
|
|
support.destroy(account)
|
|
|
|
}
|
|
|
|
super.tearDown()
|
|
|
|
}
|
|
|
|
|
2020-01-01 02:08:29 +01:00
|
|
|
func testIngestsOnePageSuccess() throws {
|
2019-10-18 23:21:02 +02:00
|
|
|
let service = TestGetStreamContentsService()
|
|
|
|
let resource = FeedlyCategoryResourceId(id: "user/1234/category/5678")
|
|
|
|
let newerThan: Date? = Date(timeIntervalSinceReferenceDate: 0)
|
|
|
|
let items = service.makeMockFeedlyEntryItem()
|
|
|
|
service.mockResult = .success(FeedlyStream(id: resource.id, updated: nil, continuation: nil, items: items))
|
|
|
|
|
|
|
|
let getStreamContentsExpectation = expectation(description: "Did Get Page of Stream Contents")
|
|
|
|
getStreamContentsExpectation.expectedFulfillmentCount = 1
|
|
|
|
|
|
|
|
service.getStreamContentsExpectation = getStreamContentsExpectation
|
|
|
|
service.parameterTester = { serviceResource, continuation, serviceNewerThan, serviceUnreadOnly in
|
|
|
|
XCTAssertEqual(serviceResource.id, resource.id)
|
|
|
|
XCTAssertEqual(serviceNewerThan, newerThan)
|
|
|
|
XCTAssertNil(continuation)
|
|
|
|
XCTAssertNil(serviceUnreadOnly)
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:16:52 +01:00
|
|
|
let syncStreamContents = FeedlySyncStreamContentsOperation(account: account, resource: resource, service: service, isPagingEnabled: true, newerThan: newerThan, log: support.log)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
let completionExpectation = expectation(description: "Did Finish")
|
2020-01-16 07:10:06 +01:00
|
|
|
syncStreamContents.completionBlock = { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
completionExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:43:29 +02:00
|
|
|
MainThreadOperationQueue.shared.add(syncStreamContents)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 2)
|
|
|
|
|
|
|
|
let expectedArticleIds = Set(items.map { $0.id })
|
2020-01-01 02:08:29 +01:00
|
|
|
let expectedArticles = try account.fetchArticles(.articleIDs(expectedArticleIds))
|
2019-10-18 23:21:02 +02:00
|
|
|
XCTAssertEqual(expectedArticles.count, expectedArticleIds.count, "Did not fetch all the articles.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testIngestsOnePageFailure() {
|
|
|
|
let service = TestGetStreamContentsService()
|
|
|
|
let resource = FeedlyCategoryResourceId(id: "user/1234/category/5678")
|
|
|
|
let newerThan: Date? = Date(timeIntervalSinceReferenceDate: 0)
|
|
|
|
|
|
|
|
service.mockResult = .failure(URLError(.timedOut))
|
|
|
|
|
|
|
|
let getStreamContentsExpectation = expectation(description: "Did Get Page of Stream Contents")
|
|
|
|
getStreamContentsExpectation.expectedFulfillmentCount = 1
|
|
|
|
|
|
|
|
service.getStreamContentsExpectation = getStreamContentsExpectation
|
|
|
|
service.parameterTester = { serviceResource, continuation, serviceNewerThan, serviceUnreadOnly in
|
|
|
|
XCTAssertEqual(serviceResource.id, resource.id)
|
|
|
|
XCTAssertEqual(serviceNewerThan, newerThan)
|
|
|
|
XCTAssertNil(continuation)
|
|
|
|
XCTAssertNil(serviceUnreadOnly)
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:16:52 +01:00
|
|
|
let syncStreamContents = FeedlySyncStreamContentsOperation(account: account, resource: resource, service: service, isPagingEnabled: true, newerThan: newerThan, log: support.log)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
let completionExpectation = expectation(description: "Did Finish")
|
2020-01-16 07:10:06 +01:00
|
|
|
syncStreamContents.completionBlock = { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
completionExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:43:29 +02:00
|
|
|
MainThreadOperationQueue.shared.add(syncStreamContents)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 2)
|
|
|
|
}
|
|
|
|
|
2020-01-01 02:08:29 +01:00
|
|
|
func testIngestsManyPagesSuccess() throws {
|
2019-10-18 23:21:02 +02:00
|
|
|
let service = TestGetPagedStreamContentsService()
|
|
|
|
let resource = FeedlyCategoryResourceId(id: "user/1234/category/5678")
|
|
|
|
let newerThan: Date? = Date(timeIntervalSinceReferenceDate: 0)
|
|
|
|
|
|
|
|
let continuations = (1...10).map { "\($0)" }
|
|
|
|
service.addAtLeastOnePage(for: resource, continuations: continuations, numberOfEntriesPerPage: 1000)
|
|
|
|
|
|
|
|
let getStreamContentsExpectation = expectation(description: "Did Get Page of Stream Contents")
|
|
|
|
getStreamContentsExpectation.expectedFulfillmentCount = 1 + continuations.count
|
|
|
|
|
|
|
|
var remainingContinuations = Set(continuations)
|
|
|
|
let getStreamPageExpectation = expectation(description: "Did Request Page")
|
|
|
|
getStreamPageExpectation.expectedFulfillmentCount = 1 + continuations.count
|
|
|
|
|
|
|
|
service.getStreamContentsExpectation = getStreamContentsExpectation
|
|
|
|
service.parameterTester = { serviceResource, continuation, serviceNewerThan, serviceUnreadOnly in
|
|
|
|
XCTAssertEqual(serviceResource.id, resource.id)
|
|
|
|
XCTAssertEqual(serviceNewerThan, newerThan)
|
|
|
|
XCTAssertNil(serviceUnreadOnly)
|
|
|
|
|
|
|
|
if let continuation = continuation {
|
|
|
|
XCTAssertTrue(remainingContinuations.contains(continuation))
|
|
|
|
remainingContinuations.remove(continuation)
|
|
|
|
}
|
|
|
|
|
|
|
|
getStreamPageExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-01-17 07:16:52 +01:00
|
|
|
let syncStreamContents = FeedlySyncStreamContentsOperation(account: account, resource: resource, service: service, isPagingEnabled: true, newerThan: newerThan, log: support.log)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
let completionExpectation = expectation(description: "Did Finish")
|
2020-01-16 07:10:06 +01:00
|
|
|
syncStreamContents.completionBlock = { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
completionExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:43:29 +02:00
|
|
|
MainThreadOperationQueue.shared.add(syncStreamContents)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 30)
|
|
|
|
|
|
|
|
// Find articles inserted.
|
|
|
|
let articleIds = Set(service.pages.values.map { $0.items }.flatMap { $0 }.map { $0.id })
|
2020-01-01 02:08:29 +01:00
|
|
|
let articles = try account.fetchArticles(.articleIDs(articleIds))
|
2019-10-18 23:21:02 +02:00
|
|
|
XCTAssertEqual(articleIds.count, articles.count)
|
|
|
|
}
|
|
|
|
}
|