Merge pull request #294 from dturner/proto-list-to-map
Change user data fields to use maps instead of lists.pull/297/head
commit
6825138452
@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.samples.apps.nowinandroid.core.datastore
|
||||||
|
|
||||||
|
import androidx.datastore.core.DataMigration
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Migrates from using lists to maps for user data.
|
||||||
|
*/
|
||||||
|
object ListToMapMigration : DataMigration<UserPreferences> {
|
||||||
|
|
||||||
|
override suspend fun cleanUp() = Unit
|
||||||
|
|
||||||
|
override suspend fun migrate(currentData: UserPreferences): UserPreferences =
|
||||||
|
currentData.copy {
|
||||||
|
// Migrate topic id lists
|
||||||
|
followedTopicIds.clear()
|
||||||
|
followedTopicIds.putAll(
|
||||||
|
currentData.deprecatedFollowedTopicIdsList.associateWith { true }
|
||||||
|
)
|
||||||
|
deprecatedFollowedTopicIds.clear()
|
||||||
|
|
||||||
|
// Migrate author ids
|
||||||
|
followedAuthorIds.clear()
|
||||||
|
followedAuthorIds.putAll(
|
||||||
|
currentData.deprecatedFollowedAuthorIdsList.associateWith { true }
|
||||||
|
)
|
||||||
|
deprecatedFollowedAuthorIds.clear()
|
||||||
|
|
||||||
|
// Migrate bookmarks
|
||||||
|
bookmarkedNewsResourceIds.clear()
|
||||||
|
bookmarkedNewsResourceIds.putAll(
|
||||||
|
currentData.deprecatedBookmarkedNewsResourceIdsList.associateWith { true }
|
||||||
|
)
|
||||||
|
deprecatedBookmarkedNewsResourceIds.clear()
|
||||||
|
|
||||||
|
// Mark migration as complete
|
||||||
|
hasDoneListToMapMigration = true
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun shouldMigrate(currentData: UserPreferences): Boolean {
|
||||||
|
return !currentData.hasDoneListToMapMigration
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2022 The Android Open Source Project
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.google.samples.apps.nowinandroid.core.datastore
|
||||||
|
|
||||||
|
import kotlinx.coroutines.test.runTest
|
||||||
|
import org.junit.Assert
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
class ListToMapMigrationTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun ListToMapMigration_should_migrate_topic_ids() = runTest {
|
||||||
|
// Set up existing preferences with topic ids
|
||||||
|
val preMigrationUserPreferences = userPreferences {
|
||||||
|
deprecatedFollowedTopicIds.addAll(listOf("1", "2", "3"))
|
||||||
|
}
|
||||||
|
// Assert that there are no topic ids in the map yet
|
||||||
|
Assert.assertEquals(
|
||||||
|
emptyMap<String, Boolean>(),
|
||||||
|
preMigrationUserPreferences.followedTopicIdsMap
|
||||||
|
)
|
||||||
|
|
||||||
|
// Run the migration
|
||||||
|
val postMigrationUserPreferences =
|
||||||
|
ListToMapMigration.migrate(preMigrationUserPreferences)
|
||||||
|
|
||||||
|
// Assert the deprecated topic ids have been migrated to the topic ids map
|
||||||
|
Assert.assertEquals(
|
||||||
|
mapOf("1" to true, "2" to true, "3" to true),
|
||||||
|
postMigrationUserPreferences.followedTopicIdsMap
|
||||||
|
)
|
||||||
|
|
||||||
|
// Assert that the migration has been marked complete
|
||||||
|
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun ListToMapMigration_should_migrate_author_ids() = runTest {
|
||||||
|
// Set up existing preferences with author ids
|
||||||
|
val preMigrationUserPreferences = userPreferences {
|
||||||
|
deprecatedFollowedAuthorIds.addAll(listOf("4", "5", "6"))
|
||||||
|
}
|
||||||
|
// Assert that there are no author ids in the map yet
|
||||||
|
Assert.assertEquals(
|
||||||
|
emptyMap<String, Boolean>(),
|
||||||
|
preMigrationUserPreferences.followedAuthorIdsMap
|
||||||
|
)
|
||||||
|
|
||||||
|
// Run the migration
|
||||||
|
val postMigrationUserPreferences =
|
||||||
|
ListToMapMigration.migrate(preMigrationUserPreferences)
|
||||||
|
|
||||||
|
// Assert the deprecated author ids have been migrated to the author ids map
|
||||||
|
Assert.assertEquals(
|
||||||
|
mapOf("4" to true, "5" to true, "6" to true),
|
||||||
|
postMigrationUserPreferences.followedAuthorIdsMap
|
||||||
|
)
|
||||||
|
|
||||||
|
// Assert that the migration has been marked complete
|
||||||
|
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun ListToMapMigration_should_migrate_bookmarks() = runTest {
|
||||||
|
// Set up existing preferences with bookmarks
|
||||||
|
val preMigrationUserPreferences = userPreferences {
|
||||||
|
deprecatedBookmarkedNewsResourceIds.addAll(listOf("7", "8", "9"))
|
||||||
|
}
|
||||||
|
// Assert that there are no bookmarks in the map yet
|
||||||
|
Assert.assertEquals(
|
||||||
|
emptyMap<String, Boolean>(),
|
||||||
|
preMigrationUserPreferences.bookmarkedNewsResourceIdsMap
|
||||||
|
)
|
||||||
|
|
||||||
|
// Run the migration
|
||||||
|
val postMigrationUserPreferences =
|
||||||
|
ListToMapMigration.migrate(preMigrationUserPreferences)
|
||||||
|
|
||||||
|
// Assert the deprecated bookmarks have been migrated to the bookmarks map
|
||||||
|
Assert.assertEquals(
|
||||||
|
mapOf("7" to true, "8" to true, "9" to true),
|
||||||
|
postMigrationUserPreferences.bookmarkedNewsResourceIdsMap
|
||||||
|
)
|
||||||
|
|
||||||
|
// Assert that the migration has been marked complete
|
||||||
|
Assert.assertTrue(postMigrationUserPreferences.hasDoneListToMapMigration)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue