|
| 1 | +package com.fsck.k9.preferences.migration |
| 2 | + |
| 3 | +import assertk.assertThat |
| 4 | +import assertk.assertions.isEqualTo |
| 5 | +import assertk.assertions.key |
| 6 | +import com.fsck.k9.preferences.createPreferencesDatabase |
| 7 | +import java.util.UUID |
| 8 | +import kotlin.test.Test |
| 9 | +import net.thunderbird.core.logging.legacy.Log |
| 10 | +import net.thunderbird.core.logging.testing.TestLogger |
| 11 | +import net.thunderbird.feature.mail.folder.api.ArchiveGranularity |
| 12 | +import org.junit.After |
| 13 | +import org.junit.Before |
| 14 | +import org.junit.runner.RunWith |
| 15 | +import org.robolectric.RobolectricTestRunner |
| 16 | + |
| 17 | +@RunWith(RobolectricTestRunner::class) |
| 18 | +class StorageMigrationTo30Test { |
| 19 | + private val database = createPreferencesDatabase() |
| 20 | + private val migrationHelper = DefaultStorageMigrationHelper() |
| 21 | + private val migration = StorageMigrationTo30(database, migrationHelper) |
| 22 | + |
| 23 | + @Before |
| 24 | + fun setUp() { |
| 25 | + Log.logger = TestLogger() |
| 26 | + } |
| 27 | + |
| 28 | + @After |
| 29 | + fun tearDown() { |
| 30 | + database.close() |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `archiveGranularity should be set to SINGLE_ARCHIVE_FOLDER for existing accounts`() { |
| 35 | + val accountUuid = createAccount() |
| 36 | + writeAccountUuids(accountUuid) |
| 37 | + |
| 38 | + migration.setDefaultArchiveGranularity() |
| 39 | + |
| 40 | + assertThat(migrationHelper.readAllValues(database)) |
| 41 | + .key("$accountUuid.archiveGranularity") |
| 42 | + .isEqualTo(ArchiveGranularity.MIGRATION_DEFAULT.name) |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + fun `archiveGranularity should not overwrite existing value`() { |
| 47 | + val accountUuid = createAccount( |
| 48 | + "archiveGranularity" to ArchiveGranularity.PER_MONTH_ARCHIVE_FOLDERS.name, |
| 49 | + ) |
| 50 | + writeAccountUuids(accountUuid) |
| 51 | + |
| 52 | + migration.setDefaultArchiveGranularity() |
| 53 | + |
| 54 | + assertThat(migrationHelper.readAllValues(database)) |
| 55 | + .key("$accountUuid.archiveGranularity") |
| 56 | + .isEqualTo(ArchiveGranularity.PER_MONTH_ARCHIVE_FOLDERS.name) |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + fun `archiveGranularity should be set for multiple accounts without the setting`() { |
| 61 | + val account1Uuid = createAccount() |
| 62 | + val account2Uuid = createAccount() |
| 63 | + val account3Uuid = createAccount() |
| 64 | + writeAccountUuids(account1Uuid, account2Uuid, account3Uuid) |
| 65 | + |
| 66 | + migration.setDefaultArchiveGranularity() |
| 67 | + |
| 68 | + val values = migrationHelper.readAllValues(database) |
| 69 | + assertThat(values) |
| 70 | + .key("$account1Uuid.archiveGranularity") |
| 71 | + .isEqualTo(ArchiveGranularity.MIGRATION_DEFAULT.name) |
| 72 | + assertThat(values) |
| 73 | + .key("$account2Uuid.archiveGranularity") |
| 74 | + .isEqualTo(ArchiveGranularity.MIGRATION_DEFAULT.name) |
| 75 | + assertThat(values) |
| 76 | + .key("$account3Uuid.archiveGranularity") |
| 77 | + .isEqualTo(ArchiveGranularity.MIGRATION_DEFAULT.name) |
| 78 | + } |
| 79 | + |
| 80 | + @Test |
| 81 | + fun `archiveGranularity should handle mix of accounts with and without existing values`() { |
| 82 | + val accountWithoutSetting = createAccount() |
| 83 | + val accountWithYearly = createAccount( |
| 84 | + "archiveGranularity" to ArchiveGranularity.PER_YEAR_ARCHIVE_FOLDERS.name, |
| 85 | + ) |
| 86 | + val accountWithMonthly = createAccount( |
| 87 | + "archiveGranularity" to ArchiveGranularity.PER_MONTH_ARCHIVE_FOLDERS.name, |
| 88 | + ) |
| 89 | + writeAccountUuids(accountWithoutSetting, accountWithYearly, accountWithMonthly) |
| 90 | + |
| 91 | + migration.setDefaultArchiveGranularity() |
| 92 | + |
| 93 | + val values = migrationHelper.readAllValues(database) |
| 94 | + assertThat(values) |
| 95 | + .key("$accountWithoutSetting.archiveGranularity") |
| 96 | + .isEqualTo(ArchiveGranularity.MIGRATION_DEFAULT.name) |
| 97 | + assertThat(values) |
| 98 | + .key("$accountWithYearly.archiveGranularity") |
| 99 | + .isEqualTo(ArchiveGranularity.PER_YEAR_ARCHIVE_FOLDERS.name) |
| 100 | + assertThat(values) |
| 101 | + .key("$accountWithMonthly.archiveGranularity") |
| 102 | + .isEqualTo(ArchiveGranularity.PER_MONTH_ARCHIVE_FOLDERS.name) |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + fun `archiveGranularity should not be set when no accounts exist`() { |
| 107 | + writeAccountUuids() |
| 108 | + |
| 109 | + migration.setDefaultArchiveGranularity() |
| 110 | + |
| 111 | + val values = migrationHelper.readAllValues(database) |
| 112 | + assertThat(values.keys.filter { it.contains("archiveGranularity") }).isEqualTo(emptyList()) |
| 113 | + } |
| 114 | + |
| 115 | + private fun writeAccountUuids(vararg accounts: String) { |
| 116 | + val accountUuids = accounts.joinToString(separator = ",") |
| 117 | + migrationHelper.insertValue(database, "accountUuids", accountUuids) |
| 118 | + } |
| 119 | + |
| 120 | + private fun createAccount(vararg pairs: Pair<String, String?>): String { |
| 121 | + val accountUuid = UUID.randomUUID().toString() |
| 122 | + |
| 123 | + for ((key, value) in pairs) { |
| 124 | + migrationHelper.insertValue(database, "$accountUuid.$key", value) |
| 125 | + } |
| 126 | + |
| 127 | + return accountUuid |
| 128 | + } |
| 129 | +} |
0 commit comments