Skip to content

Commit

Permalink
fix user info not persists when reset (#179)
Browse files Browse the repository at this point in the history
* fix reset not persisted in disk

* add unit tests

* update sample apps permission
  • Loading branch information
wenxi-zeng authored Aug 2, 2023
1 parent 980ae87 commit 310c020
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ class StorageTests {
assertEquals("{}", traits)
}

@Test
fun `userInfo reset action removes userInfo`() = runTest {
store.dispatch(UserInfo.ResetAction(), UserInfo::class)

val userId = androidStorage.read(Storage.Constants.UserId)
val anonId = androidStorage.read(Storage.Constants.AnonymousId)
val traits = androidStorage.read(Storage.Constants.Traits)

assertNotNull(anonId)
assertEquals(null, userId)
assertEquals(null, traits)
}

@Test
fun `system update calls write for settings`() = runTest {
val action = object : Action<System> {
Expand Down Expand Up @@ -137,6 +150,20 @@ class StorageTests {
)
}

@Test
fun `system reset action removes system`() = runTest {
val action = object : Action<System> {
override fun reduce(state: System): System {
return System(state.configuration, null, state.running, state.initialSettingsDispatched, state.enabled)
}
}
store.dispatch(action, System::class)

val settings = androidStorage.read(Storage.Constants.Settings)

assertEquals(null, settings)
}

@Nested
inner class KeyValueStorage {
val map = getWorkingMap(mockContext.getSharedPreferences("", 0))
Expand Down
22 changes: 13 additions & 9 deletions core/src/main/java/com/segment/analytics/kotlin/core/Storage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,25 @@ interface Storage {

suspend fun userInfoUpdate(userInfo: UserInfo) {
write(Constants.AnonymousId, userInfo.anonymousId)
userInfo.userId?.let { write(Constants.UserId, it) }

userInfo.userId?.let {
write(Constants.UserId, it)
} ?: run {
remove(Constants.UserId)
}

userInfo.traits?.let {
write(
Constants.Traits,
Json.encodeToString(JsonObject.serializer(), it)
)
write(Constants.Traits, Json.encodeToString(JsonObject.serializer(), it))
} ?: run {
remove(Constants.Traits)
}
}

suspend fun systemUpdate(system: System) {
system.settings?.let {
write(
Constants.Settings,
Json.encodeToString(Settings.serializer(), it)
)
write(Constants.Settings, Json.encodeToString(Settings.serializer(), it))
} ?: run {
remove(Constants.Settings)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,12 +704,14 @@ class AnalyticsTests {

analytics.identify("oldUserId",
buildJsonObject { put("behaviour", "bad") })
assertEquals(analytics.userIdAsync(), "oldUserId")
assertEquals(analytics.traitsAsync(), buildJsonObject { put("behaviour", "bad") })
assertEquals(analytics.userId(), "oldUserId")
assertEquals(analytics.traits(), buildJsonObject { put("behaviour", "bad") })

analytics.reset()
assertEquals(analytics.userIdAsync(), null)
assertEquals(analytics.traitsAsync(), null)
assertEquals(analytics.userId(), null)
assertEquals(analytics.traits(), null)
assertEquals(analytics.storage.read(Storage.Constants.UserId), null)
assertEquals(analytics.storage.read(Storage.Constants.Traits), null)
verify { plugin.reset() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,8 @@ internal class JavaAnalyticsTest {
analytics.reset()
assertEquals(analytics.userId(), null)
assertEquals(analytics.traits(), null)
assertEquals(analytics.storage.read(Storage.Constants.UserId), null)
assertEquals(analytics.storage.read(Storage.Constants.Traits), null)
verify { plugin.reset() }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ internal class StorageImplTest {
assertEquals("{}", traits)
}

@Test
fun `userInfo reset action removes userInfo`() = runTest {
store.dispatch(UserInfo.ResetAction(), UserInfo::class)

val userId = storage.read(Storage.Constants.UserId)
val anonId = storage.read(Storage.Constants.AnonymousId)
val traits = storage.read(Storage.Constants.Traits)

assertNotNull(anonId)
assertEquals(null, userId)
assertEquals(null, traits)
}

@Test
fun `system update calls write for settings`() = runTest {
val action = object : Action<System> {
Expand Down Expand Up @@ -135,6 +148,20 @@ internal class StorageImplTest {
)
}

@Test
fun `system reset action removes system`() = runTest {
val action = object : Action<System> {
override fun reduce(state: System): System {
return System(state.configuration, null, state.running, state.initialSettingsDispatched, state.enabled)
}
}
store.dispatch(action, System::class)

val settings = storage.read(Storage.Constants.Settings)

assertEquals(null, settings)
}

@Nested
inner class EventsStorage() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<!-- Allows location to be tracked. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:name=".MainApplication"
Expand Down
1 change: 1 addition & 0 deletions samples/kotlin-android-app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<!-- Allows location to be tracked. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application
android:name=".MainApplication"
Expand Down

0 comments on commit 310c020

Please sign in to comment.