Commit 6b4b2a91 authored by Joey's avatar Joey

core: db: add Hba1c database table and repository

Issue: #7Signed-off-by: Joey's avatarJoey <bevilacquajoey@gmail.com>
parent 6cd3959c
{
"formatVersion": 1,
"database": {
"version": 5,
"identityHash": "b58b7f3ccdae27614ff45351147eb3ac",
"entities": [
{
"tableName": "glucose",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `value` INTEGER NOT NULL, `date` INTEGER NOT NULL, `insulinId0` INTEGER NOT NULL, `insulinValue0` REAL NOT NULL, `insulinId1` INTEGER NOT NULL, `insulinValue1` REAL NOT NULL, `eatLevel` INTEGER NOT NULL, `timeFrame` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "uid",
"columnName": "uid",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "value",
"columnName": "value",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "date",
"columnName": "date",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "insulinId0",
"columnName": "insulinId0",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "insulinValue0",
"columnName": "insulinValue0",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "insulinId1",
"columnName": "insulinId1",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "insulinValue1",
"columnName": "insulinValue1",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "eatLevel",
"columnName": "eatLevel",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "timeFrame",
"columnName": "timeFrame",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"uid"
],
"autoGenerate": true
},
"indices": [
{
"name": "index_glucose_date_uid",
"unique": true,
"columnNames": [
"date",
"uid"
],
"createSql": "CREATE UNIQUE INDEX `index_glucose_date_uid` ON `${TABLE_NAME}` (`date`, `uid`)"
}
],
"foreignKeys": []
},
{
"tableName": "hba1c",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `value` REAL NOT NULL, `date` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "uid",
"columnName": "uid",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "value",
"columnName": "value",
"affinity": "REAL",
"notNull": true
},
{
"fieldPath": "date",
"columnName": "date",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"uid"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "insulin",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `name` TEXT NOT NULL, `timeFrame` INTEGER NOT NULL, `isBasal` INTEGER NOT NULL, `hasHalfUnits` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "uid",
"columnName": "uid",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "name",
"columnName": "name",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "timeFrame",
"columnName": "timeFrame",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "isBasal",
"columnName": "isBasal",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "hasHalfUnits",
"columnName": "hasHalfUnits",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"columnNames": [
"uid"
],
"autoGenerate": true
},
"indices": [],
"foreignKeys": []
}
],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, \"b58b7f3ccdae27614ff45351147eb3ac\")"
]
}
}
\ No newline at end of file
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.core.data.repositories
import androidx.test.platform.app.InstrumentationRegistry
import com.google.common.truth.Truth.assertThat
import it.diab.core.data.AppDatabase
import it.diab.core.util.extensions.hba1c
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
class Hba1cRepositoryTest {
private lateinit var repository: Hba1cRepository
@Before
fun setup() {
AppDatabase.TEST_MODE = true
val context = InstrumentationRegistry.getInstrumentation().targetContext
repository = Hba1cRepository.getInstance(context)
}
@Test
fun insert() = runBlocking {
val item = hba1c {
uid = 12
value = 12.3f
}
repository.insert(item)
val inRepo = repository.getById(item.uid)
assertThat(inRepo).isEqualTo(item)
assertThat(inRepo?.uid ?: -1).isEqualTo(item.uid)
}
@Test
fun delete() = runBlocking {
val item = hba1c {
uid = 12
value = 12.3f
}
repository.insert(item)
assertThat(repository.getById(item.uid)).isNotNull()
repository.delete(item)
assertThat(repository.getById(item.uid)).isNotEqualTo(item.uid)
}
private fun Hba1cRepository.getById(uid: Long) = getAllItems().firstOrNull { it.uid == uid }
}
\ No newline at end of file
......@@ -17,18 +17,25 @@ import androidx.room.RoomDatabase
import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
import it.diab.core.data.dao.GlucoseDao
import it.diab.core.data.dao.Hba1cDao
import it.diab.core.data.dao.InsulinDao
import it.diab.core.data.entities.Glucose
import it.diab.core.data.entities.Hba1c
import it.diab.core.data.entities.Insulin
import it.diab.core.util.SingletonHolder
import it.diab.core.util.extensions.asTimeFrame
import java.util.Date
@Database(entities = [(Glucose::class), (Insulin::class)], version = 4)
@Database(entities = [
Glucose::class,
Hba1c::class,
Insulin::class
], version = 5)
abstract class AppDatabase protected constructor() : RoomDatabase() {
abstract fun glucose(): GlucoseDao
abstract fun insulin(): InsulinDao
abstract fun hba1c(): Hba1cDao
companion object : SingletonHolder<AppDatabase, Context>({
if (AppDatabase.TEST_MODE)
......@@ -40,7 +47,8 @@ abstract class AppDatabase protected constructor() : RoomDatabase() {
.addMigrations(
AppDatabase.MIGRATION_1_2,
AppDatabase.MIGRATION_2_3,
AppDatabase.MIGRATION_3_4
AppDatabase.MIGRATION_3_4,
AppDatabase.MIGRATION_4_5
)
.build()
}) {
......@@ -127,5 +135,18 @@ abstract class AppDatabase protected constructor() : RoomDatabase() {
cursor.close()
}
}
/*
* DB version 5
*
* Hba1c
* Add new table
*/
private val MIGRATION_4_5 = object : Migration(4, 5) {
override fun migrate(database: SupportSQLiteDatabase) {
/* Hba1c */
database.execSQL("CREATE TABLE IF NOT EXISTS `hba1c` (`uid` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `value` REAL NOT NULL, `date` INTEGER NOT NULL)")
}
}
}
}
\ No newline at end of file
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.core.data.dao
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.TypeConverters
import it.diab.core.data.converters.DateConverter
import it.diab.core.data.entities.Hba1c
@Dao
@TypeConverters(DateConverter::class)
interface Hba1cDao {
@get:Query("SELECT * FROM hba1c ORDER BY date DESC")
val all: LiveData<List<Hba1c>>
@Query("SELECT * FROM hba1c ORDER BY date DESC")
fun getAllItems(): List<Hba1c>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg items: Hba1c)
@Delete
fun delete(item: Hba1c)
}
\ No newline at end of file
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.core.data.entities
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import it.diab.core.data.converters.DateConverter
import java.util.Date
import kotlin.math.roundToInt
@Entity(tableName = "hba1c")
class Hba1c {
@PrimaryKey(autoGenerate = true)
var uid: Long = 0
@ColumnInfo(name = "value")
var value: Float = 0f
@ColumnInfo
@TypeConverters(DateConverter::class)
var date: Date = Date()
@Ignore
constructor()
constructor(uid: Long, value: Float, date: Date) {
this.uid = uid
this.value = value
this.date = date
}
override fun toString() = "$uid: $value, $date"
override fun equals(other: Any?) = other != null &&
other is Hba1c &&
other.value == value &&
other.date == date
override fun hashCode() = (value * 1000).roundToInt() or date.hashCode()
}
\ No newline at end of file
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.core.data.repositories
import android.content.Context
import androidx.annotation.WorkerThread
import it.diab.core.data.AppDatabase
import it.diab.core.data.dao.Hba1cDao
import it.diab.core.data.entities.Hba1c
import it.diab.core.util.SingletonHolder
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.withContext
class Hba1cRepository private constructor(private val hba1cDao: Hba1cDao) {
val all = hba1cDao.all
@WorkerThread
fun getAllItems() = hba1cDao.getAllItems()
suspend fun insert(item: Hba1c) = withContext(IO) { hba1cDao.insert(item) }
suspend fun delete(item: Hba1c) = withContext(IO) { hba1cDao.delete(item) }
companion object : SingletonHolder<Hba1cRepository, Context>({
Hba1cRepository(AppDatabase.getInstance(it).hba1c())
})
}
\ No newline at end of file
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.core.util.extensions
import it.diab.core.data.entities.Hba1c
fun hba1c(block: Hba1c.() -> Unit) = Hba1c().apply(block)
\ No newline at end of file
/*
* Copyright (c) 2019 Bevilacqua Joey
*
* Licensed under the GNU GPLv3 license
*
* The text of the license can be found in the LICENSE file
* or at https://www.gnu.org/licenses/gpl.txt
*/
package it.diab.core.data.entities
import it.diab.core.util.extensions.hba1c
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.util.Date
@RunWith(JUnit4::class)
class Hba1cTest {
@Test
fun defaults() {
assertEquals(0f, hba1c { uid = 12 }.value)
}
@Test
fun equals() {
val a = hba1c {
uid = 1
value = 10f
date = Date(0)
}
val b = hba1c {
uid = 2
value = 10f
date = Date(0)
}
val c = hba1c {
uid = 1
value = 11f
date = Date(0)
}
assertTrue(a == b)
assertTrue(a != c)
assertTrue(b != c)
assertTrue(a.hashCode() == b.hashCode())
assertTrue(a.hashCode() != c.hashCode())
assertTrue(a.hashCode() != c.hashCode())
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment