Create FeedlyUtilities and FeedlyUtilitiesTests.

This commit is contained in:
Brent Simmons 2024-05-17 22:34:00 -07:00
parent 5760705784
commit d024e6049d
3 changed files with 121 additions and 98 deletions

View File

@ -1,98 +0,0 @@
//
// ParsedItemsKeyedByFeedURLTests.swift
// AccountTests
//
// Created by Kiel Gillard on 24/10/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import XCTest
@testable import Account
import Parser
final class ParsedItemsKeyedByFeedURLTests: 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()
}
struct TestParsedItemsProvider: FeedlyParsedItemProviding {
let parsedItemProviderName = "TestParsedItemsProvider"
var resource: FeedlyResourceID
var parsedEntries: Set<ParsedItem>
}
func testNoEntries() {
let entries = support.makeParsedItemTestDataFor(numberOfFeeds: 0, numberOfItemsInFeeds: 0)
let resource = FeedlyCategoryResourceID(id: "user/12345/category/6789")
let parsedEntries = Set(entries.values.flatMap { $0 })
let provider = TestParsedItemsProvider(resource: resource, parsedEntries: parsedEntries)
let organise = FeedlyOrganiseParsedItemsByFeedOperation(account: account, parsedItemProvider: provider, log: support.log)
let completionExpectation = expectation(description: "Did Finish")
organise.completionBlock = { _ in
completionExpectation.fulfill()
}
MainThreadOperationQueue.shared.add(organise)
waitForExpectations(timeout: 2)
let itemsAndFeedIDs = organise.parsedItemsKeyedByFeedID
XCTAssertEqual(itemsAndFeedIDs, entries)
}
func testGroupsOneEntryByFeedId() {
let entries = support.makeParsedItemTestDataFor(numberOfFeeds: 1, numberOfItemsInFeeds: 1)
let resource = FeedlyCategoryResourceID(id: "user/12345/category/6789")
let parsedEntries = Set(entries.values.flatMap { $0 })
let provider = TestParsedItemsProvider(resource: resource, parsedEntries: parsedEntries)
let organise = FeedlyOrganiseParsedItemsByFeedOperation(account: account, parsedItemProvider: provider, log: support.log)
let completionExpectation = expectation(description: "Did Finish")
organise.completionBlock = { _ in
completionExpectation.fulfill()
}
MainThreadOperationQueue.shared.add(organise)
waitForExpectations(timeout: 2)
let itemsAndFeedIDs = organise.parsedItemsKeyedByFeedID
XCTAssertEqual(itemsAndFeedIDs, entries)
}
func testGroupsManyEntriesByFeedId() {
let entries = support.makeParsedItemTestDataFor(numberOfFeeds: 100, numberOfItemsInFeeds: 100)
let resource = FeedlyCategoryResourceID(id: "user/12345/category/6789")
let parsedEntries = Set(entries.values.flatMap { $0 })
let provider = TestParsedItemsProvider(resource: resource, parsedEntries: parsedEntries)
let organise = FeedlyOrganiseParsedItemsByFeedOperation(account: account, parsedItemProvider: provider, log: support.log)
let completionExpectation = expectation(description: "Did Finish")
organise.completionBlock = { _ in
completionExpectation.fulfill()
}
MainThreadOperationQueue.shared.add(organise)
waitForExpectations(timeout: 2)
let itemsAndFeedIDs = organise.parsedItemsKeyedByFeedID
XCTAssertEqual(itemsAndFeedIDs, entries)
}
}

View File

@ -0,0 +1,34 @@
//
// FeedlyUtilities.swift
//
//
// Created by Brent Simmons on 5/17/24.
//
import Foundation
import Parser
final class FeedlyUtilities {
static func parsedItemsKeyedByFeedURL(_ parsedItems: Set<ParsedItem>) -> [String: Set<ParsedItem>] {
var d = [String: Set<ParsedItem>]()
for parsedItem in parsedItems {
let key = parsedItem.feedURL
let value: Set<ParsedItem> = {
if var items = d[key] {
items.insert(parsedItem)
return items
} else {
return [parsedItem]
}
}()
d[key] = value
}
return d
}
}

View File

@ -0,0 +1,87 @@
//
// FeedlyUtilitiesTests.swift
// AccountTests
//
// Created by Kiel Gillard on 24/10/19.
// Copyright © 2019 Ranchero Software, LLC. All rights reserved.
//
import XCTest
import Parser
@testable import Feedly
final class FeedlyUtilitiesTests: XCTestCase {
// MARK: - Test parsedItemsKeyedByFeedURL
func testParsedItemsKeyedByFeedURL_Empty() {
let testDictionary = makeParsedItemTestDataFor(numberOfFeeds: 0, numberOfItemsInFeeds: 0)
let parsedItems = parsedItemsFromDictionary(testDictionary)
let resultDictionary = FeedlyUtilities.parsedItemsKeyedByFeedURL(parsedItems)
let expectedDictionary = testDictionary
XCTAssertEqual(resultDictionary, expectedDictionary)
}
func testParsedItemsKeyedByFeedURL_OneFeedOneItem() {
let testDictionary = makeParsedItemTestDataFor(numberOfFeeds: 1, numberOfItemsInFeeds: 1)
let parsedItems = parsedItemsFromDictionary(testDictionary)
let resultDictionary = FeedlyUtilities.parsedItemsKeyedByFeedURL(parsedItems)
let expectedDictionary = testDictionary
XCTAssertEqual(resultDictionary, expectedDictionary)
}
func testParsedItemsKeyedByFeedURL_ManyFeedsManyItems() {
let testDictionary = makeParsedItemTestDataFor(numberOfFeeds: 100, numberOfItemsInFeeds: 100)
let parsedItems = parsedItemsFromDictionary(testDictionary)
let resultDictionary = FeedlyUtilities.parsedItemsKeyedByFeedURL(parsedItems)
let expectedDictionary = testDictionary
XCTAssertEqual(resultDictionary, expectedDictionary)
}
}
// MARK: - Private
private extension FeedlyUtilitiesTests {
func makeParsedItemTestDataFor(numberOfFeeds: Int, numberOfItemsInFeeds: Int) -> [String: Set<ParsedItem>] {
var d = [String: Set<ParsedItem>]()
for feedIndex in 0..<numberOfFeeds {
let feedID = "feed/\(feedIndex)"
var items = Set<ParsedItem>()
for parsedItemIndex in 0..<numberOfItemsInFeeds {
let parsedItem = makeTestParsedItem(feedID, parsedItemIndex)
items.insert(parsedItem)
}
d[feedID] = items
}
return d
}
func makeTestParsedItem(_ feedID: String, _ index: Int) -> ParsedItem {
ParsedItem(syncServiceID: "\(feedID)/articles/\(index)", uniqueID: UUID().uuidString, feedURL: feedID, url: "http://localhost/", externalURL: "http://localhost/\(feedID)/articles/\(index).html", title: "Title\(index)", language: nil, contentHTML: "Content \(index) HTML.", contentText: "Content \(index) Text", summary: nil, imageURL: nil, bannerImageURL: nil, datePublished: nil, dateModified: nil, authors: nil, tags: nil, attachments: nil)
}
func parsedItemsFromDictionary(_ d: [String: Set<ParsedItem>]) -> Set<ParsedItem> {
var parsedItems = Set<ParsedItem>()
for (key, value) in d {
parsedItems.formUnion(value)
}
return parsedItems
}
}