Skip to content
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
@@ -0,0 +1,125 @@
package hu.toliver.vinotes.data.dao

import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import hu.toliver.vinotes.data.WineDatabase
import hu.toliver.vinotes.data.local.entity.TasteEntity
import hu.toliver.vinotes.data.local.entity.WineEntity
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.runBlocking
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class CatalogImportPreservesDataTest {

private lateinit var database: WineDatabase
private lateinit var wineDao: WineDao
private lateinit var tasteDao: TasteDao

@Before
fun setUp() {
val context = ApplicationProvider.getApplicationContext<Context>()
database = Room.inMemoryDatabaseBuilder(context, WineDatabase::class.java)
.allowMainThreadQueries()
.build()
wineDao = database.wineDao()
tasteDao = database.tasteDao()
}

@After
fun tearDown() {
database.close()
}

@Test
fun catalogReimportPreservesTastingsAndUserEdits() = runBlocking {
val wineId = "catalog-wine-1"
val original = sampleCatalogWine(id = wineId, producer = "Original Producer", year = 2020)
wineDao.insertAll(listOf(original))

val tasting = sampleTasting(wineId = wineId, id = "taste-1")
tasteDao.insert(tasting)

val userEdited = original.copy(producer = "User Producer", year = 2019)
wineDao.update(userEdited)

val reimportFromCatalog = sampleCatalogWine(
id = wineId,
producer = "Catalog Producer",
year = 2022,
isFromCatalog = true
)
wineDao.insertAll(listOf(reimportFromCatalog))

val storedWine = wineDao.getById(wineId)
assertNotNull(storedWine)
assertEquals("User Producer", storedWine!!.producer)
assertEquals(2019, storedWine.year)

val tastings = tasteDao.getByWineId(wineId).first()
assertEquals(1, tastings.size)
assertEquals("taste-1", tastings.first().id)
}

private fun sampleCatalogWine(
id: String,
producer: String,
year: Int,
isFromCatalog: Boolean = true
) = WineEntity(
id = id,
name = "Test Wine",
producer = producer,
year = year,
grape = "Test Grape",
isCuvee = false,
cuveeComponents = "",
country = "HU",
region = "Test Region",
colour = "WHITE",
sweetness = "DRY",
sugar = 0f,
alcoholPercentage = 12.0,
volume = 750,
description = "",
imageUrl = "",
isFromCatalog = isFromCatalog
)

private fun sampleTasting(wineId: String, id: String) = TasteEntity(
id = id,
wineId = wineId,
clarity = "CLEAR",
colourIntensity = "MEDIUM",
colour = "WHITE",
otherVisual = "",
noseIntensity = "MEDIUM",
noseAroma = "",
noseDevelopment = "DEVELOPED",
otherNose = "",
sweetness = "DRY",
acidity = "MEDIUM",
tannin = "LOW",
body = "MEDIUM",
alcohol = "MEDIUM",
flavourIntensity = "MEDIUM",
flavourCharacteristics = "",
finish = "MEDIUM",
otherPalate = "",
rating = 4,
overallImpression = "",
bestWith = "",
wouldDrinkAgain = true,
date = 1_700_000_000_000L,
place = "Home",
latitude = null,
longitude = null
)
}
5 changes: 4 additions & 1 deletion app/src/main/java/hu/toliver/vinotes/data/dao/WineDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ interface WineDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertOrReplace(wine: WineEntity)

@Insert(onConflict = OnConflictStrategy.REPLACE)
@Insert(onConflict = OnConflictStrategy.IGNORE)
suspend fun insertAll(wines: List<WineEntity>)

@Update
suspend fun update(wine: WineEntity)

@Update
suspend fun updateAll(wines: List<WineEntity>)

@Query("DELETE FROM wines WHERE id = :id")
suspend fun deleteById(id: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ class WineRepositoryImpl @Inject constructor(
dao.insertAll(wines.map { it.toEntity(isFromCatalog = fromCatalog) })
}

override suspend fun updateAll(wines: List<Wine>, fromCatalog: Boolean): Result<Unit> = runCatching {
dao.updateAll(wines.map { it.toEntity(isFromCatalog = fromCatalog) })
}

override suspend fun update(wine: Wine): Result<Unit> = runCatching {
dao.update(wine.toEntity())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface WineRepository {
): Flow<List<Wine>>
suspend fun insert(wine: Wine): Result<Unit>
suspend fun insertAll(wines: List<Wine>, fromCatalog: Boolean): Result<Unit>
suspend fun updateAll(wines: List<Wine>, fromCatalog: Boolean): Result<Unit>
suspend fun insertIfNotExists(wine: Wine): Result<Unit>
suspend fun update(wine: Wine): Result<Unit>
suspend fun delete(id: String): Result<Unit>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class PerformDeltaSyncUseCase @Inject constructor(
}

if (delta.updated.isNotEmpty()) {
wineRepository.insertAll(delta.updated, fromCatalog = true).getOrThrow()
wineRepository.updateAll(delta.updated, fromCatalog = true).getOrThrow()
}

if (delta.removedIds.isNotEmpty()) {
Expand Down
Loading