Refresh semesters if previous list was downloaded in different m… (#776)

This commit is contained in:
Mikołaj Pich 2020-04-19 23:21:59 +02:00 committed by GitHub
parent 9b18e3669d
commit 9c0e2dc533
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 21 deletions

View File

@ -4,6 +4,7 @@ import com.github.pwittchen.reactivenetwork.library.rx2.ReactiveNetwork
import com.github.pwittchen.reactivenetwork.library.rx2.internet.observing.InternetObservingSettings
import io.github.wulkanowy.data.db.entities.Semester
import io.github.wulkanowy.data.db.entities.Student
import io.github.wulkanowy.sdk.Sdk
import io.github.wulkanowy.utils.getCurrentOrLast
import io.github.wulkanowy.utils.isCurrent
import io.github.wulkanowy.utils.uniqueSubtract
@ -20,10 +21,12 @@ class SemesterRepository @Inject constructor(
) {
fun getSemesters(student: Student, forceRefresh: Boolean = false, refreshOnNoCurrent: Boolean = false): Single<List<Semester>> {
return local.getSemesters(student).filter { !forceRefresh }.filter {
if (refreshOnNoCurrent) {
it.any { semester -> semester.isCurrent }
} else true
return local.getSemesters(student).filter { !forceRefresh }.filter { semesters ->
when {
Sdk.Mode.valueOf(student.loginMode) != Sdk.Mode.API -> semesters.firstOrNull { it.isCurrent }?.diaryId != 0
refreshOnNoCurrent -> semesters.any { semester -> semester.isCurrent }
else -> true
}
}.switchIfEmpty(ReactiveNetwork.checkInternetConnectivity(settings)
.flatMap {
if (it) remote.getSemesters(student)

View File

@ -29,7 +29,7 @@ class StudentLocal @Inject constructor(
fun getStudents(decryptPass: Boolean): Maybe<List<Student>> {
return studentDb.loadAll()
.map { list -> list.map { it.apply { if (decryptPass) password = decrypt(password) } } }
.map { list -> list.map { it.apply { if (decryptPass && Sdk.Mode.valueOf(loginMode) != Sdk.Mode.API) password = decrypt(password) } } }
.filter { it.isNotEmpty() }
}

View File

@ -8,6 +8,7 @@ import io.github.wulkanowy.data.repositories.createSemesterEntity
import io.reactivex.Maybe
import io.reactivex.Single
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotEquals
import org.junit.Before
import org.junit.Test
import org.mockito.Mock
@ -37,13 +38,14 @@ class SemesterRepositoryTest {
fun initTest() {
MockitoAnnotations.initMocks(this)
semesterRepository = SemesterRepository(semesterRemote, semesterLocal, settings)
doReturn("SCRAPPER").`when`(student).loginMode
}
@Test
fun getSemesters_noSemesters() {
val semesters = listOf(
createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(0, 0, now().minusMonths(3), now())
createSemesterEntity(1, 1, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(1, 2, now().minusMonths(3), now())
)
doReturn(Maybe.empty<Semester>()).`when`(semesterLocal).getSemesters(student)
@ -55,11 +57,47 @@ class SemesterRepositoryTest {
verify(semesterLocal).saveSemesters(semesters)
}
@Test
fun getSemesters_invalidDiary_api() {
doReturn("API").`when`(student).loginMode
val badSemesters = listOf(
createSemesterEntity(0, 1, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(0, 2, now().minusMonths(3), now())
)
doReturn(Maybe.just(badSemesters)).`when`(semesterLocal).getSemesters(student)
val items = semesterRepository.getSemesters(student).blockingGet()
assertEquals(2, items.size)
assertEquals(0, items[0].diaryId)
}
@Test
fun getSemesters_invalidDiary_scrapper() {
doReturn("SCRAPPER").`when`(student).loginMode
val badSemesters = listOf(
createSemesterEntity(0, 1, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(0, 2, now().minusMonths(3), now())
)
val goodSemesters = listOf(
createSemesterEntity(1, 1, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(1, 2, now().minusMonths(3), now())
)
doReturn(Maybe.just(badSemesters), Maybe.just(badSemesters), Maybe.just(goodSemesters)).`when`(semesterLocal).getSemesters(student)
doReturn(Single.just(goodSemesters)).`when`(semesterRemote).getSemesters(student)
val items = semesterRepository.getSemesters(student).blockingGet()
assertEquals(2, items.size)
assertNotEquals(0, items[0].diaryId)
}
@Test
fun getSemesters_noCurrent() {
val semesters = listOf(
createSemesterEntity(0, 0, now().minusMonths(12), now().minusMonths(6)),
createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(1))
createSemesterEntity(1, 1, now().minusMonths(12), now().minusMonths(6)),
createSemesterEntity(1, 2, now().minusMonths(6), now().minusMonths(1))
)
doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student)
@ -71,8 +109,8 @@ class SemesterRepositoryTest {
@Test
fun getSemesters_oneCurrent() {
val semesters = listOf(
createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(0, 0, now().minusMonths(3), now())
createSemesterEntity(1, 1, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(1, 2, now().minusMonths(3), now())
)
doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student)
@ -84,8 +122,8 @@ class SemesterRepositoryTest {
@Test
fun getSemesters_doubleCurrent() {
val semesters = listOf(
createSemesterEntity(0, 0, now(), now()),
createSemesterEntity(0, 0, now(), now())
createSemesterEntity(1, 1, now(), now()),
createSemesterEntity(1, 2, now(), now())
)
doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student)
@ -97,8 +135,8 @@ class SemesterRepositoryTest {
@Test
fun getSemesters_noSemesters_refreshOnNoCurrent() {
val semesters = listOf(
createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(0, 0, now().minusMonths(3), now())
createSemesterEntity(1, 1, now().minusMonths(6), now().minusMonths(3)),
createSemesterEntity(1, 2, now().minusMonths(3), now())
)
doReturn(Maybe.empty<Semester>()).`when`(semesterLocal).getSemesters(student)
@ -113,8 +151,8 @@ class SemesterRepositoryTest {
@Test
fun getSemesters_noCurrent_refreshOnNoCurrent() {
val semesters = listOf(
createSemesterEntity(0, 0, now().minusMonths(12), now().minusMonths(6)),
createSemesterEntity(0, 0, now().minusMonths(6), now().minusMonths(1))
createSemesterEntity(1, 1, now().minusMonths(12), now().minusMonths(6)),
createSemesterEntity(1, 2, now().minusMonths(6), now().minusMonths(1))
)
doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student)
@ -127,8 +165,8 @@ class SemesterRepositoryTest {
@Test
fun getSemesters_doubleCurrent_refreshOnNoCurrent() {
val semesters = listOf(
createSemesterEntity(0, 0, now(), now()),
createSemesterEntity(0, 0, now(), now())
createSemesterEntity(1, 1, now(), now()),
createSemesterEntity(1, 2, now(), now())
)
doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student)
@ -140,8 +178,8 @@ class SemesterRepositoryTest {
@Test(expected = IllegalArgumentException::class)
fun getCurrentSemester_doubleCurrent() {
val semesters = listOf(
createSemesterEntity(0, 0, now(), now()),
createSemesterEntity(0, 0, now(), now())
createSemesterEntity(1, 1, now(), now()),
createSemesterEntity(1, 1, now(), now())
)
doReturn(Maybe.just(semesters)).`when`(semesterLocal).getSemesters(student)