|
| 1 | +/* |
| 2 | + * openScale |
| 3 | + * Copyright (C) 2025 olie.xdev <olie.xdeveloper@googlemail.com> |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * This program is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | +package com.health.openscale.core.usecase |
| 19 | + |
| 20 | +import android.app.Application |
| 21 | +import androidx.test.core.app.ApplicationProvider |
| 22 | +import com.google.common.truth.Truth.assertThat |
| 23 | +import com.health.openscale.core.data.ActivityLevel |
| 24 | +import com.health.openscale.core.data.GenderType |
| 25 | +import com.health.openscale.core.data.Measurement |
| 26 | +import com.health.openscale.core.data.MeasurementTypeKey |
| 27 | +import com.health.openscale.core.data.MeasurementValue |
| 28 | +import com.health.openscale.core.data.User |
| 29 | +import com.health.openscale.core.database.AppDatabase |
| 30 | +import com.health.openscale.core.database.DatabaseRepository |
| 31 | +import com.health.openscale.getDefaultMeasurementTypes |
| 32 | +import com.health.openscale.testutil.RoomTestSupport |
| 33 | +import kotlinx.coroutines.CoroutineScope |
| 34 | +import kotlinx.coroutines.Dispatchers |
| 35 | +import kotlinx.coroutines.SupervisorJob |
| 36 | +import kotlinx.coroutines.flow.first |
| 37 | +import kotlinx.coroutines.runBlocking |
| 38 | +import org.junit.After |
| 39 | +import org.junit.Before |
| 40 | +import org.junit.Test |
| 41 | +import org.junit.runner.RunWith |
| 42 | +import org.robolectric.RobolectricTestRunner |
| 43 | +import org.robolectric.annotation.Config |
| 44 | +import java.io.File |
| 45 | + |
| 46 | +/** |
| 47 | + * Tests [MeasurementCrudUseCases.saveMeasurement] — insert and the update value-diff |
| 48 | + * (delete removed / update existing / insert new) — against in-memory Room (Robolectric). |
| 49 | + * Assertions target specific measurement types since recalculation adds derived values. |
| 50 | + */ |
| 51 | +@RunWith(RobolectricTestRunner::class) |
| 52 | +@Config(sdk = [34]) |
| 53 | +class MeasurementCrudUseCasesTest { |
| 54 | + |
| 55 | + private val app: Application get() = ApplicationProvider.getApplicationContext() |
| 56 | + private lateinit var db: AppDatabase |
| 57 | + private lateinit var repo: DatabaseRepository |
| 58 | + private lateinit var crud: MeasurementCrudUseCases |
| 59 | + private var userId = 0 |
| 60 | + private var weightId = 0 |
| 61 | + private var waistId = 0 |
| 62 | + private var neckId = 0 |
| 63 | + |
| 64 | + @Before |
| 65 | + fun setUp() = runBlocking { |
| 66 | + db = RoomTestSupport.inMemory(app) |
| 67 | + repo = RoomTestSupport.repositoryFor(db) |
| 68 | + repo.insertAllMeasurementTypes(getDefaultMeasurementTypes()) |
| 69 | + val settings = RoomTestSupport.settingsFacadeFor( |
| 70 | + CoroutineScope(SupervisorJob() + Dispatchers.IO), |
| 71 | + File(app.cacheDir, "crud-${System.nanoTime()}.preferences_pb"), |
| 72 | + ) |
| 73 | + crud = RoomTestSupport.measurementCrudFor(app, repo, settings) |
| 74 | + |
| 75 | + val types = repo.getAllMeasurementTypes().first() |
| 76 | + weightId = types.first { it.key == MeasurementTypeKey.WEIGHT }.id |
| 77 | + waistId = types.first { it.key == MeasurementTypeKey.WAIST }.id |
| 78 | + neckId = types.first { it.key == MeasurementTypeKey.NECK }.id |
| 79 | + |
| 80 | + userId = repo.insertUser( |
| 81 | + User( |
| 82 | + name = "u", birthDate = 0L, gender = GenderType.MALE, heightCm = 175f, |
| 83 | + activityLevel = ActivityLevel.MODERATE, useAssistedWeighing = false, |
| 84 | + ) |
| 85 | + ).toInt() |
| 86 | + } |
| 87 | + |
| 88 | + @After |
| 89 | + fun tearDown() = db.close() |
| 90 | + |
| 91 | + @Test |
| 92 | + fun saveMeasurement_insert_persistsValues() = runBlocking { |
| 93 | + val id = crud.saveMeasurement( |
| 94 | + Measurement(userId = userId, timestamp = 1_000L), |
| 95 | + listOf(MeasurementValue(measurementId = 0, typeId = weightId, floatValue = 70f)), |
| 96 | + ).getOrThrow() |
| 97 | + |
| 98 | + assertThat(id).isGreaterThan(0) |
| 99 | + val values = repo.getValuesForMeasurement(id).first() |
| 100 | + assertThat(values.any { it.typeId == weightId && it.floatValue == 70f }).isTrue() |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + fun saveMeasurement_update_appliesValueDiff() = runBlocking { |
| 105 | + // initial: WEIGHT + WAIST |
| 106 | + val id = crud.saveMeasurement( |
| 107 | + Measurement(userId = userId, timestamp = 2_000L), |
| 108 | + listOf( |
| 109 | + MeasurementValue(measurementId = 0, typeId = weightId, floatValue = 70f), |
| 110 | + MeasurementValue(measurementId = 0, typeId = waistId, floatValue = 90f), |
| 111 | + ), |
| 112 | + ).getOrThrow() |
| 113 | + val weightValueId = repo.getValuesForMeasurement(id).first().first { it.typeId == weightId }.id |
| 114 | + |
| 115 | + // update: keep+change WEIGHT, drop WAIST, add NECK |
| 116 | + crud.saveMeasurement( |
| 117 | + Measurement(id = id, userId = userId, timestamp = 2_000L), |
| 118 | + listOf( |
| 119 | + MeasurementValue(id = weightValueId, measurementId = id, typeId = weightId, floatValue = 72f), |
| 120 | + MeasurementValue(measurementId = 0, typeId = neckId, floatValue = 38f), |
| 121 | + ), |
| 122 | + ).getOrThrow() |
| 123 | + |
| 124 | + val after = repo.getValuesForMeasurement(id).first() |
| 125 | + assertThat(after.none { it.typeId == waistId }).isTrue() // deleted |
| 126 | + assertThat(after.first { it.typeId == weightId }.floatValue).isWithin(1e-3f).of(72f) // updated |
| 127 | + assertThat(after.any { it.typeId == neckId && it.floatValue == 38f }).isTrue() // inserted |
| 128 | + } |
| 129 | +} |
0 commit comments