Adding unit tests for DefaultCreateUnableToDecryptEventEntityTask

This commit is contained in:
Maxime NATUREL 2022-12-27 14:38:21 +01:00 committed by Maxime NATUREL
parent a04c60a85b
commit e9f59d85b4
4 changed files with 79 additions and 1 deletions

View File

@ -32,7 +32,6 @@ internal interface CreateUnableToDecryptEventEntityTask : Task<CreateUnableToDec
)
}
// TODO add unit tests
internal class DefaultCreateUnableToDecryptEventEntityTask @Inject constructor(
@SessionDatabase val realmConfiguration: RealmConfiguration,
) : CreateUnableToDecryptEventEntityTask {

View File

@ -0,0 +1,65 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* 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.crypto.tasks
import io.mockk.unmockkAll
import io.mockk.verify
import io.realm.RealmModel
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Test
import org.matrix.android.sdk.internal.database.model.UnableToDecryptEventEntity
import org.matrix.android.sdk.test.fakes.FakeRealm
import org.matrix.android.sdk.test.fakes.FakeRealmConfiguration
@OptIn(ExperimentalCoroutinesApi::class)
internal class DefaultCreateUnableToDecryptEventEntityTaskTest {
private val fakeRealmConfiguration = FakeRealmConfiguration()
private val defaultCreateUnableToDecryptEventEntityTask = DefaultCreateUnableToDecryptEventEntityTask(
realmConfiguration = fakeRealmConfiguration.instance,
)
@After
fun tearDown() {
unmockkAll()
}
@Test
fun `given an event id when execute then insert entity into database`() = runTest {
// Given
val anEventId = "event-id"
val params = CreateUnableToDecryptEventEntityTask.Params(
eventId = anEventId,
)
val fakeRealm = FakeRealm()
fakeRealm.givenExecuteTransactionAsync()
fakeRealmConfiguration.givenGetRealmInstance(fakeRealm.instance)
// When
defaultCreateUnableToDecryptEventEntityTask.execute(params)
// Then
verify {
fakeRealm.instance.insert(match<RealmModel> {
it is UnableToDecryptEventEntity && it.eventId == anEventId
})
}
}
}

View File

@ -23,6 +23,7 @@ import io.mockk.mockk
import io.mockk.runs
import io.mockk.verify
import io.realm.Realm
import io.realm.Realm.Transaction
import io.realm.RealmModel
import io.realm.RealmObject
import io.realm.RealmQuery
@ -42,6 +43,13 @@ internal class FakeRealm {
inline fun <reified T : RealmModel> verifyInsertOrUpdate(crossinline verification: MockKVerificationScope.() -> T) {
verify { instance.insertOrUpdate(verification()) }
}
fun givenExecuteTransactionAsync() {
every { instance.executeTransactionAsync(any()) } answers {
firstArg<Transaction>().execute(instance)
mockk()
}
}
}
inline fun <reified T : RealmModel> RealmQuery<T>.givenFindFirst(

View File

@ -17,6 +17,7 @@
package org.matrix.android.sdk.test.fakes
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
import io.realm.Realm
@ -36,4 +37,9 @@ internal class FakeRealmConfiguration {
secondArg<(Realm) -> T>().invoke(realm)
}
}
fun givenGetRealmInstance(realm: Realm) {
mockkStatic(Realm::class)
every { Realm.getInstance(instance) } returns realm
}
}