Skip to content

Commit

Permalink
Delete local translation and insert a new one
Browse files Browse the repository at this point in the history
As we cannot change the Id of the translation through UPDATE query.
  • Loading branch information
mohamede1945 committed Nov 8, 2023
1 parent 56d8864 commit 27329aa
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,10 @@ public struct TranslationsRepository {

private func saveCombined(translations: [Translation], localMap: [String: Translation]) async throws {
for translation in translations {
if localMap[translation.fileName] != nil {
try await persistence.update(translation)
} else {
try await persistence.insert(translation)
if let oldTranslation = localMap[translation.fileName] {
try await persistence.remove(oldTranslation)
}
try await persistence.insert(translation)
}
}
}
16 changes: 11 additions & 5 deletions Domain/TranslationService/Tests/TranslationsRepositoryTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TranslationsRepositoryTests: XCTestCase {
func test_updatedVersion() async throws {
let translation = translations[0]
let updatedTranslation = Translation(
id: translation.id,
id: translation.id + 45,
displayName: translation.displayName,
translator: translation.translator,
translatorForeign: translation.translatorForeign,
Expand All @@ -69,11 +69,10 @@ class TranslationsRepositoryTests: XCTestCase {
XCTAssertEqual(localResults, [updatedTranslation])
}

// TODO: This crashes the app, we should fix it.
func DISABLED_test_updatedFileName() async throws {
func test_updatedFileName() async throws {
let translation = translations[0]
let updatedTranslation = Translation(
id: translation.id,
id: translation.id + 19,
displayName: translation.displayName,
translator: translation.translator,
translatorForeign: translation.translatorForeign,
Expand All @@ -90,7 +89,14 @@ class TranslationsRepositoryTests: XCTestCase {
try await service.downloadAndSyncTranslations()

let localResults = try await retriever.getLocalTranslations()
XCTAssertEqual(localResults, [updatedTranslation])

// Expected to create a duplicate entry if fileName changed,
// fileName should not change to upgrade, it's a unique value
// per translation
XCTAssertEqual(localResults.sorted(), [
expectedTranslation(translation, installedVersion: nil),
expectedTranslation(updatedTranslation, installedVersion: nil),
].sorted())
}

// MARK: Private
Expand Down

0 comments on commit 27329aa

Please sign in to comment.