Fix connected tests and news resource queries

Change-Id: I111876049543f5a5f3cf203b80069bd31c6104f8
pull/2/head
Adetunji Dahunsi 3 years ago committed by Don Turner
parent 356d00531a
commit 14807caa42

@ -151,20 +151,20 @@ class NewsResourceDaoTest {
newsResourceTopicCrossRefEntities
)
val filteredNewsResources = newsResourceDao.getNewsResourcesForTopicsStream(
val filteredNewsResources = newsResourceDao.getNewsResourcesStream(
filterTopicIds = topicEntities
.map(TopicEntity::id)
.toSet()
.toSet(),
).first()
assertEquals(
listOf(1, 2),
listOf(1, 0),
filteredNewsResources.map { it.entity.id }
)
}
@Test
fun newsResourceDao_filters_items_by_author_topics_ids_by_descending_publish_date() = runTest {
fun newsResourceDao_filters_items_by_author_ids_by_descending_publish_date() = runTest {
val authorEntities = listOf(
testAuthorEntity(
id = 1,
@ -208,7 +208,7 @@ class NewsResourceDaoTest {
newsResourceDao.upsertNewsResources(newsResourceEntities)
newsResourceDao.insertOrIgnoreAuthorCrossRefEntities(newsResourceAuthorCrossRefEntities)
val filteredNewsResources = newsResourceDao.getNewsResourcesForAuthorsStream(
val filteredNewsResources = newsResourceDao.getNewsResourcesStream(
filterAuthorIds = authorEntities
.map(AuthorEntity::id)
.toSet()
@ -219,6 +219,92 @@ class NewsResourceDaoTest {
filteredNewsResources.map { it.entity.id }
)
}
@Test
fun newsResourceDao_filters_items_by_topic_ids_or_author_ids_by_descending_publish_date() =
runTest {
val topicEntities = listOf(
testTopicEntity(
id = 1,
name = "1"
),
testTopicEntity(
id = 2,
name = "2"
),
)
val authorEntities = listOf(
testAuthorEntity(
id = 1,
name = "1"
),
testAuthorEntity(
id = 2,
name = "2"
),
)
val newsResourceEntities = listOf(
testNewsResource(
id = 0,
millisSinceEpoch = 0,
),
testNewsResource(
id = 1,
millisSinceEpoch = 3,
),
testNewsResource(
id = 2,
millisSinceEpoch = 1,
),
testNewsResource(
id = 3,
millisSinceEpoch = 2,
),
// Should be missing as no topics or authors match it
testNewsResource(
id = 4,
millisSinceEpoch = 10,
),
)
val episodeEntityShells = newsResourceEntities
.map(NewsResourceEntity::episodeEntityShell)
.distinct()
val newsResourceTopicCrossRefEntities = topicEntities.mapIndexed { index, topicEntity ->
NewsResourceTopicCrossRef(
newsResourceId = index,
topicId = topicEntity.id
)
}
val newsResourceAuthorCrossRefEntities =
authorEntities.mapIndexed { index, authorEntity ->
NewsResourceAuthorCrossRef(
// Offset news resources by two
newsResourceId = index + 2,
authorId = authorEntity.id
)
}
topicDao.upsertTopics(topicEntities)
authorDao.upsertAuthors(authorEntities)
episodeDao.upsertEpisodes(episodeEntityShells)
newsResourceDao.upsertNewsResources(newsResourceEntities)
newsResourceDao.insertOrIgnoreTopicCrossRefEntities(newsResourceTopicCrossRefEntities)
newsResourceDao.insertOrIgnoreAuthorCrossRefEntities(newsResourceAuthorCrossRefEntities)
val filteredNewsResources = newsResourceDao.getNewsResourcesStream(
filterTopicIds = topicEntities
.map(TopicEntity::id)
.toSet(),
filterAuthorIds = authorEntities
.map(AuthorEntity::id)
.toSet()
).first()
assertEquals(
listOf(1, 3, 2, 0),
filteredNewsResources.map { it.entity.id }
)
}
}
private fun testAuthorEntity(

@ -50,45 +50,17 @@ interface NewsResourceDao {
SELECT news_resource_id FROM news_resources_topics
WHERE topic_id IN (:filterTopicIds)
)
ORDER BY publish_date DESC
"""
)
fun getNewsResourcesForTopicsStream(
filterTopicIds: Set<Int>
): Flow<List<PopulatedNewsResource>>
@Query(
value = """
SELECT * FROM news_resources
WHERE id in
OR id in
(
SELECT news_resource_id FROM news_resources_authors
WHERE author_id IN (:filterAuthorIds)
)
ORDER BY publish_date DESC
"""
)
fun getNewsResourcesForAuthorsStream(
filterAuthorIds: Set<Int>
): Flow<List<PopulatedNewsResource>>
@Query(
value = """
SELECT * FROM news_resources
WHERE id in
(
SELECT topics.topic_id FROM news_resources_topics as topics
INNER JOIN news_resources_authors as authors
ON topics.news_resource_id == authors.news_resource_id
WHERE topics.topic_id IN (:filterTopicIds)
AND authors.author_id IN (:filterAuthorIds)
WHERE author_id IN (:filterAuthorIds)
)
ORDER BY publish_date DESC
"""
)
fun getNewsResourcesStream(
filterAuthorIds: Set<Int>,
filterTopicIds: Set<Int>
filterAuthorIds: Set<Int> = emptySet(),
filterTopicIds: Set<Int> = emptySet(),
): Flow<List<PopulatedNewsResource>>
/**

@ -60,17 +60,10 @@ class LocalNewsRepository @Inject constructor(
override fun getNewsResourcesStream(
filterAuthorIds: Set<Int>,
filterTopicIds: Set<Int>
): Flow<List<NewsResource>> = when {
filterAuthorIds.isEmpty() -> {
newsResourceDao.getNewsResourcesForTopicsStream(filterTopicIds)
}
filterTopicIds.isEmpty() -> {
newsResourceDao.getNewsResourcesForAuthorsStream(filterAuthorIds)
}
else -> {
newsResourceDao.getNewsResourcesStream(filterAuthorIds, filterTopicIds)
}
}
): Flow<List<NewsResource>> = newsResourceDao.getNewsResourcesStream(
filterAuthorIds = filterAuthorIds,
filterTopicIds = filterTopicIds
)
.map { it.map(PopulatedNewsResource::asExternalModel) }
override suspend fun sync() = changeListSync(

@ -29,11 +29,11 @@ interface NewsRepository {
fun getNewsResourcesStream(): Flow<List<NewsResource>>
/**
* Returns available news resources as a stream filtered by authors and topics.
* Returns available news resources as a stream filtered by authors or topics.
*/
fun getNewsResourcesStream(
filterAuthorIds: Set<Int>,
filterTopicIds: Set<Int>,
filterAuthorIds: Set<Int> = emptySet(),
filterTopicIds: Set<Int> = emptySet(),
): Flow<List<NewsResource>>
/**

@ -104,12 +104,13 @@ class LocalNewsRepositoryTest {
fun localNewsRepository_news_resources_topic_filtered_stream_is_backed_by_news_resource_dao() =
runTest {
assertEquals(
newsResourceDao.getNewsResourcesForTopicsStream(filteredInterestsIds)
newsResourceDao.getNewsResourcesStream(
filterTopicIds = filteredInterestsIds,
)
.first()
.map(PopulatedNewsResource::asExternalModel),
subject.getNewsResourcesStream(
filterTopicIds = filteredInterestsIds,
filterAuthorIds = emptySet()
)
.first()
)
@ -118,7 +119,6 @@ class LocalNewsRepositoryTest {
emptyList<NewsResource>(),
subject.getNewsResourcesStream(
filterTopicIds = nonPresentInterestsIds,
filterAuthorIds = emptySet()
)
.first()
)
@ -128,11 +128,12 @@ class LocalNewsRepositoryTest {
fun localNewsRepository_news_resources_author_filtered_stream_is_backed_by_news_resource_dao() =
runTest {
assertEquals(
newsResourceDao.getNewsResourcesForAuthorsStream(filteredInterestsIds)
newsResourceDao.getNewsResourcesStream(
filterAuthorIds = filteredInterestsIds
)
.first()
.map(PopulatedNewsResource::asExternalModel),
subject.getNewsResourcesStream(
filterTopicIds = emptySet(),
filterAuthorIds = filteredInterestsIds
)
.first()
@ -141,7 +142,6 @@ class LocalNewsRepositoryTest {
assertEquals(
emptyList<NewsResource>(),
subject.getNewsResourcesStream(
filterTopicIds = emptySet(),
filterAuthorIds = nonPresentInterestsIds
)
.first()

@ -74,22 +74,6 @@ class TestNewsResourceDao : NewsResourceDao {
}
}
override fun getNewsResourcesForTopicsStream(
filterTopicIds: Set<Int>
): Flow<List<PopulatedNewsResource>> =
getNewsResourcesStream()
.map { resources ->
resources.filter { resource -> resource.topics.any { it.id in filterTopicIds } }
}
override fun getNewsResourcesForAuthorsStream(
filterAuthorIds: Set<Int>
): Flow<List<PopulatedNewsResource>> =
getNewsResourcesStream()
.map { resources ->
resources.filter { resource -> resource.authors.any { it.id in filterAuthorIds } }
}
override suspend fun insertOrIgnoreNewsResources(
entities: List<NewsResourceEntity>
): List<Long> {

Loading…
Cancel
Save