Throw error if there is no matching eduOne student in getSemesters() method

This commit is contained in:
Mikołaj Pich 2024-03-25 23:49:17 +01:00
parent a324bee117
commit 35dec57cb6
No known key found for this signature in database
5 changed files with 64 additions and 4 deletions

View file

@ -18,7 +18,7 @@ ext {
moshi = "1.13.0"
}
version = "2.5.3"
version = "2.5.4-SNAPSHOT"
group = "io.github.wulkanowy"
nexusPublishing {

View file

@ -123,8 +123,8 @@ internal class StudentPlusRepository(
suspend fun getSemesters(studentId: Int, diaryId: Int, unitId: Int): List<Semester> {
val student = api.getContext().students.find {
val key = getDecodedKey(it.key)
key.studentId == studentId && key.diaryId == diaryId && key.unitId == unitId
} ?: return emptyList()
key.studentId == studentId
} ?: throw NoSuchElementException()
val level = student.className.takeWhile { it.isDigit() }
return api.getSemesters(student.key, diaryId).map {
Semester(

View file

@ -42,6 +42,10 @@ abstract class BaseLocalTest : BaseTest() {
)
}
fun MockWebServer.enqueue(content: String, responseCode: Int = 200) {
enqueue(MockResponse().setBody(content).setResponseCode(responseCode))
}
@After
fun tearDown() {
server.shutdown()

View file

@ -0,0 +1,43 @@
package io.github.wulkanowy.sdk.scrapper.register
import io.github.wulkanowy.sdk.scrapper.BaseLocalTest
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
class SemestersPlusTest : BaseLocalTest() {
@Test
fun `get semesters when diary and unit id are not set`() = runTest {
val repo = getStudentPlusRepo {
// ~ echo "MS0yLTEtMw==" | base64 -d
// 1-2-1-3
it.enqueue("Context-all-disabled.json", RegisterTest::class.java)
it.enqueue("OkresyKlasyfikacyjne.json", RegisterTest::class.java)
}
val semesters = repo.getSemesters(1, 0, 0)
assertEquals(2, semesters.size)
}
@Test(expected = NoSuchElementException::class)
fun `get semesters when there is no matching student list`() = runTest {
val repo = getStudentPlusRepo {
// ~ echo "MS0yLTEtMw==" | base64 -d
// 1-2-1-3
it.enqueue("Context-all-disabled.json", RegisterTest::class.java)
}
repo.getSemesters(2, 0, 0)
}
@Test
fun `get semesters when there is empty semesters list`() = runTest {
val repo = getStudentPlusRepo {
it.enqueue("Context-all-disabled.json", RegisterTest::class.java)
it.enqueue("[]")
}
val semesters = repo.getSemesters(1, 0, 0)
assertEquals(0, semesters.size)
}
}

View file

@ -1 +1,14 @@
[]
[
{
"numerOkresu": 1,
"dataOd": "2023-09-01T00:00:00+02:00",
"dataDo": "2024-01-31T00:00:00+01:00",
"id": 12
},
{
"numerOkresu": 2,
"dataOd": "2024-02-01T00:00:00+01:00",
"dataDo": "2024-08-31T00:00:00+02:00",
"id": 13
}
]