Skip to content

Commit

Permalink
added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EricBAndrews committed Oct 1, 2023
1 parent 561c6b2 commit 3e1b257
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Mlem/Models/Trackers/RecentSearchesTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RecentSearchesTracker: ObservableObject {

recentSearches = .init()
if let accountHash = apiClient.accountHash {
let identifiers = persistenceRepository.loadRecentSearches(accountHash: accountHash)
let identifiers = persistenceRepository.loadRecentSearches(for: accountHash)

for id in identifiers {
print(id.contentType, id.contentId)
Expand Down
2 changes: 1 addition & 1 deletion Mlem/Repositories/PersistenceRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class PersistenceRepository {
try await save(value, to: Path.savedAccounts)
}

func loadRecentSearches(accountHash: Int) -> [ContentModelIdentifier] {
func loadRecentSearches(for accountHash: Int) -> [ContentModelIdentifier] {
let searches = load([Int: [ContentModelIdentifier]].self, from: Path.recentSearches) ?? [:]
return searches[accountHash] ?? []
}
Expand Down
22 changes: 14 additions & 8 deletions MlemTests/Persistence/PersistenceRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,30 @@ final class PersistenceRepositoryTests: XCTestCase {
func testSaveRecentSearches() async throws {
let searches: [ContentModelIdentifier] = [.init(contentType: .user, contentId: 1), .init(contentType: .community, contentId: 2)]

try await repository.saveRecentSearches(searches) // write the examples to disk
let searchesFromDisk = try load([ContentModelIdentifier].self) // load them from the disk _without_ using the repository
try await repository.saveRecentSearches(for: 1, with: searches) // write the examples to disk
let searchesFromDisk = try load([Int: [ContentModelIdentifier]].self) // load them from the disk _without_ using the repository

XCTAssertEqual(searches, searchesFromDisk) // confirm what was written to disk matches what we sent in
let expected: [Int: [ContentModelIdentifier]] = [1: searches]
XCTAssertEqual(expected, searchesFromDisk) // confirm what was written to disk matches what we sent in
}

func testLoadRecentSearchesWithValues() async throws {
let searches: [ContentModelIdentifier] = [.init(contentType: .user, contentId: 1), .init(contentType: .community, contentId: 2)]
let searches1: [ContentModelIdentifier] = [.init(contentType: .user, contentId: 1), .init(contentType: .community, contentId: 2)]
let searches2: [ContentModelIdentifier] = [.init(contentType: .user, contentId: 2), .init(contentType: .community, contentId: 3)]

try await repository.saveRecentSearches(for: 1, with: searches1)
try await repository.saveRecentSearches(for: 2, with: searches2)

try await repository.saveRecentSearches(searches) // write the example terms to the disk
let loadedSearches = repository.loadRecentSearches() // read them back
let loadedSearches1 = repository.loadRecentSearches(accountHash: 1) // read them back

Check failure on line 139 in MlemTests/Persistence/PersistenceRepositoryTests.swift

View workflow job for this annotation

GitHub Actions / Xcode test results

error

Incorrect argument label in call (have 'accountHash:', expected 'for:')
let loadedSearches2 = repository.loadRecentSearches(accountHash: 2)

Check failure on line 140 in MlemTests/Persistence/PersistenceRepositoryTests.swift

View workflow job for this annotation

GitHub Actions / Xcode test results

error

Incorrect argument label in call (have 'accountHash:', expected 'for:')

XCTAssertEqual(loadedSearches, searches) // assert we were given the same values back
XCTAssertEqual(loadedSearches1, searches1) // assert we were given the same values back
XCTAssertEqual(loadedSearches2, searches2)
}

func testLoadRecentSearchesWithoutValues() async throws {
XCTAssert(disk.isEmpty) // assert that our mock disk has nothing in it
let loadedSearches = repository.loadRecentSearches() // perform a load knowing the disk is empty
let loadedSearches = repository.loadRecentSearches(accountHash: 1) // perform a load knowing the disk is empty

Check failure on line 148 in MlemTests/Persistence/PersistenceRepositoryTests.swift

View workflow job for this annotation

GitHub Actions / Xcode test results

error

Incorrect argument label in call (have 'accountHash:', expected 'for:')
XCTAssert(loadedSearches.isEmpty) // assert we were returned an empty list
}

Expand Down

0 comments on commit 3e1b257

Please sign in to comment.