1
0
mirror of https://github.com/wulkanowy/wulkanowy.git synced 2025-01-18 17:46:44 -06:00

Fix calc vulcan average in second semester (#1779)

This commit is contained in:
Rafał Borcz 2022-02-12 22:22:15 +01:00 committed by GitHub
parent 18568c86be
commit edd1c9442e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 17 deletions

View File

@ -10,9 +10,7 @@ import io.github.wulkanowy.data.repositories.GradeRepository
import io.github.wulkanowy.data.repositories.PreferencesRepository
import io.github.wulkanowy.data.repositories.SemesterRepository
import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.ui.modules.grade.GradeAverageMode.ALL_YEAR
import io.github.wulkanowy.ui.modules.grade.GradeAverageMode.BOTH_SEMESTERS
import io.github.wulkanowy.ui.modules.grade.GradeAverageMode.ONE_SEMESTER
import io.github.wulkanowy.ui.modules.grade.GradeAverageMode.*
import io.github.wulkanowy.utils.calcAverage
import io.github.wulkanowy.utils.changeModifier
import io.github.wulkanowy.utils.flowWithResourceIn
@ -144,20 +142,20 @@ class GradeAverageProvider @Inject constructor(
isGradeAverageForceCalc: Boolean,
secondSemesterSubject: GradeSubject,
firstSemesterSubject: GradeSubject?
): Double {
): Double = if (!isAnyVulcanAverage || isGradeAverageForceCalc) {
val divider = if (secondSemesterSubject.grades.any { it.weightValue > .0 }) 2 else 1
return if (!isAnyVulcanAverage || isGradeAverageForceCalc) {
val secondSemesterAverage =
secondSemesterSubject.grades.updateModifiers(student)
.calcAverage(isOptionalArithmeticAverage)
val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student)
?.calcAverage(isOptionalArithmeticAverage) ?: secondSemesterAverage
val secondSemesterAverage = secondSemesterSubject.grades.updateModifiers(student)
.calcAverage(isOptionalArithmeticAverage)
val firstSemesterAverage = firstSemesterSubject?.grades?.updateModifiers(student)
?.calcAverage(isOptionalArithmeticAverage) ?: secondSemesterAverage
(secondSemesterAverage + firstSemesterAverage) / divider
} else {
(secondSemesterSubject.average + (firstSemesterSubject?.average ?: secondSemesterSubject.average)) / divider
}
(secondSemesterAverage + firstSemesterAverage) / divider
} else {
val divider = if (secondSemesterSubject.average > 0) 2 else 1
(secondSemesterSubject.average + (firstSemesterSubject?.average
?: secondSemesterSubject.average)) / divider
}
private fun getGradeSubjects(

View File

@ -859,12 +859,51 @@ class GradeAverageProviderTest {
) to listOf(getSummary(semesterId = 23, subject = "Fizyka", average = .0))
}
val items = runBlocking { gradeAverageProvider.getGradesDetailsWithAverage(student, semesters[2].semesterId, true).getResult() }
val items = runBlocking {
gradeAverageProvider.getGradesDetailsWithAverage(
student,
semesters[2].semesterId,
true
).getResult()
}
assertEquals(5.5555, items.single { it.subject == "Fizyka" }.average, .0001) // (from details): 5.72727272 + 4,8 → .average()
assertEquals(
5.5555,
items.single { it.subject == "Fizyka" }.average,
.0001
) // (from details): 5.72727272 + 4,8 → .average()
}
private fun getGrade(semesterId: Int, subject: String, value: Double, modifier: Double = 0.0, weight: Double = 1.0, entry: String = ""): Grade {
@Test
fun `calc both semesters average when both summary have same average from vulcan and second semester has no grades`() {
every { preferencesRepository.gradeAverageForceCalc } returns false
every { preferencesRepository.gradeAverageMode } returns GradeAverageMode.BOTH_SEMESTERS
every { preferencesRepository.isOptionalArithmeticAverage } returns false
coEvery { gradeRepository.getGrades(student, semesters[1], true) } returns
flowWithResource { firstGrades to firstSummaries }
coEvery { gradeRepository.getGrades(student, semesters[2], true) } returns
flowWithResource { listOf<Grade>() to firstSummaries }
val items = runBlocking {
gradeAverageProvider.getGradesDetailsWithAverage(
student,
semesters[2].semesterId,
true
).getResult()
}
assertEquals(3.1, items.single { it.subject == "Fizyka" }.average, .0001)
}
private fun getGrade(
semesterId: Int,
subject: String,
value: Double,
modifier: Double = 0.0,
weight: Double = 1.0,
entry: String = ""
): Grade {
return Grade(
studentId = 101,
semesterId = semesterId,