add testdouble for newsresourcefts dao

pull/1785/head
Tobi Oyelekan 8 months ago
parent d15c739812
commit 31227f3607

@ -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<NewsResourceFtsEntity>())
override suspend fun insertAll(newsResources: List<NewsResourceFtsEntity>) {
entitiesStateFlow.update { oldValues ->
(oldValues + newsResources)
.distinctBy { it.newsResourceId }
}
}
override fun searchAllNewsResources(query: String): Flow<List<String>> {
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<Int> {
return flowOf(entitiesStateFlow.value.size)
}
}
Loading…
Cancel
Save