Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Unverified Commit 73bf8466 authored by Wolf Montwé's avatar Wolf Montwé
Browse files

Change ContactRepository to allow nullable values to be cached

parent 9cdf3e2b
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ import org.koin.core.qualifier.named
import org.koin.dsl.module

internal val contactModule = module {
    single<Cache<EmailAddress, Contact>>(named(CACHE_NAME)) {
    single<Cache<EmailAddress, Contact?>>(named(CACHE_NAME)) {
        SynchronizedCache(
            delegateCache = ExpiringCache(clock = get()),
        )
+7 −7
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@ interface CachingRepository {
}

internal class CachingContactRepository(
    private val cache: Cache<EmailAddress, Contact>,
    private val cache: Cache<EmailAddress, Contact?>,
    private val dataSource: ContactDataSource,
) : ContactRepository, CachingRepository {

@@ -26,16 +26,16 @@ internal class CachingContactRepository(
            return cache[emailAddress]
        }

        val contact = dataSource.getContactFor(emailAddress)

        if (contact != null) {
            cache[emailAddress] = contact
        return dataSource.getContactFor(emailAddress).also {
            cache[emailAddress] = it
        }

        return contact
    }

    override fun hasContactFor(emailAddress: EmailAddress): Boolean {
        if (cache.hasKey(emailAddress)) {
            return cache[emailAddress] != null
        }

        return dataSource.hasContactFor(emailAddress)
    }

+1 −1
Original line number Diff line number Diff line
@@ -9,7 +9,7 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

@RunWith(RobolectricTestRunner::class)
internal class CoreCommonAndroidModuleTest {
internal class CoreCommonAndroidModuleKtTest {

    @Test
    fun `should have a valid di module`() {
+25 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ import org.robolectric.RobolectricTestRunner
internal class CachingContactRepositoryTest {

    private val dataSource = mock<ContactDataSource>()
    private val cache = InMemoryCache<EmailAddress, Contact>()
    private val cache = InMemoryCache<EmailAddress, Contact?>()

    private val testSubject = CachingContactRepository(cache = cache, dataSource = dataSource)

@@ -60,6 +60,21 @@ internal class CachingContactRepositoryTest {
        assertThat(result1).isEqualTo(result2)
    }

    @Test
    fun `getContactFor() caches null`() {
        dataSource.stub {
            on { getContactFor(CONTACT_EMAIL_ADDRESS) } doReturnConsecutively listOf(
                null,
                CONTACT,
            )
        }

        val result1 = testSubject.getContactFor(CONTACT_EMAIL_ADDRESS)
        val result2 = testSubject.getContactFor(CONTACT_EMAIL_ADDRESS)

        assertThat(result1).isEqualTo(result2)
    }

    @Test
    fun `getContactFor() returns cached contact`() {
        cache[CONTACT_EMAIL_ADDRESS] = CONTACT
@@ -76,6 +91,15 @@ internal class CachingContactRepositoryTest {
        assertThat(result).isFalse()
    }

    @Test
    fun `hasContactFor() returns false if cached contact is null`() {
        cache[CONTACT_EMAIL_ADDRESS] = null

        val result = testSubject.hasContactFor(CONTACT_EMAIL_ADDRESS)

        assertThat(result).isFalse()
    }

    @Test
    fun `hasContactFor() returns true if contact exists`() {
        dataSource.stub { on { hasContactFor(CONTACT_EMAIL_ADDRESS) } doReturn true }
+1 −1
Original line number Diff line number Diff line
@@ -10,7 +10,7 @@ import org.robolectric.RobolectricTestRunner
import org.robolectric.RuntimeEnvironment

@RunWith(RobolectricTestRunner::class)
internal class ContactKoinModuleTest {
internal class ContactKoinModuleKtTest {
    @Test
    fun `should have a valid di module`() {
        koinApplication {
Loading