AgendaSchemaDaoTest.kt 123 lignes · 4164 octets
package fr.ebii.card2vcf.data

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [31])
class AgendaSchemaDaoTest {
    private lateinit var db: CrmDatabase
    private lateinit var rdvDao: RdvDao
    private lateinit var reservationDao: ReservationDao
    private lateinit var indisponibiliteDao: IndisponibiliteDao

    @Before
    fun setUp() {
        val ctx = ApplicationProvider.getApplicationContext<Context>()
        db = Room.inMemoryDatabaseBuilder(ctx, CrmDatabase::class.java)
            .allowMainThreadQueries()
            .build()
        rdvDao = db.rdvDao()
        reservationDao = db.reservationDao()
        indisponibiliteDao = db.indisponibiliteDao()
    }

    @After
    fun tearDown() = db.close()

    @Test
    fun rdvUpsertGetListDirtyAndDelete() = runBlocking {
        val localId = rdvDao.upsert(
            RdvEntity(
                serverId = "rdv-1",
                titre = "Point client",
                lieu = "Bureau",
                debutMs = 1_000L,
                finMs = 2_000L,
                updatedAt = 1_000L,
                dirtyLocal = true,
            ),
        )

        val found = rdvDao.getByServerId("rdv-1")
        assertNotNull(found)
        assertEquals(localId, found!!.localId)
        assertEquals("Point client", found.titre)
        assertTrue(found.dirtyLocal)

        assertEquals(1, rdvDao.listDirty().size)

        rdvDao.upsert(found.copy(titre = "Point client reporté", updatedAt = 2_000L, dirtyLocal = false))
        assertEquals("Point client reporté", rdvDao.getByServerId("rdv-1")!!.titre)
        assertEquals(0, rdvDao.listDirty().size)

        rdvDao.deleteByServerId("rdv-1")
        assertNull(rdvDao.getByServerId("rdv-1"))
    }

    @Test
    fun reservationUpsertListByCibleAndConflictPending() = runBlocking {
        val localId = reservationDao.upsert(
            ReservationEntity(
                serverId = "resa-1",
                cibleType = "salle",
                cibleId = "salle-a",
                debutMs = 1_000L,
                finMs = 2_000L,
                motif = "Réunion",
                updatedAt = 1_000L,
                dirtyLocal = true,
            ),
        )

        val found = reservationDao.getByServerId("resa-1")
        assertNotNull(found)
        assertEquals(localId, found!!.localId)
        assertEquals("active", found.statut)
        assertEquals(1, reservationDao.listByCible("salle", "salle-a").size)
        assertEquals(1, reservationDao.listDirty().size)

        reservationDao.upsert(found.copy(conflictPending = true, dirtyLocal = false))
        val updated = reservationDao.getByServerId("resa-1")
        assertTrue(updated!!.conflictPending)
        assertEquals(0, reservationDao.listDirty().size)

        reservationDao.deleteByServerId("resa-1")
        assertNull(reservationDao.getByServerId("resa-1"))
    }

    @Test
    fun indisponibiliteUpsertListByCibleAndDelete() = runBlocking {
        indisponibiliteDao.upsert(
            IndisponibiliteEntity(
                serverId = "indispo-1",
                cibleType = "vehicule",
                cibleId = "vehicule-a",
                debutMs = 1_000L,
                finMs = 2_000L,
                nature = "maintenance",
                updatedAt = 1_000L,
            ),
        )

        val found = indisponibiliteDao.getByServerId("indispo-1")
        assertNotNull(found)
        assertEquals("maintenance", found!!.nature)
        assertEquals(1, indisponibiliteDao.listByCible("vehicule", "vehicule-a").size)

        indisponibiliteDao.deleteByServerId("indispo-1")
        assertNull(indisponibiliteDao.getByServerId("indispo-1"))
    }
}