Unit tests for habit group

pull/2020/head
Dharanish 1 year ago
parent 7a2d486fed
commit 8043cb2e1d

@ -0,0 +1,121 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* 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.test
import org.isoron.uhabits.core.models.HabitGroup
import org.isoron.uhabits.core.models.HabitGroupList
import org.isoron.uhabits.core.models.HabitList
import org.isoron.uhabits.core.models.ModelFactory
import org.isoron.uhabits.core.models.NumericalHabitType
import org.isoron.uhabits.core.models.PaletteColor
import org.isoron.uhabits.core.models.sqlite.SQLiteHabitList
class HabitGroupFixtures(private val modelFactory: ModelFactory, private val habitList: HabitList, private val habitGroupList: HabitGroupList) {
private val habitFixtures = HabitFixtures(modelFactory, habitList)
fun createEmptyHabitGroup(
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0
): HabitGroup {
val hgr = modelFactory.buildHabitGroup()
hgr.name = name
hgr.question = "Did you exercise today?"
hgr.color = color
hgr.position = position
saveIfSQLite(hgr)
return hgr
}
fun createGroupWithEmptyHabits(
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
numHabits: Int = 1
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createEmptyHabit())
}
saveIfSQLite(hgr)
return hgr
}
fun createGroupWithEmptyNumericalHabits(
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
targetType: NumericalHabitType = NumericalHabitType.AT_LEAST,
numHabits: Int = 1
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createEmptyNumericalHabit(targetType))
}
saveIfSQLite(hgr)
return hgr
}
fun createGroupWithNumericalHabits(
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
numHabits: Int = 1
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createNumericalHabit())
}
saveIfSQLite(hgr)
return hgr
}
fun createGroupWithLongHabits(
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
numHabits: Int = 1
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createLongHabit())
}
saveIfSQLite(hgr)
return hgr
}
fun createGroupWithShortHabits(
name: String = "Exercise",
color: PaletteColor = PaletteColor(3),
position: Int = 0,
numHabits: Int = 1
): HabitGroup {
val hgr = createEmptyHabitGroup(name, color, position)
for (i in 1..numHabits) {
hgr.habitList.add(habitFixtures.createShortHabit())
}
saveIfSQLite(hgr)
return hgr
}
private fun saveIfSQLite(hgr: HabitGroup) {
if (hgr.habitList !is SQLiteHabitList) return
habitGroupList.add(hgr)
}
}

@ -0,0 +1,124 @@
/*
* Copyright (C) 2016-2021 Álinson Santos Xavier <git@axavier.org>
*
* 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
import org.hamcrest.CoreMatchers.`is`
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.core.IsEqual.equalTo
import org.isoron.uhabits.core.BaseUnitTest
import org.isoron.uhabits.core.utils.DateUtils.Companion.getToday
import org.junit.Assert.assertNotEquals
import org.junit.Test
import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertTrue
class HabitGroupTest : BaseUnitTest() {
@Throws(Exception::class)
override fun setUp() {
super.setUp()
}
@Test
fun testUuidGeneration() {
val uuid1 = modelFactory.buildHabitGroup().uuid!!
val uuid2 = modelFactory.buildHabitGroup().uuid!!
assertNotEquals(uuid1, uuid2)
}
@Test
fun test_copyAttributes() {
val model = modelFactory.buildHabitGroup()
model.isArchived = true
model.color = PaletteColor(0)
model.reminder = Reminder(8, 30, WeekdayList(1))
val habitGroup = modelFactory.buildHabitGroup()
habitGroup.copyFrom(model)
assertEquals(habitGroup.isArchived, model.isArchived)
assertThat(habitGroup.isArchived, `is`(model.isArchived))
assertThat(habitGroup.color, `is`(model.color))
assertThat(habitGroup.reminder, equalTo(model.reminder))
}
@Test
fun test_hasReminder() {
val hgr = modelFactory.buildHabitGroup()
assertThat(hgr.hasReminder(), `is`(false))
hgr.reminder = Reminder(8, 30, WeekdayList.EVERY_DAY)
assertThat(hgr.hasReminder(), `is`(true))
}
@Test
@Throws(Exception::class)
fun test_isCompleted() {
val hgr = groupFixtures.createGroupWithEmptyHabits()
assertFalse(hgr.isCompletedToday())
hgr.habitList.getByPosition(0).originalEntries.add(Entry(getToday(), Entry.YES_MANUAL))
hgr.recompute()
assertTrue(hgr.isCompletedToday())
}
@Test
@Throws(Exception::class)
fun test_isEntered() {
val hgr = groupFixtures.createGroupWithEmptyHabits()
assertFalse(hgr.isEnteredToday())
hgr.habitList.getByPosition(0).originalEntries.add(Entry(getToday(), Entry.NO))
hgr.recompute()
assertTrue(hgr.isEnteredToday())
}
@Test
@Throws(Exception::class)
fun test_isCompleted_numerical() {
val hgr = groupFixtures.createGroupWithEmptyNumericalHabits()
val h = hgr.habitList.getByPosition(0)
assertFalse(hgr.isCompletedToday())
h.originalEntries.add(Entry(getToday(), 4000))
h.recompute()
assertTrue(hgr.isCompletedToday())
h.originalEntries.add(Entry(getToday(), 2000))
h.recompute()
assertTrue(hgr.isCompletedToday())
h.originalEntries.add(Entry(getToday(), 1000))
h.recompute()
assertFalse(hgr.isCompletedToday())
h.targetType = NumericalHabitType.AT_MOST
h.originalEntries.add(Entry(getToday(), 4000))
h.recompute()
assertFalse(hgr.isCompletedToday())
h.originalEntries.add(Entry(getToday(), 2000))
h.recompute()
assertTrue(hgr.isCompletedToday())
h.originalEntries.add(Entry(getToday(), 1000))
h.recompute()
assertTrue(hgr.isCompletedToday())
}
@Test
@Throws(Exception::class)
fun testURI() {
assertTrue(habitGroupList.isEmpty)
val hgr = modelFactory.buildHabitGroup()
habitGroupList.add(hgr)
assertThat(hgr.id, equalTo(0L))
assertThat(hgr.uriString, equalTo("content://org.isoron.uhabits/habitgroup/0"))
}
}
Loading…
Cancel
Save