diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo032Test.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo032Test.kt new file mode 100644 index 0000000000..f66ccd9cf2 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/internal/database/migration/MigrateSessionTo032Test.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 org.matrix.android.sdk.internal.database.migration + +import org.junit.Test +import org.matrix.android.sdk.test.fakes.FakeDynamicRealm +import org.matrix.android.sdk.internal.database.model.RoomSummaryEntityFields + +internal class MigrateSessionTo032Test { + + private val fakeDynamicRealm = FakeDynamicRealm() + private val migrator = MigrateSessionTo032(fakeDynamicRealm.instance) + + @Test + fun `when doMigrate, then directParentNames added`() { + migrator.doMigrate(fakeDynamicRealm.instance) + + fakeDynamicRealm.fakeRealmSchema.withObjectSchema("RoomSummaryEntity") + .verifyListFieldAdded(RoomSummaryEntityFields.DIRECT_PARENT_NAMES.`$`, String::class.java) + .verifyStringTransformation(RoomSummaryEntityFields.DIRECT_PARENT_NAMES.`$`, "") + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeDynamicRealm.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeDynamicRealm.kt new file mode 100644 index 0000000000..939b3be0c1 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeDynamicRealm.kt @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 org.matrix.android.sdk.test.fakes + +import io.mockk.every +import io.mockk.mockk +import io.realm.DynamicRealm + +class FakeDynamicRealm( + val fakeRealmSchema: FakeRealmSchema = FakeRealmSchema() +) { + + val instance: DynamicRealm = mockk { + every { schema } returns fakeRealmSchema.instance + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeDynamicRealmObject.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeDynamicRealmObject.kt new file mode 100644 index 0000000000..1e0e1808b0 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeDynamicRealmObject.kt @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 org.matrix.android.sdk.test.fakes + +import io.mockk.justRun +import io.mockk.mockk +import io.mockk.verify +import io.realm.DynamicRealmObject + +class FakeDynamicRealmObject { + + val instance: DynamicRealmObject = mockk { + justRun { setString(any(), any()) } + } + + fun verifySetString(fieldName: String, value: String) { + verify { instance.setString(fieldName, value) } + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealmObjectSchema.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealmObjectSchema.kt new file mode 100644 index 0000000000..91f05b9604 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealmObjectSchema.kt @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 org.matrix.android.sdk.test.fakes + +import io.mockk.every +import io.mockk.mockk +import io.mockk.slot +import io.mockk.verify +import io.realm.RealmObjectSchema +import io.realm.RealmObjectSchema.Function + +class FakeRealmObjectSchema( + private val fakeDynamicRealmObject: FakeDynamicRealmObject = FakeDynamicRealmObject() +) { + + val instance: RealmObjectSchema = mockk { + every { addRealmListField(any(), any>()) } returns this + every { transform(any()) } returns this + } + + fun verifyListFieldAdded(fieldName: String, type: Class<*>) = apply { + verify { instance.addRealmListField(fieldName, type) } + } + + fun verifyStringTransformation(fieldName: String, transformedInto: String) = apply { + val transformationSlot = slot() + verify { instance.transform(capture(transformationSlot)) } + transformationSlot.captured.apply(fakeDynamicRealmObject.instance) + fakeDynamicRealmObject.verifySetString(fieldName, transformedInto) + } +} diff --git a/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealmSchema.kt b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealmSchema.kt new file mode 100644 index 0000000000..519654b883 --- /dev/null +++ b/matrix-sdk-android/src/test/java/org/matrix/android/sdk/test/fakes/FakeRealmSchema.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 New Vector Ltd + * + * 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 + * + * http://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 org.matrix.android.sdk.test.fakes + +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import io.realm.RealmSchema + +class FakeRealmSchema( + private val fakeRealmObjectSchema: FakeRealmObjectSchema = FakeRealmObjectSchema() +) { + + val instance: RealmSchema = mockk { + every { this@mockk.get(any()) } returns fakeRealmObjectSchema.instance + } + + fun withObjectSchema(className: String): FakeRealmObjectSchema { + verify { instance.get(className) } + return fakeRealmObjectSchema + } +}