Add support for match mailboxes with more different names (#1961)

This commit is contained in:
Mikołaj Pich 2022-09-01 14:55:00 +02:00 committed by GitHub
parent e574e5e2ec
commit 6153c7b97d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 91 additions and 5 deletions

View File

@ -47,7 +47,37 @@ class MailboxRepository @Inject constructor(
} else mailbox
}
private fun List<Mailbox>.filterByStudent(student: Student): Mailbox? = find {
it.studentName.trim() == student.studentName.trim()
private fun List<Mailbox>.filterByStudent(student: Student): Mailbox? {
val normalizedStudentName = student.studentName.normalizeStudentName()
return find {
it.studentName.normalizeStudentName() == normalizedStudentName
} ?: singleOrNull {
it.studentName.getFirstAndLastPart() == normalizedStudentName.getFirstAndLastPart()
} ?: singleOrNull {
it.studentName.getUnauthorizedVersion() == normalizedStudentName
}
}
private fun String.normalizeStudentName(): String {
return trim().split(" ").joinToString(" ") { part ->
part.lowercase().replaceFirstChar { it.uppercase() }
}
}
private fun String.getFirstAndLastPart(): String {
val parts = normalizeStudentName().split(" ")
val endParts = parts.filterIndexed { i, _ ->
i == 0 || parts.size == i - 1
}
return endParts.joinToString(" ")
}
private fun String.getUnauthorizedVersion(): String {
return normalizeStudentName().split(" ")
.joinToString(" ") {
it.first() + "*".repeat(it.length - 1)
}
}
}

View File

@ -77,20 +77,76 @@ class MailboxRepositoryTest {
assertEquals(expectedMailbox, selectedMailbox)
}
@Test(expected = IllegalArgumentException::class)
fun `get mailbox for student with strange name`() = runTest {
@Test
fun `get mailbox for unique non-authorized student`() = runTest {
val student = getStudentEntity(
userName = "Stanisław Kowalski",
studentName = "J**** K*****",
studentName = "J** K*******",
)
val expectedMailbox = getMailboxEntity("Jan Kowalski")
coEvery { mailboxDao.loadAll(any()) } returns listOf(
expectedMailbox,
)
assertEquals(expectedMailbox, systemUnderTest.getMailbox(student))
}
@Test(expected = IllegalArgumentException::class)
fun `get mailbox for not-unique non-authorized student`() = runTest {
val student = getStudentEntity(
userName = "Stanisław Kowalski",
studentName = "J** K*******",
)
coEvery { mailboxDao.loadAll(any()) } returns listOf(
getMailboxEntity("Jan Kowalski"),
getMailboxEntity("Jan Kurowski"),
)
systemUnderTest.getMailbox(student)
}
@Test
fun `get mailbox for student with uppercase name`() = runTest {
val student = getStudentEntity(
userName = "Mochoń Julia",
studentName = "KLAUDIA MOCHOŃ",
)
val expectedMailbox = getMailboxEntity("Klaudia Mochoń")
coEvery { mailboxDao.loadAll(any()) } returns listOf(
expectedMailbox,
)
assertEquals(expectedMailbox, systemUnderTest.getMailbox(student))
}
@Test
fun `get mailbox for student with second name`() = runTest {
val student = getStudentEntity(
userName = "Fistaszek Karolina",
studentName = "Julia Fistaszek",
)
val expectedMailbox = getMailboxEntity("Julia Maria Fistaszek")
coEvery { mailboxDao.loadAll(any()) } returns listOf(
expectedMailbox,
)
assertEquals(expectedMailbox, systemUnderTest.getMailbox(student))
}
@Test
fun `get mailbox for student with second name and uppercase`() = runTest {
val student = getStudentEntity(
userName = "BEDNAREK KAMIL",
studentName = "ALEKSANDRA BEDNAREK",
)
val expectedMailbox = getMailboxEntity("Aleksandra Anna Bednarek")
coEvery { mailboxDao.loadAll(any()) } returns listOf(
expectedMailbox,
)
assertEquals(expectedMailbox, systemUnderTest.getMailbox(student))
}
private fun getMailboxEntity(
studentName: String,
) = Mailbox(