From 0b83a66b85a16d5e590078825fee4f6cf461959f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miko=C5=82aj=20Pich?= Date: Tue, 28 Sep 2021 23:10:11 +0200 Subject: [PATCH] Remove disappearing teachers workaround from timetable repository (#1545) --- .../data/repositories/TimetableRepository.kt | 12 +- .../repositories/TimetableRepositoryTest.kt | 129 +----------------- 2 files changed, 3 insertions(+), 138 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/data/repositories/TimetableRepository.kt b/app/src/main/java/io/github/wulkanowy/data/repositories/TimetableRepository.kt index 1540d3cc7..769fa0f0d 100644 --- a/app/src/main/java/io/github/wulkanowy/data/repositories/TimetableRepository.kt +++ b/app/src/main/java/io/github/wulkanowy/data/repositories/TimetableRepository.kt @@ -123,17 +123,7 @@ class TimetableRepository @Inject constructor( lessonsNew: List, ) { val lessonsToRemove = lessonsOld uniqueSubtract lessonsNew - val lessonsToAdd = (lessonsNew uniqueSubtract lessonsOld).map { new -> - val matchingOld = lessonsOld.singleOrNull { new.start == it.start } - if (matchingOld != null) { - val useOldTeacher = new.teacher.isEmpty() && !new.changes && !matchingOld.changes - new.copy( - room = if (new.room.isEmpty()) matchingOld.room else new.room, - teacher = if (useOldTeacher) matchingOld.teacher - else new.teacher - ) - } else new - } + val lessonsToAdd = lessonsNew uniqueSubtract lessonsOld timetableDb.deleteAll(lessonsToRemove) timetableDb.insertAll(lessonsToAdd) diff --git a/app/src/test/java/io/github/wulkanowy/data/repositories/TimetableRepositoryTest.kt b/app/src/test/java/io/github/wulkanowy/data/repositories/TimetableRepositoryTest.kt index 75c75a66c..edb3125eb 100644 --- a/app/src/test/java/io/github/wulkanowy/data/repositories/TimetableRepositoryTest.kt +++ b/app/src/test/java/io/github/wulkanowy/data/repositories/TimetableRepositoryTest.kt @@ -67,136 +67,11 @@ class TimetableRepositoryTest { timetableRepository = TimetableRepository(timetableDb, timetableAdditionalDao, timetableHeaderDao, sdk, timetableNotificationSchedulerHelper, refreshHelper) } - @Test - fun copyRoomToCompletedFromPrevious() { - // prepare - val remoteList = listOf( - createTimetableRemote(of(2021, 1, 4, 8, 0), 1, "", "Przyroda"), - createTimetableRemote(of(2021, 1, 4, 8, 50), 2, "", "Religia"), - createTimetableRemote(of(2021, 1, 4, 9, 40), 3, "", "W-F"), - createTimetableRemote(of(2021, 1, 4, 10, 30), 4, "", "W-F") - ) - coEvery { sdk.getTimetableFull(any(), any()) } returns TimetableFull(emptyList(), remoteList, emptyList()) - - val localList = listOf( - createTimetableRemote(of(2021, 1, 4, 8, 0), 1, "123", "Przyroda"), - createTimetableRemote(of(2021, 1, 4, 8, 50), 2, "321", "Religia"), - createTimetableRemote(of(2021, 1, 4, 9, 40), 3, "213", "W-F"), - createTimetableRemote(of(2021, 1, 4, 10, 30), 3, "213", "W-F", "Jan Kowalski") - ) - coEvery { timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) } returns flowOf(localList.mapToEntities(semester)) - coEvery { timetableDb.insertAll(any()) } returns listOf(1, 2, 3) - coEvery { timetableDb.deleteAll(any()) } just Runs - - coEvery { timetableAdditionalDao.loadAll(1, 1, startDate, endDate) } returns flowOf(listOf()) - coEvery { timetableAdditionalDao.insertAll(emptyList()) } returns listOf(1, 2, 3) - coEvery { timetableAdditionalDao.deleteAll(emptyList()) } just Runs - - coEvery { timetableHeaderDao.loadAll(1, 1, startDate, endDate) } returns flowOf(listOf()) - coEvery { timetableHeaderDao.insertAll(emptyList()) } returns listOf(1, 2, 3) - coEvery { timetableHeaderDao.deleteAll(emptyList()) } just Runs - - // execute - val res = runBlocking { - timetableRepository.getTimetable(student, semester, startDate, endDate, true).toFirstResult() - } - - // verify - assertEquals(4, res.data?.lessons.orEmpty().size) - coVerify { - timetableDb.insertAll(withArg { - assertEquals(4, it.size) - assertEquals("123", it[0].room) - assertEquals("321", it[1].room) - assertEquals("213", it[2].room) - }) - } - coVerify { timetableDb.deleteAll(match { it.size == 4 }) } - } - - @Test - fun copyTeacherToCompletedFromPrevious() { - // prepare - val remoteList = listOf( - createTimetableRemote(of(2021, 1, 4, 8, 0), 1, "123", "Matematyka", "Paweł Poniedziałkowski", false), // skip - createTimetableRemote(of(2021, 1, 4, 8, 50), 2, "124", "Matematyka", "Jakub Wtorkowski", true), - createTimetableRemote(of(2021, 1, 4, 9, 40), 3, "125", "Język polski", "Joanna Poniedziałkowska", false), - createTimetableRemote(of(2021, 1, 4, 10, 40), 4, "126", "Język polski", "Joanna Wtorkowska", true), // skip - - createTimetableRemote(of(2021, 1, 5, 8, 0), 1, "123", "Język polski", "", false), - createTimetableRemote(of(2021, 1, 5, 8, 50), 2, "124", "Język polski", "", true), - createTimetableRemote(of(2021, 1, 5, 9, 40), 3, "125", "Język polski", "", false), - createTimetableRemote(of(2021, 1, 5, 10, 40), 4, "126", "Język polski", "", true), - - createTimetableRemote(of(2021, 1, 6, 8, 0), 1, "123", "Matematyka", "Paweł Środowski", false), - createTimetableRemote(of(2021, 1, 6, 8, 50), 2, "124", "Matematyka", "Paweł Czwartkowski", true), - createTimetableRemote(of(2021, 1, 6, 9, 40), 3, "125", "Matematyka", "Paweł Środowski", false), - createTimetableRemote(of(2021, 1, 6, 10, 40), 4, "126", "Matematyka", "Paweł Czwartkowski", true) - ) - coEvery { sdk.getTimetableFull(startDate, endDate) } returns TimetableFull(emptyList(), remoteList, emptyList()) - - val localList = listOf( - createTimetableRemote(of(2021, 1, 4, 8, 0), 1, "123", "Matematyka", "Paweł Poniedziałkowski", false), - createTimetableRemote(of(2021, 1, 4, 8, 50), 2, "124", "Matematyka", "Paweł Poniedziałkowski", false), - createTimetableRemote(of(2021, 1, 4, 9, 40), 3, "125", "Język polski", "Joanna Wtorkowska", true), - createTimetableRemote(of(2021, 1, 4, 10, 40), 4, "126", "Język polski", "Joanna Wtorkowska", true), - - createTimetableRemote(of(2021, 1, 5, 8, 0), 1, "123", "Język polski", "Joanna Wtorkowska", false), - createTimetableRemote(of(2021, 1, 5, 8, 50), 2, "124", "Język polski", "Joanna Wtorkowska", false), - createTimetableRemote(of(2021, 1, 5, 9, 40), 3, "125", "Język polski", "Joanna Środowska", true), - createTimetableRemote(of(2021, 1, 5, 10, 40), 4, "126", "Język polski", "Joanna Środowska", true), - - createTimetableRemote(of(2021, 1, 6, 8, 0), 1, "123", "Matematyka", "", false), - createTimetableRemote(of(2021, 1, 6, 8, 50), 2, "124", "Matematyka", "", false), - createTimetableRemote(of(2021, 1, 6, 9, 40), 3, "125", "Matematyka", "", true), - createTimetableRemote(of(2021, 1, 6, 10, 40), 4, "126", "Matematyka", "", true) - ) - coEvery { timetableDb.loadAll(semester.diaryId, semester.studentId, startDate, endDate) } returns flowOf(localList.mapToEntities(semester)) - coEvery { timetableDb.insertAll(any()) } returns listOf(1, 2, 3) - coEvery { timetableDb.deleteAll(any()) } just Runs - - coEvery { timetableAdditionalDao.loadAll(1, 1, startDate, endDate) } returns flowOf(listOf()) - coEvery { timetableAdditionalDao.insertAll(emptyList()) } returns listOf(1, 2, 3) - coEvery { timetableAdditionalDao.deleteAll(emptyList()) } just Runs - - coEvery { timetableHeaderDao.loadAll(1, 1, startDate, endDate) } returns flowOf(listOf()) - coEvery { timetableHeaderDao.insertAll(emptyList()) } returns listOf(1, 2, 3) - coEvery { timetableHeaderDao.deleteAll(emptyList()) } just Runs - - // execute - val res = runBlocking { timetableRepository.getTimetable(student, semester, startDate, endDate, true).toFirstResult() } - - // verify - assertEquals(null, res.error) - assertEquals(12, res.data!!.lessons.size) - - coVerify { - timetableDb.insertAll(withArg { - assertEquals(10, it.size) -// assertEquals("Paweł Poniedziałkowski", it[0].teacher) // skip - assertEquals("Jakub Wtorkowski", it[0].teacher) - assertEquals("Joanna Poniedziałkowska", it[1].teacher) -// assertEquals("Joanna Wtorkowska", it[3].teacher) // skip - - assertEquals("Joanna Wtorkowska", it[2].teacher) - assertEquals("", it[3].teacher) - assertEquals("", it[4].teacher) - assertEquals("", it[5].teacher) - - assertEquals("Paweł Środowski", it[6].teacher) - assertEquals("Paweł Czwartkowski", it[7].teacher) - assertEquals("Paweł Środowski", it[8].teacher) - assertEquals("Paweł Czwartkowski", it[9].teacher) - }) - } - coVerify { timetableDb.deleteAll(match { it.size == 10 }) } - } - @Test fun `force refresh without difference`() { val remoteList = listOf( - createTimetableRemote(of(2021, 1, 4, 8, 0), 1, "123", "Język polski", "", false), - createTimetableRemote(of(2021, 1, 4, 8, 50), 2, "124", "Język polski", "", true) + createTimetableRemote(of(2021, 1, 4, 8, 0), 1, "123", "Język polski", "Jan Kowalski", false), + createTimetableRemote(of(2021, 1, 4, 8, 50), 2, "124", "Język niemiecki", "Joanna Czarniecka", true) ) // prepare