mirror of https://github.com/iSoron/uhabits.git
parent
27023e50ae
commit
c904e22c0f
@ -0,0 +1,86 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.core.models.sqlite
|
||||||
|
|
||||||
|
import org.isoron.uhabits.core.database.*
|
||||||
|
import org.isoron.uhabits.core.models.*
|
||||||
|
import org.isoron.uhabits.core.models.sqlite.records.*
|
||||||
|
import org.isoron.uhabits.core.utils.*
|
||||||
|
|
||||||
|
class SQLiteEntries(database: Database) : Entries() {
|
||||||
|
val repository = Repository(EntryRecord::class.java, database)
|
||||||
|
var habitId: Long? = null
|
||||||
|
var isLoaded = false
|
||||||
|
|
||||||
|
private fun loadRecords() {
|
||||||
|
if (isLoaded) return
|
||||||
|
val habitId = habitId ?: throw IllegalStateException("habitId must be set")
|
||||||
|
val records = repository.findAll("where habit = ? order by timestamp",
|
||||||
|
habitId.toString())
|
||||||
|
for (rec in records) super.add(rec.toEntry())
|
||||||
|
isLoaded = true
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun get(timestamp: Timestamp): Entry {
|
||||||
|
loadRecords()
|
||||||
|
return super.get(timestamp)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getByInterval(from: Timestamp, to: Timestamp): List<Entry> {
|
||||||
|
loadRecords()
|
||||||
|
return super.getByInterval(from, to)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun add(entry: Entry) {
|
||||||
|
loadRecords()
|
||||||
|
val habitId = habitId ?: throw IllegalStateException("habitId must be set")
|
||||||
|
|
||||||
|
// Remove existing rows
|
||||||
|
repository.execSQL("delete from repetitions where habit = ? and timestamp = ?",
|
||||||
|
habitId.toString(),
|
||||||
|
entry.timestamp.unixTime.toString())
|
||||||
|
|
||||||
|
// Add new row
|
||||||
|
val record = EntryRecord().apply { copyFrom(entry) }
|
||||||
|
record.habitId = habitId
|
||||||
|
repository.save(record)
|
||||||
|
|
||||||
|
// Add to memory list
|
||||||
|
super.add(entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun getKnown(): List<Entry> {
|
||||||
|
loadRecords()
|
||||||
|
return super.getKnown()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun groupBy(field: DateUtils.TruncateField, firstWeekday: Int, isNumerical: Boolean): List<Entry> {
|
||||||
|
loadRecords()
|
||||||
|
return super.groupBy(field, firstWeekday, isNumerical)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun computeFrom(other: Entries, frequency: Frequency, isNumerical: Boolean) {
|
||||||
|
throw UnsupportedOperationException()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun clear() {
|
||||||
|
throw UnsupportedOperationException()
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,105 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2016-2020 Álinson Santos Xavier <isoron@gmail.com>
|
||||||
|
*
|
||||||
|
* This file is part of Loop Habit Tracker.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by the
|
||||||
|
* Free Software Foundation, either version 3 of the License, or (at your
|
||||||
|
* option) any later version.
|
||||||
|
*
|
||||||
|
* Loop Habit Tracker is distributed in the hope that it will be useful, but
|
||||||
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||||
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||||
|
* more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License along
|
||||||
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package org.isoron.uhabits.core.models.sqlite
|
||||||
|
|
||||||
|
import junit.framework.Assert.*
|
||||||
|
import org.isoron.uhabits.core.BaseUnitTest.*
|
||||||
|
import org.isoron.uhabits.core.database.*
|
||||||
|
import org.isoron.uhabits.core.models.*
|
||||||
|
import org.isoron.uhabits.core.models.Entry.Companion.UNKNOWN
|
||||||
|
import org.isoron.uhabits.core.models.sqlite.records.*
|
||||||
|
import org.isoron.uhabits.core.utils.*
|
||||||
|
import org.junit.*
|
||||||
|
|
||||||
|
class SQLiteEntriesTest {
|
||||||
|
|
||||||
|
private val database = buildMemoryDatabase()
|
||||||
|
private val repository = Repository(EntryRecord::class.java, database)
|
||||||
|
private val entries = SQLiteEntries(database)
|
||||||
|
private val today = DateUtils.getToday()
|
||||||
|
|
||||||
|
@Before
|
||||||
|
fun setUp() {
|
||||||
|
// Create a habit and add it to the database to satisfy foreign key requirements
|
||||||
|
val factory = SQLModelFactory(database)
|
||||||
|
val habitList = factory.buildHabitList()
|
||||||
|
val habit = factory.buildHabit()
|
||||||
|
habitList.add(habit)
|
||||||
|
entries.habitId = habit.id
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testLoad() {
|
||||||
|
val today = DateUtils.getToday()
|
||||||
|
repository.save(EntryRecord().apply {
|
||||||
|
habitId = entries.habitId
|
||||||
|
timestamp = today.unixTime
|
||||||
|
value = 500
|
||||||
|
})
|
||||||
|
repository.save(EntryRecord().apply {
|
||||||
|
habitId = entries.habitId
|
||||||
|
timestamp = today.minus(5).unixTime
|
||||||
|
value = 300
|
||||||
|
})
|
||||||
|
assertEquals(
|
||||||
|
Entry(timestamp = today, value = 500),
|
||||||
|
entries.get(today),
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
Entry(timestamp = today.minus(1), value = UNKNOWN),
|
||||||
|
entries.get(today.minus(1)),
|
||||||
|
)
|
||||||
|
assertEquals(
|
||||||
|
Entry(timestamp = today.minus(5), value = 300),
|
||||||
|
entries.get(today.minus(5)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testAdd() {
|
||||||
|
assertNull(getByTimestamp(1, today))
|
||||||
|
|
||||||
|
val original = Entry(today, 150)
|
||||||
|
entries.add(original)
|
||||||
|
|
||||||
|
val retrieved = getByTimestamp(1, today)
|
||||||
|
assertNotNull(retrieved)
|
||||||
|
assertEquals(original, retrieved!!.toEntry())
|
||||||
|
|
||||||
|
val replacement = Entry(today, 90)
|
||||||
|
entries.add(replacement)
|
||||||
|
|
||||||
|
val retrieved2 = getByTimestamp(1, today)
|
||||||
|
assertNotNull(retrieved2)
|
||||||
|
assertEquals(replacement, retrieved2!!.toEntry())
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getByTimestamp(
|
||||||
|
habitId: Int,
|
||||||
|
timestamp: Timestamp,
|
||||||
|
): EntryRecord? {
|
||||||
|
return repository.findFirst(
|
||||||
|
"where habit = ? and timestamp = ?",
|
||||||
|
habitId.toString(),
|
||||||
|
timestamp.unixTime.toString(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue