Fix selecting student mailbox based on studentName field (#1958)

This commit is contained in:
Mikołaj Pich 2022-08-30 09:07:07 +02:00 committed by GitHub
parent f2cb3b4f9e
commit 535206056d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 141 additions and 7 deletions

View File

@ -186,7 +186,7 @@ ext {
}
dependencies {
implementation "io.github.wulkanowy:sdk:1.7.0"
implementation "io.github.wulkanowy:sdk:09e5215894"
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:1.1.8'

View File

@ -11,7 +11,4 @@ interface MailboxDao : BaseDao<Mailbox> {
@Query("SELECT * FROM Mailboxes WHERE userLoginId = :userLoginId ")
suspend fun loadAll(userLoginId: Int): List<Mailbox>
@Query("SELECT * FROM Mailboxes WHERE userLoginId = :userLoginId AND studentName = :studentName ")
suspend fun load(userLoginId: Int, studentName: String): Mailbox?
}

View File

@ -32,17 +32,22 @@ class MailboxRepository @Inject constructor(
suspend fun getMailbox(student: Student): Mailbox {
val isExpired = refreshHelper.shouldBeRefreshed(getRefreshKey(cacheKey, student))
val mailbox = mailboxDao.load(student.userLoginId, student.studentName)
val mailboxes = mailboxDao.loadAll(student.userLoginId)
val mailbox = mailboxes.filterByStudent(student)
return if (isExpired || mailbox == null) {
refreshMailboxes(student)
val newMailbox = mailboxDao.load(student.userLoginId, student.studentName)
val newMailbox = mailboxDao.loadAll(student.userLoginId).filterByStudent(student)
requireNotNull(newMailbox) {
"Mailbox for ${student.userName} - ${student.studentName} not found!"
"Mailbox for ${student.userName} - ${student.studentName} not found! Saved mailboxes: $mailboxes"
}
newMailbox
} else mailbox
}
private fun List<Mailbox>.filterByStudent(student: Student): Mailbox? = find {
it.studentName.trim() == student.studentName.trim()
}
}

View File

@ -0,0 +1,132 @@
package io.github.wulkanowy.data.repositories
import io.github.wulkanowy.data.db.dao.MailboxDao
import io.github.wulkanowy.data.db.entities.Mailbox
import io.github.wulkanowy.data.db.entities.MailboxType
import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.AutoRefreshHelper
import io.mockk.MockKAnnotations
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import io.mockk.impl.annotations.SpyK
import io.mockk.just
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import java.time.Instant
@OptIn(ExperimentalCoroutinesApi::class)
class MailboxRepositoryTest {
@SpyK
private var sdk = Sdk()
@MockK
private lateinit var mailboxDao: MailboxDao
@MockK
private lateinit var refreshHelper: AutoRefreshHelper
private lateinit var systemUnderTest: MailboxRepository
@Before
fun setUp() {
MockKAnnotations.init(this)
coEvery { refreshHelper.shouldBeRefreshed(any()) } returns false
coEvery { refreshHelper.updateLastRefreshTimestamp(any()) } just Runs
coEvery { mailboxDao.deleteAll(any()) } just Runs
coEvery { mailboxDao.insertAll(any()) } returns emptyList()
coEvery { mailboxDao.loadAll(any()) } returns emptyList()
coEvery { sdk.getMailboxes() } returns emptyList()
systemUnderTest = MailboxRepository(
mailboxDao = mailboxDao,
sdk = sdk,
refreshHelper = refreshHelper,
)
}
@Test(expected = IllegalArgumentException::class)
fun `get mailbox that doesn't exist`() = runTest {
val student = getStudentEntity(
userName = "Stanisław Kowalski",
studentName = "Jan Kowalski",
)
coEvery { sdk.getMailboxes() } returns emptyList()
systemUnderTest.getMailbox(student)
}
@Test
fun `get mailbox for user with additional spaces`() = runTest {
val student = getStudentEntity(
userName = " Stanisław Kowalski ",
studentName = " Jan Kowalski ",
)
val expectedMailbox = getMailboxEntity("Jan Kowalski ")
coEvery { mailboxDao.loadAll(any()) } returns listOf(
expectedMailbox,
)
val selectedMailbox = systemUnderTest.getMailbox(student)
assertEquals(expectedMailbox, selectedMailbox)
}
@Test(expected = IllegalArgumentException::class)
fun `get mailbox for student with strange name`() = runTest {
val student = getStudentEntity(
userName = "Stanisław Kowalski",
studentName = "J**** K*****",
)
val expectedMailbox = getMailboxEntity("Jan Kowalski")
coEvery { mailboxDao.loadAll(any()) } returns listOf(
expectedMailbox,
)
systemUnderTest.getMailbox(student)
}
private fun getMailboxEntity(
studentName: String,
) = Mailbox(
globalKey = "",
fullName = "",
userName = "",
userLoginId = 123,
studentName = studentName,
schoolNameShort = "",
type = MailboxType.STUDENT,
)
private fun getStudentEntity(
studentName: String,
userName: String,
) = Student(
scrapperBaseUrl = "http://fakelog.cf",
email = "jan@fakelog.cf",
certificateKey = "",
classId = 0,
className = "",
isCurrent = false,
isParent = false,
loginMode = Sdk.Mode.API.name,
loginType = Sdk.ScrapperLoginType.STANDARD.name,
mobileBaseUrl = "",
password = "",
privateKey = "",
registrationDate = Instant.now(),
schoolName = "",
schoolShortName = "test",
schoolSymbol = "",
studentId = 1,
studentName = studentName,
symbol = "",
userLoginId = 1,
userName = userName,
)
}