2019-10-18 23:21:02 +02:00
|
|
|
//
|
|
|
|
// FeedlyCreateFeedsForCollectionFoldersOperationTests.swift
|
|
|
|
// AccountTests
|
|
|
|
//
|
|
|
|
// Created by Kiel Gillard on 22/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 FeedlyCreateFeedsForCollectionFoldersOperationTests: 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()
|
|
|
|
}
|
|
|
|
|
|
|
|
class FeedsAndFoldersProvider: FeedlyFeedsAndFoldersProviding {
|
|
|
|
var feedsAndFolders = [([FeedlyFeed], Folder)]()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAddFeeds() {
|
|
|
|
let feedsForFolderOne = [
|
2019-12-01 21:39:05 +01:00
|
|
|
FeedlyFeed(id: "feed/1", title: "Feed One", updated: nil, website: nil),
|
|
|
|
FeedlyFeed(id: "feed/2", title: "Feed Two", updated: nil, website: nil)
|
2019-10-18 23:21:02 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
let feedsForFolderTwo = [
|
2019-12-01 21:39:05 +01:00
|
|
|
FeedlyFeed(id: "feed/1", title: "Feed One", updated: nil, website: nil),
|
|
|
|
FeedlyFeed(id: "feed/3", title: "Feed Three", updated: nil, website: nil),
|
2019-10-18 23:21:02 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
let folderOne: (name: String, id: String) = ("FolderOne", "folder/1")
|
|
|
|
let folderTwo: (name: String, id: String) = ("FolderTwo", "folder/2")
|
|
|
|
let namesAndFeeds = [(folderOne, feedsForFolderOne), (folderTwo, feedsForFolderTwo)]
|
|
|
|
|
|
|
|
let provider = FeedsAndFoldersProvider()
|
|
|
|
provider.feedsAndFolders = namesAndFeeds.map { (folder, feeds) in
|
|
|
|
let accountFolder = account.ensureFolder(with: folder.name)!
|
|
|
|
accountFolder.externalID = folder.id
|
|
|
|
return (feeds, accountFolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
let createFeeds = FeedlyCreateFeedsForCollectionFoldersOperation(account: account, feedsAndFoldersProvider: provider, log: support.log)
|
|
|
|
let completionExpectation = expectation(description: "Did Finish")
|
2020-01-16 07:10:06 +01:00
|
|
|
createFeeds.completionBlock = { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
completionExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
XCTAssertTrue(account.flattenedWebFeeds().isEmpty, "Expected empty account.")
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2020-08-04 19:43:29 +02:00
|
|
|
MainThreadOperationQueue.shared.add(createFeeds)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 2)
|
|
|
|
|
|
|
|
let feedIds = Set([feedsForFolderOne, feedsForFolderTwo]
|
|
|
|
.flatMap { $0 }
|
2019-12-01 21:39:05 +01:00
|
|
|
.map { $0.id })
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
let feedTitles = Set([feedsForFolderOne, feedsForFolderTwo]
|
|
|
|
.flatMap { $0 }
|
|
|
|
.map { $0.title })
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
let accountFeeds = account.flattenedWebFeeds()
|
|
|
|
let ingestedIds = Set(accountFeeds.map { $0.webFeedID })
|
2019-10-18 23:21:02 +02:00
|
|
|
let ingestedTitles = Set(accountFeeds.map { $0.nameForDisplay })
|
|
|
|
|
|
|
|
let missingIds = feedIds.subtracting(ingestedIds)
|
|
|
|
let missingTitles = feedTitles.subtracting(ingestedTitles)
|
|
|
|
|
|
|
|
XCTAssertTrue(missingIds.isEmpty, "Failed to ingest feeds with these ids.")
|
|
|
|
XCTAssertTrue(missingTitles.isEmpty, "Failed to ingest feeds with these titles.")
|
|
|
|
|
|
|
|
let expectedFolderAndFeedIds = namesAndFeeds
|
|
|
|
.sorted { $0.0.id < $1.0.id }
|
|
|
|
.map { folder, feeds -> [String: [String]] in
|
2019-12-01 21:39:05 +01:00
|
|
|
return [folder.id: feeds.map { $0.id }.sorted(by: <)]
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let ingestedFolderAndFeedIds = (account.folders ?? Set())
|
|
|
|
.sorted { $0.externalID! < $1.externalID! }
|
|
|
|
.compactMap { folder -> [String: [String]]? in
|
2019-11-15 03:11:41 +01:00
|
|
|
return [folder.externalID!: folder.topLevelWebFeeds.map { $0.webFeedID }.sorted(by: <)]
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(expectedFolderAndFeedIds, ingestedFolderAndFeedIds, "Did not ingest feeds in their corresponding folders.")
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRemoveFeeds() {
|
|
|
|
let folderOne: (name: String, id: String) = ("FolderOne", "folder/1")
|
|
|
|
let folderTwo: (name: String, id: String) = ("FolderTwo", "folder/2")
|
2019-12-01 21:39:05 +01:00
|
|
|
let feedToRemove = FeedlyFeed(id: "feed/1", title: "Feed One", updated: nil, website: nil)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
var feedsForFolderOne = [
|
|
|
|
feedToRemove,
|
2019-12-01 21:39:05 +01:00
|
|
|
FeedlyFeed(id: "feed/2", title: "Feed Two", updated: nil, website: nil)
|
2019-10-18 23:21:02 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
var feedsForFolderTwo = [
|
|
|
|
feedToRemove,
|
2019-12-01 21:39:05 +01:00
|
|
|
FeedlyFeed(id: "feed/3", title: "Feed Three", updated: nil, website: nil),
|
2019-10-18 23:21:02 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
// Add initial content.
|
|
|
|
do {
|
|
|
|
let namesAndFeeds = [(folderOne, feedsForFolderOne), (folderTwo, feedsForFolderTwo)]
|
|
|
|
|
|
|
|
let provider = FeedsAndFoldersProvider()
|
|
|
|
provider.feedsAndFolders = namesAndFeeds.map { (folder, feeds) in
|
|
|
|
let accountFolder = account.ensureFolder(with: folder.name)!
|
|
|
|
accountFolder.externalID = folder.id
|
|
|
|
return (feeds, accountFolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
let createFeeds = FeedlyCreateFeedsForCollectionFoldersOperation(account: account, feedsAndFoldersProvider: provider, log: support.log)
|
|
|
|
let completionExpectation = expectation(description: "Did Finish")
|
2020-01-16 07:10:06 +01:00
|
|
|
createFeeds.completionBlock = { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
completionExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
XCTAssertTrue(account.flattenedWebFeeds().isEmpty, "Expected empty account.")
|
2019-10-18 23:21:02 +02:00
|
|
|
|
2020-08-04 19:43:29 +02:00
|
|
|
MainThreadOperationQueue.shared.add(createFeeds)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
feedsForFolderOne.removeAll { $0.id == feedToRemove.id }
|
|
|
|
feedsForFolderTwo.removeAll { $0.id == feedToRemove.id }
|
|
|
|
let namesAndFeeds = [(folderOne, feedsForFolderOne), (folderTwo, feedsForFolderTwo)]
|
|
|
|
|
|
|
|
let provider = FeedsAndFoldersProvider()
|
|
|
|
provider.feedsAndFolders = namesAndFeeds.map { (folder, feeds) in
|
|
|
|
let accountFolder = account.ensureFolder(with: folder.name)!
|
|
|
|
accountFolder.externalID = folder.id
|
|
|
|
return (feeds, accountFolder)
|
|
|
|
}
|
|
|
|
|
|
|
|
let removeFeeds = FeedlyCreateFeedsForCollectionFoldersOperation(account: account, feedsAndFoldersProvider: provider, log: support.log)
|
|
|
|
let completionExpectation = expectation(description: "Did Finish")
|
2020-01-16 07:10:06 +01:00
|
|
|
removeFeeds.completionBlock = { _ in
|
2019-10-18 23:21:02 +02:00
|
|
|
completionExpectation.fulfill()
|
|
|
|
}
|
|
|
|
|
2020-08-04 19:43:29 +02:00
|
|
|
MainThreadOperationQueue.shared.add(removeFeeds)
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
waitForExpectations(timeout: 2)
|
|
|
|
|
|
|
|
let feedIds = Set([feedsForFolderOne, feedsForFolderTwo]
|
|
|
|
.flatMap { $0 }
|
2019-12-01 21:39:05 +01:00
|
|
|
.map { $0.id })
|
2019-10-18 23:21:02 +02:00
|
|
|
|
|
|
|
let feedTitles = Set([feedsForFolderOne, feedsForFolderTwo]
|
|
|
|
.flatMap { $0 }
|
|
|
|
.map { $0.title })
|
|
|
|
|
2019-11-15 03:11:41 +01:00
|
|
|
let accountFeeds = account.flattenedWebFeeds()
|
|
|
|
let ingestedIds = Set(accountFeeds.map { $0.webFeedID })
|
2019-10-18 23:21:02 +02:00
|
|
|
let ingestedTitles = Set(accountFeeds.map { $0.nameForDisplay })
|
|
|
|
|
|
|
|
XCTAssertEqual(ingestedIds.count, feedIds.count)
|
|
|
|
XCTAssertEqual(ingestedTitles.count, feedTitles.count)
|
|
|
|
|
|
|
|
let missingIds = feedIds.subtracting(ingestedIds)
|
|
|
|
let missingTitles = feedTitles.subtracting(ingestedTitles)
|
|
|
|
|
|
|
|
XCTAssertTrue(missingIds.isEmpty, "Failed to ingest feeds with these ids.")
|
|
|
|
XCTAssertTrue(missingTitles.isEmpty, "Failed to ingest feeds with these titles.")
|
|
|
|
|
|
|
|
let expectedFolderAndFeedIds = namesAndFeeds
|
|
|
|
.sorted { $0.0.id < $1.0.id }
|
|
|
|
.map { folder, feeds -> [String: [String]] in
|
2019-12-01 21:39:05 +01:00
|
|
|
return [folder.id: feeds.map { $0.id }.sorted(by: <)]
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let ingestedFolderAndFeedIds = (account.folders ?? Set())
|
|
|
|
.sorted { $0.externalID! < $1.externalID! }
|
|
|
|
.compactMap { folder -> [String: [String]]? in
|
2019-11-15 03:11:41 +01:00
|
|
|
return [folder.externalID!: folder.topLevelWebFeeds.map { $0.webFeedID }.sorted(by: <)]
|
2019-10-18 23:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
XCTAssertEqual(expectedFolderAndFeedIds, ingestedFolderAndFeedIds, "Did not ingest feeds to their corresponding folders.")
|
|
|
|
}
|
|
|
|
}
|