2019-10-18 23:21:02 +02:00
|
|
|
//
|
|
|
|
// FeedlyGetCollectionsOperationTests.swift
|
|
|
|
// AccountTests
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 21/10/19.
|
|
|
|
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import XCTest
|
|
|
|
@testable import Account
|
|
|
|
import os.log
|
2020-01-16 07:10:06 +01:00
|
|
|
import RSCore
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
class FeedlyGetCollectionsOperationTests: XCTestCase {
|
|
|
|
|
|
|
|
func testGetCollections() {
|
|
|
|
let support = FeedlyTestSupport()
|
|
|
|
let (transport, caller) = support.makeMockNetworkStack()
|
2020-08-04 19:43:29 +02:00
|
|
|
let jsonName = "JSON/feedly_collections_initial"
|
2019-10-18 23:21:02 +02:00
|
|
|
transport.testFiles["/v3/collections"] = "\(jsonName).json"
|
|
|
|
|
|
|
|
let getCollections = FeedlyGetCollectionsOperation(service: caller, log: support.log)
|
|
|
|
let completionExpectation = expectation(description: "Did Finish")
|
2020-01-16 07:10:06 +01:00
|
|
|
getCollections.completionBlock = { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
completionExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-02-06 07:37:43 +01:00
|
|
|
MainThreadOperationQueue.shared.add(getCollections)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 2)
|
|
|
|
|
|
|
|
let collections = support.testJSON(named: jsonName) as! [[String:Any]]
|
|
|
|
let labelsInJSON = Set(collections.map { $0["label"] as! String })
|
|
|
|
let idsInJSON = Set(collections.map { $0["id"] as! String })
|
|
|
|
|
|
|
|
let labels = Set(getCollections.collections.map { $0.label })
|
|
|
|
let ids = Set(getCollections.collections.map { $0.id })
|
|
|
|
|
|
|
|
let missingLabels = labelsInJSON.subtracting(labels)
|
|
|
|
let missingIds = idsInJSON.subtracting(ids)
|
|
|
|
|
|
|
|
XCTAssertEqual(getCollections.collections.count, collections.count, "Mismatch between collections provided by operation and test JSON collections.")
|
|
|
|
XCTAssertTrue(missingLabels.isEmpty, "Collections with these labels did not have a corresponding \(FeedlyCollection.self) value with the same name.")
|
|
|
|
XCTAssertTrue(missingIds.isEmpty, "Collections with these ids did not have a corresponding \(FeedlyCollection.self) with the same id.")
|
|
|
|
|
|
|
|
for collection in collections {
|
|
|
|
let collectionId = collection["id"] as! String
|
|
|
|
let collectionFeeds = collection["feeds"] as! [[String: Any]]
|
|
|
|
let collectionFeedIds = Set(collectionFeeds.map { $0["id"] as! String })
|
|
|
|
|
|
|
|
for operationCollection in getCollections.collections where operationCollection.id == collectionId {
|
|
|
|
let feedIds = Set(operationCollection.feeds.map { $0.id })
|
|
|
|
let missingIds = collectionFeedIds.subtracting(feedIds)
|
|
|
|
XCTAssertTrue(missingIds.isEmpty, "Feeds with these ids were not found in the \"\(operationCollection.label)\" \(FeedlyCollection.self).")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testGetCollectionsError() {
|
|
|
|
|
|
|
|
class TestDelegate: FeedlyOperationDelegate {
|
|
|
|
var errorExpectation: XCTestExpectation?
|
|
|
|
var error: Error?
|
|
|
|
|
|
|
|
func feedlyOperation(_ operation: FeedlyOperation, didFailWith error: Error) {
|
|
|
|
self.error = error
|
|
|
|
errorExpectation?.fulfill()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let delegate = TestDelegate()
|
|
|
|
delegate.errorExpectation = expectation(description: "Did Fail With Expected Error")
|
|
|
|
|
|
|
|
let support = FeedlyTestSupport()
|
|
|
|
let service = TestGetCollectionsService()
|
|
|
|
service.mockResult = .failure(URLError(.timedOut))
|
|
|
|
|
|
|
|
let getCollections = FeedlyGetCollectionsOperation(service: service, log: support.log)
|
|
|
|
getCollections.delegate = delegate
|
|
|
|
|
|
|
|
let completionExpectation = expectation(description: "Did Finish")
|
2020-01-16 07:10:06 +01:00
|
|
|
getCollections.completionBlock = { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
completionExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-02-06 07:37:43 +01:00
|
|
|
MainThreadOperationQueue.shared.add(getCollections)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 2)
|
|
|
|
|
|
|
|
XCTAssertNotNil(delegate.error)
|
|
|
|
XCTAssertTrue(getCollections.collections.isEmpty, "Collections should be empty.")
|
|
|
|
}
|
|
|
|
}
|