Fix instrumentation tests

Although they are very old, at least they do not break anymore in case
anyone wants to execute them.
This commit is contained in:
Adolfo Santiago 2022-05-28 09:05:51 +02:00
parent 3b268ae745
commit 249cdf2ddf
No known key found for this signature in database
GPG Key ID: 244D6F9A317B4A65
3 changed files with 131 additions and 111 deletions

View File

@ -3,7 +3,7 @@ package com.keylesspalace.tusky
import androidx.room.testing.MigrationTestHelper import androidx.room.testing.MigrationTestHelper
import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory import androidx.sqlite.db.framework.FrameworkSQLiteOpenHelperFactory
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import androidx.test.ext.junit.runners.AndroidJUnit4 import androidx.test.runner.AndroidJUnit4
import com.keylesspalace.tusky.db.AppDatabase import com.keylesspalace.tusky.db.AppDatabase
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Rule import org.junit.Rule
@ -61,4 +61,4 @@ class MigrationsTest {
assertEquals(accountId, cursor.getString(4)) assertEquals(accountId, cursor.getString(4))
assertEquals(username, cursor.getString(5)) assertEquals(username, cursor.getString(5))
} }
} }

View File

@ -1,10 +1,15 @@
package com.keylesspalace.tusky package com.keylesspalace.tusky
import androidx.room.Room import androidx.room.Room
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry import androidx.test.platform.app.InstrumentationRegistry
import com.keylesspalace.tusky.db.* import androidx.test.runner.AndroidJUnit4
import com.keylesspalace.tusky.db.AppDatabase
import com.keylesspalace.tusky.db.TimelineAccountEntity
import com.keylesspalace.tusky.db.TimelineDao
import com.keylesspalace.tusky.db.TimelineStatusEntity
import com.keylesspalace.tusky.db.TimelineStatusWithAccount
import com.keylesspalace.tusky.entity.Status import com.keylesspalace.tusky.entity.Status
import com.keylesspalace.tusky.entity.Status.Visibility.PRIVATE
import com.keylesspalace.tusky.repository.TimelineRepository import com.keylesspalace.tusky.repository.TimelineRepository
import org.junit.After import org.junit.After
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
@ -37,16 +42,18 @@ class TimelineDAOTest {
val ignoredOne = makeStatus(statusId = 1) val ignoredOne = makeStatus(statusId = 1)
val ignoredTwo = makeStatus(accountId = 2) val ignoredTwo = makeStatus(accountId = 2)
for ((status, author, reblogger) in listOf(setOne, setTwo, ignoredOne, ignoredTwo)) { for((status, author, reblogger) in listOf(setOne, setTwo, ignoredOne, ignoredTwo)) {
timelineDao.insertInTransaction(status, author, reblogger) timelineDao.insertInTransaction(status, author, reblogger)
} }
val resultsFromDb = timelineDao.getStatusesForAccount(setOne.first.timelineUserId, val resultsFromDb = timelineDao.getStatusesForAccount(
maxId = "21", sinceId = ignoredOne.first.serverId, limit = 10) setOne.first.timelineUserId,
.blockingGet() maxId = "21", sinceId = ignoredOne.first.serverId, limit = 10
)
.blockingGet()
assertEquals(2, resultsFromDb.size) assertEquals(2, resultsFromDb.size)
for ((set, fromDb) in listOf(setTwo, setOne).zip(resultsFromDb)) { for((set, fromDb) in listOf(setTwo, setOne).zip(resultsFromDb)) {
val (status, author, reblogger) = set val (status, author, reblogger) = set
assertEquals(status, fromDb.status) assertEquals(status, fromDb.status)
assertEquals(author, fromDb.account) assertEquals(author, fromDb.account)
@ -64,7 +71,7 @@ class TimelineDAOTest {
timelineDao.insertStatusIfNotThere(placeholder) timelineDao.insertStatusIfNotThere(placeholder)
val fromDb = timelineDao.getStatusesForAccount(status.timelineUserId, null, null, 10) val fromDb = timelineDao.getStatusesForAccount(status.timelineUserId, null, null, 10)
.blockingGet() .blockingGet()
val result = fromDb.first() val result = fromDb.first()
assertEquals(1, fromDb.size) assertEquals(1, fromDb.size)
@ -79,40 +86,45 @@ class TimelineDAOTest {
val now = System.currentTimeMillis() val now = System.currentTimeMillis()
val oldDate = now - TimelineRepository.CLEANUP_INTERVAL - 20_000 val oldDate = now - TimelineRepository.CLEANUP_INTERVAL - 20_000
val oldThisAccount = makeStatus( val oldThisAccount = makeStatus(
statusId = 5, statusId = 5,
createdAt = oldDate createdAt = oldDate
) )
val oldAnotherAccount = makeStatus( val oldAnotherAccount = makeStatus(
statusId = 10, statusId = 10,
createdAt = oldDate, createdAt = oldDate,
accountId = 2 accountId = 2
) )
val recentThisAccount = makeStatus( val recentThisAccount = makeStatus(
statusId = 30, statusId = 30,
createdAt = System.currentTimeMillis() createdAt = System.currentTimeMillis()
) )
val recentAnotherAccount = makeStatus( val recentAnotherAccount = makeStatus(
statusId = 60, statusId = 60,
createdAt = System.currentTimeMillis(), createdAt = System.currentTimeMillis(),
accountId = 2 accountId = 2
) )
for ((status, author, reblogAuthor) in listOf(oldThisAccount, oldAnotherAccount, recentThisAccount, recentAnotherAccount)) { for((status, author, reblogAuthor) in listOf(
oldThisAccount,
oldAnotherAccount,
recentThisAccount,
recentAnotherAccount
)) {
timelineDao.insertInTransaction(status, author, reblogAuthor) timelineDao.insertInTransaction(status, author, reblogAuthor)
} }
timelineDao.cleanup(now - TimelineRepository.CLEANUP_INTERVAL) timelineDao.cleanup(now - TimelineRepository.CLEANUP_INTERVAL)
assertEquals( assertEquals(
listOf(recentThisAccount), listOf(recentThisAccount),
timelineDao.getStatusesForAccount(1, null, null, 100).blockingGet() timelineDao.getStatusesForAccount(1, null, null, 100).blockingGet()
.map { it.toTriple() } .map { it.toTriple() }
) )
assertEquals( assertEquals(
listOf(recentAnotherAccount), listOf(recentAnotherAccount),
timelineDao.getStatusesForAccount(2, null, null, 100).blockingGet() timelineDao.getStatusesForAccount(2, null, null, 100).blockingGet()
.map { it.toTriple() } .map { it.toTriple() }
) )
} }
@ -120,128 +132,136 @@ class TimelineDAOTest {
fun overwriteDeletedStatus() { fun overwriteDeletedStatus() {
val oldStatuses = listOf( val oldStatuses = listOf(
makeStatus(statusId = 3), makeStatus(statusId = 3),
makeStatus(statusId = 2), makeStatus(statusId = 2),
makeStatus(statusId = 1) makeStatus(statusId = 1)
) )
timelineDao.deleteRange(1, oldStatuses.last().first.serverId, oldStatuses.first().first.serverId) timelineDao.deleteRange(
1,
oldStatuses.last().first.serverId,
oldStatuses.first().first.serverId
)
for ((status, author, reblogAuthor) in oldStatuses) { for((status, author, reblogAuthor) in oldStatuses) {
timelineDao.insertInTransaction(status, author, reblogAuthor) timelineDao.insertInTransaction(status, author, reblogAuthor)
} }
// status 2 gets deleted, newly loaded status contain only 1 + 3 // status 2 gets deleted, newly loaded status contain only 1 + 3
val newStatuses = listOf( val newStatuses = listOf(
makeStatus(statusId = 3), makeStatus(statusId = 3),
makeStatus(statusId = 1) makeStatus(statusId = 1)
) )
timelineDao.deleteRange(1, newStatuses.last().first.serverId, newStatuses.first().first.serverId) timelineDao.deleteRange(
1,
newStatuses.last().first.serverId,
newStatuses.first().first.serverId
)
for ((status, author, reblogAuthor) in newStatuses) { for((status, author, reblogAuthor) in newStatuses) {
timelineDao.insertInTransaction(status, author, reblogAuthor) timelineDao.insertInTransaction(status, author, reblogAuthor)
} }
//make sure status 2 is no longer in db //make sure status 2 is no longer in db
assertEquals( assertEquals(
newStatuses, newStatuses,
timelineDao.getStatusesForAccount(1, null, null, 100).blockingGet() timelineDao.getStatusesForAccount(1, null, null, 100).blockingGet()
.map { it.toTriple() } .map { it.toTriple() }
) )
} }
private fun makeStatus( private fun makeStatus(
accountId: Long = 1, accountId: Long = 1,
statusId: Long = 10, statusId: Long = 10,
reblog: Boolean = false, reblog: Boolean = false,
createdAt: Long = statusId, createdAt: Long = statusId,
authorServerId: String = "20" authorServerId: String = "20"
): Triple<TimelineStatusEntity, TimelineAccountEntity, TimelineAccountEntity?> { ): Triple<TimelineStatusEntity, TimelineAccountEntity, TimelineAccountEntity?> {
val author = TimelineAccountEntity( val author = TimelineAccountEntity(
authorServerId, authorServerId,
accountId, accountId,
"localUsername", "localUsername",
"username", "username",
"displayName", "displayName",
"blah", "blah",
"avatar", "avatar",
"[\"tusky\": \"http://tusky.cool/emoji.jpg\"]", "[\"tusky\": \"http://tusky.cool/emoji.jpg\"]",
false false
) )
val reblogAuthor = if (reblog) { val reblogAuthor = if(reblog) {
TimelineAccountEntity( TimelineAccountEntity(
"R$authorServerId", "R$authorServerId",
accountId, accountId,
"RlocalUsername", "RlocalUsername",
"Rusername", "Rusername",
"RdisplayName", "RdisplayName",
"Rblah", "Rblah",
"Ravatar", "Ravatar",
"[]", "[]",
false false
) )
} else null } else null
val even = accountId % 2 == 0L val even = accountId % 2 == 0L
val status = TimelineStatusEntity( val status = TimelineStatusEntity(
serverId = statusId.toString(), serverId = statusId.toString(),
url = "url$statusId", url = "url$statusId",
timelineUserId = accountId, timelineUserId = accountId,
authorServerId = authorServerId, authorServerId = authorServerId,
inReplyToId = "inReplyToId$statusId", inReplyToId = "inReplyToId$statusId",
inReplyToAccountId = "inReplyToAccountId$statusId", inReplyToAccountId = "inReplyToAccountId$statusId",
content = "Content!$statusId", content = "Content!$statusId",
createdAt = createdAt, createdAt = createdAt,
emojis = "emojis$statusId", emojis = "emojis$statusId",
reblogsCount = 1 * statusId.toInt(), reblogsCount = 1 * statusId.toInt(),
favouritesCount = 2 * statusId.toInt(), favouritesCount = 2 * statusId.toInt(),
reblogged = even, reblogged = even,
bookmarked = !even, bookmarked = !even,
favourited = even, favourited = even,
sensitive = !even, sensitive = !even,
spoilerText = "spoier$statusId", spoilerText = "spoier$statusId",
visibility = Status.Visibility.PRIVATE, visibility = PRIVATE,
attachments = "attachments$accountId", attachments = "attachments$accountId",
mentions = "mentions$accountId", mentions = "mentions$accountId",
application = "application$accountId", application = "application$accountId",
reblogServerId = if (reblog) (statusId * 100).toString() else null, reblogServerId = if(reblog) (statusId * 100).toString() else null,
reblogAccountId = reblogAuthor?.serverId, reblogAccountId = reblogAuthor?.serverId,
poll = null, poll = null,
muted = false pleroma = null
) )
return Triple(status, author, reblogAuthor) return Triple(status, author, reblogAuthor)
} }
private fun createPlaceholder(serverId: String, timelineUserId: Long): TimelineStatusEntity { private fun createPlaceholder(serverId: String, timelineUserId: Long): TimelineStatusEntity {
return TimelineStatusEntity( return TimelineStatusEntity(
serverId = serverId, serverId = serverId,
url = null, url = null,
timelineUserId = timelineUserId, timelineUserId = timelineUserId,
authorServerId = null, authorServerId = null,
inReplyToId = null, inReplyToId = null,
inReplyToAccountId = null, inReplyToAccountId = null,
content = null, content = null,
createdAt = 0L, createdAt = 0L,
emojis = null, emojis = null,
reblogsCount = 0, reblogsCount = 0,
favouritesCount = 0, favouritesCount = 0,
reblogged = false, reblogged = false,
bookmarked = false, bookmarked = false,
favourited = false, favourited = false,
sensitive = false, sensitive = false,
spoilerText = null, spoilerText = null,
visibility = null, visibility = null,
attachments = null, attachments = null,
mentions = null, mentions = null,
application = null, application = null,
reblogServerId = null, reblogServerId = null,
reblogAccountId = null, reblogAccountId = null,
poll = null, poll = null,
muted = false pleroma = null
) )
} }