Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

Clean up outdated family tests in recycle bin on App start (EXPOSUREAPP-12481) #5008

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -121,25 +121,4 @@ class FamilyTestDatabaseTest : BaseTestInstrumentation() {
dao.getAllActive().first().size shouldBe 0
dao.getAllInRecycleBin().first().size shouldBe 0
}

@Test
fun testDeleteAfterRetention() = runBlocking {
dao.insert(test.toEntity())
dao.insert(test2.toEntity())

dao.getAllActive().first().size shouldBe 2
dao.getAllInRecycleBin().first().size shouldBe 0

dao.update(identifier) {
it.moveToRecycleBin(now)
}

dao.getAllActive().first().size shouldBe 1
dao.getAllInRecycleBin().first().size shouldBe 1

dao.deleteFromRecycleBin(now.millis + 1)

dao.getAllActive().first().size shouldBe 1
dao.getAllInRecycleBin().first().size shouldBe 0
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ interface FamilyCoronaTestDao {
@Query("SELECT * FROM family_corona_test WHERE moved_to_recycle_bin_at_millis IS NOT NULL")
fun getAllInRecycleBin(): Flow<List<FamilyCoronaTestEntity?>>

@Query("DELETE FROM family_corona_test WHERE moved_to_recycle_bin_at_millis < :olderThanMillis")
suspend fun deleteFromRecycleBin(olderThanMillis: Long)

@Query("DELETE FROM family_corona_test")
suspend fun deleteAll()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ package de.rki.coronawarnapp.recyclebin.cleanup
import de.rki.coronawarnapp.coronatest.type.PersonalCoronaTest
import de.rki.coronawarnapp.covidcertificate.common.certificate.CwaCovidCertificate
import de.rki.coronawarnapp.covidcertificate.common.repository.CertificateContainerId
import de.rki.coronawarnapp.familytest.core.model.FamilyCoronaTest
import de.rki.coronawarnapp.reyclebin.cleanup.RecycleBinCleanUpService
import de.rki.coronawarnapp.reyclebin.coronatest.RecycledCoronaTestsProvider
import de.rki.coronawarnapp.reyclebin.covidcertificate.RecycledCertificatesProvider
import de.rki.coronawarnapp.util.TimeStamper
import io.mockk.MockKAnnotations
import io.mockk.Runs
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.impl.annotations.RelaxedMockK
import io.mockk.just
import io.mockk.mockk
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runBlockingTest
Expand All @@ -34,6 +38,8 @@ class RecycleBinCleanUpServiceTest : BaseTest() {
MockKAnnotations.init(this)

every { timeStamper.nowUTC } returns Instant.parse("2021-10-13T12:00:00.000Z")

coEvery { recycledCoronaTestsProvider.deleteCoronaTest(any()) } just Runs
}

private fun createInstance() = RecycleBinCleanUpService(
Expand All @@ -53,12 +59,18 @@ class RecycleBinCleanUpServiceTest : BaseTest() {
}

private fun createTest(days: Int) = createTest(recycleTime = now.minus(Days.days(days).toStandardDuration()))
private fun familyTest(days: Int) = createFamilyTest(recycleTime = now.minus(Days.days(days).toStandardDuration()))

private fun createTest(recycleTime: Instant): PersonalCoronaTest = mockk {
every { recycledAt } returns recycleTime
every { identifier } returns recycleTime.toString()
}

private fun createFamilyTest(recycleTime: Instant): FamilyCoronaTest = mockk {
every { recycledAt } returns recycleTime
every { identifier } returns recycleTime.toString()
}

@Test
fun `No recycled items, nothing to delete`() = runBlockingTest {
every { recycledCertificatesProvider.recycledCertificates } returns flowOf(emptySet())
Expand All @@ -78,13 +90,20 @@ class RecycleBinCleanUpServiceTest : BaseTest() {
val certWith30DaysOfRetention = createCert(30)
val testWith0DaysOfRetention = createTest(0)
val testWith30DaysOfRetention = createTest(30)
val familyTestWith0DaysOfRetention = familyTest(0)
val familyTestWith30DaysOfRetention = familyTest(30)

every { recycledCertificatesProvider.recycledCertificates } returns flowOf(
setOf(certWith0DaysOfRetention, certWith30DaysOfRetention)
)

every { recycledCoronaTestsProvider.tests } returns flowOf(
setOf(testWith0DaysOfRetention, testWith30DaysOfRetention)
setOf(
testWith0DaysOfRetention,
testWith30DaysOfRetention,
familyTestWith0DaysOfRetention,
familyTestWith30DaysOfRetention
)
)

createInstance().clearRecycledItems()
Expand All @@ -105,18 +124,21 @@ class RecycleBinCleanUpServiceTest : BaseTest() {
val testExact30Days = createTest(nowMinus30Days)
val test30DaysAnd1Ms = createTest(nowMinus30DaysAnd1Ms)

val familyTestExact30Days = createFamilyTest(nowMinus30Days)
val familyTest30DaysAnd1Ms = createFamilyTest(nowMinus30DaysAnd1Ms)

every { recycledCertificatesProvider.recycledCertificates } returns flowOf(
setOf(certExact30Days, cert30DaysAnd1Ms)
)

every { recycledCoronaTestsProvider.tests } returns flowOf(
setOf(testExact30Days, test30DaysAnd1Ms)
setOf(testExact30Days, test30DaysAnd1Ms, familyTest30DaysAnd1Ms, familyTestExact30Days)
)

createInstance().clearRecycledItems()

val containerIds = listOf(cert30DaysAnd1Ms.containerId)
val identifiers = listOf(test30DaysAnd1Ms.identifier)
val identifiers = listOf(test30DaysAnd1Ms.identifier, familyTest30DaysAnd1Ms.identifier)
coVerify(exactly = 1) {
recycledCertificatesProvider.deleteAllCertificate(containerIds)
recycledCoronaTestsProvider.deleteAllCoronaTest(identifiers)
Expand Down