SyncSchemaDaoTest.kt 92 lignes · 3040 octets
package fr.ebii.card2vcf.sync

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import fr.ebii.card2vcf.data.CrmDatabase
import fr.ebii.card2vcf.data.TacheEntity
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.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 SyncSchemaDaoTest {
    private lateinit var db: CrmDatabase
    private lateinit var syncOpDao: SyncOpDao
    private lateinit var tacheDao: fr.ebii.card2vcf.data.TacheDao

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

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

    @Test
    fun syncOpInsertAndList() = runBlocking {
        val id = syncOpDao.insert(
            SyncOpEntity(
                entityType = "contact",
                op = "create",
                payloadJson = """{"fullName":"Ada"}""",
                localId = 42L,
                createdAt = 1000L,
            ),
        )
        val ops = syncOpDao.listAll()
        assertEquals(1, ops.size)
        assertEquals(id, ops[0].id)
        assertEquals("contact", ops[0].entityType)
        assertEquals("create", ops[0].op)
        assertEquals(42L, ops[0].localId)
        assertNotNull(syncOpDao.getById(id))
        syncOpDao.deleteById(id)
        assertNull(syncOpDao.getById(id))
        assertEquals(0, syncOpDao.listAll().size)
    }

    @Test
    fun tacheUpsertAndGetByServerId() = runBlocking {
        val localId = tacheDao.upsert(
            TacheEntity(
                serverId = "tache-1",
                projetServerId = "projet-1",
                titre = "Préparer démo",
                colonneId = "col-a",
                ordre = 2,
                assigneA = "alice",
                auteur = "bob",
                createdAt = 500L,
                updatedAt = 600L,
            ),
        )
        val found = tacheDao.getByServerId("tache-1")
        assertNotNull(found)
        assertEquals(localId, found!!.localId)
        assertEquals("Préparer démo", found.titre)
        assertEquals("projet-1", found.projetServerId)
        assertEquals(2, found.ordre)
        assertEquals("alice", found.assigneA)

        tacheDao.upsert(found.copy(titre = "Démo prête", updatedAt = 700L))
        assertEquals("Démo prête", tacheDao.getByServerId("tache-1")!!.titre)

        assertEquals(1, tacheDao.listByProjetServerId("projet-1").size)
        tacheDao.deleteByServerId("tache-1")
        assertNull(tacheDao.getByServerId("tache-1"))
    }
}