Skip to content

Commit

Permalink
Add tie breaker for root fetching
Browse files Browse the repository at this point in the history
  • Loading branch information
bwaresiak committed Jul 26, 2024
1 parent 1c25c93 commit 69140da
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 6 deletions.
25 changes: 19 additions & 6 deletions Sources/Bookmarks/BookmarkUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ public struct BookmarkUtils {
let request = BookmarkEntity.fetchRequest()
request.predicate = NSPredicate(format: "%K == %@", #keyPath(BookmarkEntity.uuid), BookmarkEntity.Constants.rootFolderID)
request.returnsObjectsAsFaults = false
request.fetchLimit = 1

return try? context.fetch(request).first
let result = (try? context.fetch(request)) ?? []

// We cannot use simply sort descriptor as this is to-many on both sides of a relationship.
return result.sorted(by: { ($0.children?.count ?? 0) > ($1.children?.count ?? 0) }).first
}

public static func fetchFavoritesFolders(for displayMode: FavoritesDisplayMode, in context: NSManagedObjectContext) -> [BookmarkEntity] {
Expand All @@ -40,9 +42,11 @@ public struct BookmarkUtils {
let request = BookmarkEntity.fetchRequest()
request.predicate = NSPredicate(format: "%K == %@", #keyPath(BookmarkEntity.uuid), uuid)
request.returnsObjectsAsFaults = false
request.fetchLimit = 1

return try? context.fetch(request).first
let result = (try? context.fetch(request)) ?? []

// We cannot use simply sort descriptor as this is to-many on both sides of a relationship.
return result.sorted(by: { ($0.favorites?.count ?? 0) > ($1.favorites?.count ?? 0) }).first
}

public static func fetchFavoritesFolders(withUUIDs uuids: Set<String>, in context: NSManagedObjectContext) -> [BookmarkEntity] {
Expand All @@ -51,9 +55,18 @@ public struct BookmarkUtils {
let request = BookmarkEntity.fetchRequest()
request.predicate = NSPredicate(format: "%K in %@", #keyPath(BookmarkEntity.uuid), uuids)
request.returnsObjectsAsFaults = false
request.fetchLimit = uuids.count

return (try? context.fetch(request)) ?? []
var objects = (try? context.fetch(request)) ?? []
objects.sort(by: { ($0.favorites?.count ?? 0) > ($1.favorites?.count ?? 0) })

Check failure on line 61 in Sources/Bookmarks/BookmarkUtils.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
var result = [BookmarkEntity]()
for uuid in uuids {
if let entity = objects.first(where: { $0.uuid == uuid }) {
result.append(entity)
}
}

return result
}

public static func fetchOrphanedEntities(_ context: NSManagedObjectContext) -> [BookmarkEntity] {
Expand Down
71 changes: 71 additions & 0 deletions Tests/BookmarksTests/BookmarkUtilsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,77 @@ final class BookmarkUtilsTests: XCTestCase {
try? FileManager.default.removeItem(at: location)
}

func testThatFetchingRootFolderPicksTheOneWithMostChildren() {
let context = bookmarksDatabase.makeContext(concurrencyType: .privateQueueConcurrencyType)
context.performAndWait {
BookmarkUtils.prepareFoldersStructure(in: context)
try! context.save()

guard let root1 = BookmarkUtils.fetchRootFolder(context) else {
XCTFail("root required")
return
}

let root2 = BookmarkEntity.makeFolder(title: root1.title!, parent: root1, context: context)
root2.uuid = root1.uuid
root2.parent = nil

let root3 = BookmarkEntity.makeFolder(title: root1.title!, parent: root1, context: context)
root3.uuid = root1.uuid
root3.parent = nil

_ = BookmarkEntity.makeBookmark(title: "a", url: "a", parent: root2, context: context)
_ = BookmarkEntity.makeBookmark(title: "b", url: "b", parent: root2, context: context)
_ = BookmarkEntity.makeBookmark(title: "c", url: "c", parent: root1, context: context)

try! context.save()

Check failure on line 75 in Tests/BookmarksTests/BookmarkUtilsTests.swift

View workflow job for this annotation

GitHub Actions / Run SwiftLint

Lines should not have trailing whitespace (trailing_whitespace)
let root = BookmarkUtils.fetchRootFolder(context)
XCTAssertEqual(root, root2)
}
}

func testThatFetchingRootFavoritesFolderPicksTheOneWithMostFavorites() {
let context = bookmarksDatabase.makeContext(concurrencyType: .privateQueueConcurrencyType)
context.performAndWait {
BookmarkUtils.prepareFoldersStructure(in: context)
try! context.save()

guard let root = BookmarkUtils.fetchRootFolder(context),
let mobileFav1 = BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.mobile.rawValue, in: context),
let unifiedFav1 = BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.unified.rawValue, in: context) else {
XCTFail("root required")
return
}

let mobileFav2 = BookmarkEntity.makeFolder(title: mobileFav1.title!, parent: root, context: context)
mobileFav2.uuid = mobileFav1.uuid
mobileFav2.parent = nil

let unifiedFav2 = BookmarkEntity.makeFolder(title: unifiedFav1.title!, parent: root, context: context)
unifiedFav2.uuid = unifiedFav1.uuid
unifiedFav2.parent = nil

let bA = BookmarkEntity.makeBookmark(title: "a", url: "a", parent: root, context: context)
let bB = BookmarkEntity.makeBookmark(title: "b", url: "b", parent: root, context: context)

bA.addToFavorites(favoritesRoot: mobileFav2)
bA.addToFavorites(favoritesRoot: unifiedFav1)
bB.addToFavorites(folders: [mobileFav2, unifiedFav1])

try! context.save()

let roots = BookmarkUtils.fetchFavoritesFolders(for: .displayUnified(native: .mobile), in: context)
XCTAssertEqual(Set(roots.map { $0.uuid }), [mobileFav2.uuid, unifiedFav1.uuid])

let mobileFav = BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.mobile.rawValue, in: context)
XCTAssertEqual(mobileFav, mobileFav2)

let unifiedFav = BookmarkUtils.fetchFavoritesFolder(withUUID: FavoritesFolderID.unified.rawValue, in: context)
XCTAssertEqual(unifiedFav, unifiedFav1)
}
}

func testCopyFavoritesWhenDisablingSyncInDisplayNativeMode() async throws {

let context = bookmarksDatabase.makeContext(concurrencyType: .privateQueueConcurrencyType)
Expand Down

0 comments on commit 69140da

Please sign in to comment.