diff --git a/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceFtsDao.kt b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceFtsDao.kt new file mode 100644 index 000000000..b6de4320b --- /dev/null +++ b/core/data/src/test/kotlin/com/google/samples/apps/nowinandroid/core/data/testdoubles/TestNewsResourceFtsDao.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2024 The Android Open Source Project + * + * 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 + * + * https://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 com.google.samples.apps.nowinandroid.core.data.testdoubles + +import com.google.samples.apps.nowinandroid.core.database.dao.NewsResourceFtsDao +import com.google.samples.apps.nowinandroid.core.database.model.NewsResourceFtsEntity +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.flowOf +import kotlinx.coroutines.flow.update + +class TestNewsResourceFtsDao : NewsResourceFtsDao { + private val entitiesStateFlow = MutableStateFlow(emptyList()) + + override suspend fun insertAll(newsResources: List) { + entitiesStateFlow.update { oldValues -> + (oldValues + newsResources) + .distinctBy { it.newsResourceId } + } + } + + override fun searchAllNewsResources(query: String): Flow> { + val results = entitiesStateFlow.value.filter { entity -> + entity.title.contains(query) || entity.content.contains(query) + } + .map { it.newsResourceId } + + return flowOf(results.distinct()) + } + + override fun getCount(): Flow { + return flowOf(entitiesStateFlow.value.size) + } +}