Adding unit test on aggregation processor

This commit is contained in:
Maxime NATUREL 2022-12-14 14:13:49 +01:00
parent 9338ec9805
commit 644803dcf3
3 changed files with 59 additions and 2 deletions

View file

@ -211,7 +211,6 @@ internal class DefaultPollAggregationProcessor @Inject constructor(
}
}
// TODO add unit tests
/**
* Check that all related votes to a given poll are all retrieved and aggregated.
*/

View file

@ -16,9 +16,13 @@
package org.matrix.android.sdk.internal.session.room.aggregation.poll
import io.mockk.coVerify
import io.mockk.every
import io.mockk.mockk
import io.realm.RealmList
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.amshove.kluent.shouldBeFalse
import org.amshove.kluent.shouldBeTrue
import org.junit.Before
@ -34,6 +38,7 @@ import org.matrix.android.sdk.internal.database.model.PollResponseAggregatedSumm
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.AN_EVENT_ID
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.AN_INVALID_POLL_RESPONSE_EVENT
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.A_BROKEN_POLL_REPLACE_EVENT
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.A_POLL_END_CONTENT
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.A_POLL_END_EVENT
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.A_POLL_REFERENCE_EVENT
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.A_POLL_REPLACE_EVENT
@ -43,13 +48,22 @@ import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsT
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.A_ROOM_ID
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.A_TIMELINE_EVENT
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollEventsTestData.A_USER_ID_1
import org.matrix.android.sdk.internal.session.room.relation.poll.FetchPollResponseEventsTask
import org.matrix.android.sdk.test.fakes.FakeFetchPollResponseEventsTask
import org.matrix.android.sdk.test.fakes.FakeRealm
import org.matrix.android.sdk.test.fakes.FakeTaskExecutor
import org.matrix.android.sdk.test.fakes.givenEqualTo
import org.matrix.android.sdk.test.fakes.givenFindFirst
@OptIn(ExperimentalCoroutinesApi::class)
class DefaultPollAggregationProcessorTest {
private val pollAggregationProcessor: PollAggregationProcessor = DefaultPollAggregationProcessor()
private val fakeTaskExecutor = FakeTaskExecutor()
private val fakeFetchPollResponseEventsTask = FakeFetchPollResponseEventsTask()
private val pollAggregationProcessor: PollAggregationProcessor = DefaultPollAggregationProcessor(
taskExecutor = fakeTaskExecutor.instance,
fetchPollResponseEventsTask = fakeFetchPollResponseEventsTask
)
private val realm = FakeRealm()
private val session = mockk<Session>()
@ -135,6 +149,28 @@ class DefaultPollAggregationProcessorTest {
pollAggregationProcessor.handlePollEndEvent(session, powerLevelsHelper, realm.instance, event).shouldBeFalse()
}
@Test
fun `given a non local echo poll end event, when is processed, then ensure to aggregate all poll responses`() = runTest {
// Given
every { realm.instance.createObject(PollResponseAggregatedSummaryEntity::class.java) } returns PollResponseAggregatedSummaryEntity()
val powerLevelsHelper = mockRedactionPowerLevels("another-sender-id", true)
val event = A_POLL_END_EVENT.copy(senderId = "another-sender-id")
every { fakeTaskExecutor.instance.executorScope } returns this
val expectedParams = FetchPollResponseEventsTask.Params(
roomId = A_POLL_END_EVENT.roomId.orEmpty(),
startPollEventId = A_POLL_END_CONTENT.relatesTo?.eventId.orEmpty(),
)
// When
pollAggregationProcessor.handlePollEndEvent(session, powerLevelsHelper, realm.instance, event)
advanceUntilIdle()
// Then
coVerify {
fakeFetchPollResponseEventsTask.execute(expectedParams)
}
}
private fun mockEventAnnotationsSummaryEntity() {
realm.givenWhere<EventAnnotationsSummaryEntity>()
.givenFindFirst(EventAnnotationsSummaryEntity())

View file

@ -0,0 +1,22 @@
/*
* 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.mockk
import org.matrix.android.sdk.internal.session.room.relation.poll.FetchPollResponseEventsTask
class FakeFetchPollResponseEventsTask : FetchPollResponseEventsTask by mockk(relaxed = true)