Skip to content

Commit c0361d5

Browse files
committed
Fix tests
1 parent 712f650 commit c0361d5

File tree

4 files changed

+63
-61
lines changed

4 files changed

+63
-61
lines changed

sync/sync-settings-impl/src/test/java/com/duckduckgo/sync/settings/impl/FakeCrypto.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ package com.duckduckgo.sync.settings.impl
1919
import com.duckduckgo.sync.api.*
2020

2121
class FakeCrypto : SyncCrypto {
22-
override fun encrypt(text: String) = text
22+
override suspend fun encrypt(text: String) = text
2323

24-
override fun decrypt(data: String) = data
24+
override suspend fun decrypt(data: String) = data
2525
}

sync/sync-settings-impl/src/test/java/com/duckduckgo/sync/settings/impl/SettingsSyncDataPersisterTest.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import com.duckduckgo.sync.api.engine.SyncMergeResult.Success
2828
import com.duckduckgo.sync.api.engine.SyncableDataPersister.SyncConflictResolution.DEDUPLICATION
2929
import com.duckduckgo.sync.api.engine.SyncableDataPersister.SyncConflictResolution.TIMESTAMP
3030
import kotlinx.coroutines.*
31+
import kotlinx.coroutines.test.runTest
3132
import org.junit.*
3233
import org.junit.Assert.assertEquals
3334
import org.junit.Assert.assertTrue
@@ -67,7 +68,7 @@ class SettingsSyncDataPersisterTest {
6768
}
6869

6970
@Test
70-
fun whenPersistChangesDeduplicationWithdValueThenCallDeduplicateWithValue() {
71+
fun whenPersistChangesDeduplicationWithdValueThenCallDeduplicateWithValue() = runTest {
7172
val result = testee.onSuccess(
7273
changes = SyncChangesResponse(
7374
type = SyncableType.SETTINGS,
@@ -81,7 +82,7 @@ class SettingsSyncDataPersisterTest {
8182
}
8283

8384
@Test
84-
fun whenPersistChangesDeduplicationWithDeletedValueThenCallDeduplicateWithNull() {
85+
fun whenPersistChangesDeduplicationWithDeletedValueThenCallDeduplicateWithNull() = runTest {
8586
val result = testee.onSuccess(
8687
changes = SyncChangesResponse(
8788
type = SyncableType.SETTINGS,
@@ -95,7 +96,7 @@ class SettingsSyncDataPersisterTest {
9596
}
9697

9798
@Test
98-
fun whenPersistChangesTimestampAndNoRecentChangeThenCallMergeWithValue() {
99+
fun whenPersistChangesTimestampAndNoRecentChangeThenCallMergeWithValue() = runTest {
99100
settingSyncStore.startTimeStamp = "2023-08-31T10:06:16.022Z"
100101
val result = testee.onSuccess(
101102
changes = SyncChangesResponse(
@@ -110,7 +111,7 @@ class SettingsSyncDataPersisterTest {
110111
}
111112

112113
@Test
113-
fun whenPersistChangesTimestampWithDeletedValueThenCallSaveWithNull() {
114+
fun whenPersistChangesTimestampWithDeletedValueThenCallSaveWithNull() = runTest {
114115
val result = testee.onSuccess(
115116
changes = SyncChangesResponse(
116117
type = SyncableType.SETTINGS,
@@ -124,7 +125,7 @@ class SettingsSyncDataPersisterTest {
124125
}
125126

126127
@Test
127-
fun whenPersistChangesTimestampButRecentlyModifiedThenSkip() {
128+
fun whenPersistChangesTimestampButRecentlyModifiedThenSkip() = runTest {
128129
settingSyncStore.startTimeStamp = "2023-08-31T10:06:16.022Z"
129130
metadataDao.addOrUpdate(
130131
SettingsSyncMetadataEntity(
@@ -147,7 +148,7 @@ class SettingsSyncDataPersisterTest {
147148
}
148149

149150
@Test
150-
fun whenPersistChangesSucceedsThenUpdateServerAndClientTimestamps() {
151+
fun whenPersistChangesSucceedsThenUpdateServerAndClientTimestamps() = runTest {
151152
settingSyncStore.startTimeStamp = "2023-08-31T10:06:16.022Z"
152153

153154
val result = testee.onSuccess(

sync/sync-settings-impl/src/test/java/com/duckduckgo/sync/settings/impl/SettingsSyncDataProviderTest.kt

+7-7
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class SettingsSyncDataProviderTest {
9696
}
9797

9898
@Test
99-
fun whenGetChangesForFirstSyncThenChangesIncludeAllValues() {
99+
fun whenGetChangesForFirstSyncThenChangesIncludeAllValues() = runTest {
100100
val changes = testee.getChanges()
101101

102102
assertTrue(changes.type == SyncableType.SETTINGS)
@@ -111,7 +111,7 @@ class SettingsSyncDataProviderTest {
111111

112112
@Test
113113
@Ignore("Need to decide strategy first")
114-
fun whenGetChangesSubsequentCallsWithNewValueThenIncludeNewValues() {
114+
fun whenGetChangesSubsequentCallsWithNewValueThenIncludeNewValues() = runTest {
115115
settingSyncStore.serverModifiedSince = "2022-01-01T00:00:00Z"
116116
settingSyncStore.clientModifiedSince = "2022-01-01T00:00:00Z"
117117

@@ -128,7 +128,7 @@ class SettingsSyncDataProviderTest {
128128
}
129129

130130
@Test
131-
fun whenGetChangesSubsequentCallsAndNoChangesThenUpdatesAreEmpty() {
131+
fun whenGetChangesSubsequentCallsAndNoChangesThenUpdatesAreEmpty() = runTest {
132132
settingSyncStore.serverModifiedSince = "2022-01-01T00:00:00Z"
133133
settingSyncStore.clientModifiedSince = "2022-01-01T00:00:00Z"
134134
metadataDao.addOrUpdate(SettingsSyncMetadataEntity(duckAddressSetting.key, "2022-01-01T00:00:00Z", ""))
@@ -141,7 +141,7 @@ class SettingsSyncDataProviderTest {
141141
}
142142

143143
@Test
144-
fun whenDBHasDataButItIsFirstSyncThenIncludeAllValues() {
144+
fun whenDBHasDataButItIsFirstSyncThenIncludeAllValues() = runTest {
145145
metadataDao.addOrUpdate(SettingsSyncMetadataEntity(duckAddressSetting.key, "2022-01-01T00:00:00Z", ""))
146146

147147
val changes = testee.getChanges()
@@ -157,7 +157,7 @@ class SettingsSyncDataProviderTest {
157157
}
158158

159159
@Test
160-
fun whenGetChangesForFirstSyncAndSettingNullThenSendAsDeleted() {
160+
fun whenGetChangesForFirstSyncAndSettingNullThenSendAsDeleted() = runTest {
161161
duckAddressSetting.save(null)
162162

163163
val changes = testee.getChanges()
@@ -173,7 +173,7 @@ class SettingsSyncDataProviderTest {
173173
}
174174

175175
@Test
176-
fun whenGetChangesSubsequentCallsAndSettingNullThenSendAsDeleted() {
176+
fun whenGetChangesSubsequentCallsAndSettingNullThenSendAsDeleted() = runTest {
177177
settingSyncStore.serverModifiedSince = "2022-01-01T00:00:00Z"
178178
settingSyncStore.clientModifiedSince = "2022-01-01T00:00:00Z"
179179
metadataDao.addOrUpdate(SettingsSyncMetadataEntity(duckAddressSetting.key, "2022-01-02T00:00:00Z", ""))
@@ -192,7 +192,7 @@ class SettingsSyncDataProviderTest {
192192
}
193193

194194
@Test
195-
fun whenSyncableSettingNotFoundThenSkipUpdate() {
195+
fun whenSyncableSettingNotFoundThenSkipUpdate() = runTest {
196196
settingSyncStore.serverModifiedSince = "2022-01-01T00:00:00Z"
197197
settingSyncStore.clientModifiedSince = "2022-01-01T00:00:00Z"
198198
metadataDao.addOrUpdate(SettingsSyncMetadataEntity("unknown_setting", "2022-01-02T00:00:00Z", ""))

sync/sync-store/src/test/java/com/duckduckgo/sync/store/SyncSharedPrefsStoreTest.kt

+47-46
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
2020
import androidx.test.platform.app.InstrumentationRegistry
2121
import com.duckduckgo.common.test.CoroutineTestRule
2222
import kotlinx.coroutines.test.TestScope
23+
import kotlinx.coroutines.test.runTest
2324
import org.junit.Assert.assertEquals
2425
import org.junit.Assert.assertNull
2526
import org.junit.Assert.assertTrue
@@ -43,78 +44,78 @@ class SyncSharedPrefsStoreTest {
4344
sharedPrefsProvider,
4445
TestScope(),
4546
coroutineRule.testDispatcherProvider,
46-
syncFeature.createAsyncPreferences().isEnabled(),
47+
createAsyncPreferences = true,
4748
)
4849
}
4950

5051
@Test
51-
fun whenUserIdStoredThenValueUpdatedInPrefsStore() {
52-
assertNull(store.userId)
53-
store.userId = "test_user"
54-
assertEquals("test_user", store.userId)
55-
store.userId = null
56-
assertNull(store.userId)
52+
fun whenUserIdStoredThenValueUpdatedInPrefsStore() = runTest {
53+
assertNull(store.getUserId())
54+
store.setUserId("test_user")
55+
assertEquals("test_user", store.getUserId())
56+
store.setUserId(null)
57+
assertNull(store.getUserId())
5758
}
5859

5960
@Test
60-
fun whenDeviceNameStoredThenValueUpdatedInPrefsStore() {
61-
assertNull(store.deviceName)
62-
store.deviceName = "test_device"
63-
assertEquals("test_device", store.deviceName)
64-
store.deviceName = null
65-
assertNull(store.deviceName)
61+
fun whenDeviceNameStoredThenValueUpdatedInPrefsStore() = runTest {
62+
assertNull(store.getDeviceName())
63+
store.setDeviceName("test_device")
64+
assertEquals("test_device", store.getDeviceName())
65+
store.setDeviceName(null)
66+
assertNull(store.getDeviceName())
6667
}
6768

6869
@Test
69-
fun whenDeviceIdStoredThenValueUpdatedInPrefsStore() {
70-
assertNull(store.deviceId)
71-
store.deviceId = "test_device_id"
72-
assertEquals("test_device_id", store.deviceId)
73-
store.deviceId = null
74-
assertNull(store.deviceId)
70+
fun whenDeviceIdStoredThenValueUpdatedInPrefsStore() = runTest {
71+
assertNull(store.getDeviceId())
72+
store.setDeviceId("test_device_id")
73+
assertEquals("test_device_id", store.getDeviceId())
74+
store.setDeviceId(null)
75+
assertNull(store.getDeviceId())
7576
}
7677

7778
@Test
78-
fun whenStoreCredentialsThenValuesUpdatedInPrefsStore() {
79-
assertNull(store.userId)
80-
assertNull(store.deviceName)
81-
assertNull(store.deviceId)
82-
assertNull(store.primaryKey)
83-
assertNull(store.secretKey)
84-
assertNull(store.token)
79+
fun whenStoreCredentialsThenValuesUpdatedInPrefsStore() = runTest {
80+
assertNull(store.getUserId())
81+
assertNull(store.getDeviceName())
82+
assertNull(store.getDeviceId())
83+
assertNull(store.getPrimaryKey())
84+
assertNull(store.getSecretKey())
85+
assertNull(store.getToken())
8586
store.storeCredentials("userId", "deviceId", "deviceName", "primaryKey", "secretKey", "token")
86-
assertEquals("userId", store.userId)
87-
assertEquals("deviceName", store.deviceName)
88-
assertEquals("deviceId", store.deviceId)
89-
assertEquals("primaryKey", store.primaryKey)
90-
assertEquals("secretKey", store.secretKey)
91-
assertEquals("token", store.token)
87+
assertEquals("userId", store.getUserId())
88+
assertEquals("deviceName", store.getDeviceName())
89+
assertEquals("deviceId", store.getDeviceId())
90+
assertEquals("primaryKey", store.getPrimaryKey())
91+
assertEquals("secretKey", store.getSecretKey())
92+
assertEquals("token", store.getToken())
9293
}
9394

9495
@Test
95-
fun whenIsSignedInThenReturnTrueIfUserHasAuthKeys() {
96+
fun whenIsSignedInThenReturnTrueIfUserHasAuthKeys() = runTest {
9697
store.storeCredentials("userId", "deviceId", "deviceName", "primaryKey", "secretKey", "token")
9798

9899
assertTrue(store.isSignedIn())
99100
}
100101

101102
@Test
102-
fun whenClearAllThenReturnRemoveAllKeys() {
103+
fun whenClearAllThenReturnRemoveAllKeys() = runTest {
103104
store.storeCredentials("userId", "deviceId", "deviceName", "primaryKey", "secretKey", "token")
104-
assertEquals("userId", store.userId)
105-
assertEquals("deviceName", store.deviceName)
106-
assertEquals("deviceId", store.deviceId)
107-
assertEquals("primaryKey", store.primaryKey)
108-
assertEquals("secretKey", store.secretKey)
109-
assertEquals("token", store.token)
105+
assertEquals("userId", store.getUserId())
106+
assertEquals("deviceName", store.getDeviceName())
107+
assertEquals("deviceId", store.getDeviceId())
108+
assertEquals("primaryKey", store.getPrimaryKey())
109+
assertEquals("secretKey", store.getSecretKey())
110+
assertEquals("token", store.getToken())
110111

111112
store.clearAll()
112113

113-
assertNull(store.userId)
114-
assertNull(store.deviceName)
115-
assertNull(store.deviceId)
116-
assertNull(store.primaryKey)
117-
assertNull(store.secretKey)
118-
assertNull(store.token)
114+
assertNull(store.getUserId())
115+
assertNull(store.getDeviceName())
116+
assertNull(store.getDeviceId())
117+
assertNull(store.getPrimaryKey())
118+
assertNull(store.getSecretKey())
119+
assertNull(store.getToken())
119120
}
120121
}

0 commit comments

Comments
 (0)