From 5e0422848b2d4fd08732fc8c86d96c2b9baafb35 Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Thu, 31 Mar 2016 07:54:00 -0400 Subject: [PATCH] Add tests for EditHabitCommand --- .../unit/commands/EditHabitCommandTest.java | 120 ++++++++++++++++++ .../uhabits/commands/EditHabitCommand.java | 27 ++-- 2 files changed, 135 insertions(+), 12 deletions(-) create mode 100644 app/src/androidTest/java/org/isoron/uhabits/unit/commands/EditHabitCommandTest.java diff --git a/app/src/androidTest/java/org/isoron/uhabits/unit/commands/EditHabitCommandTest.java b/app/src/androidTest/java/org/isoron/uhabits/unit/commands/EditHabitCommandTest.java new file mode 100644 index 000000000..f9612b11a --- /dev/null +++ b/app/src/androidTest/java/org/isoron/uhabits/unit/commands/EditHabitCommandTest.java @@ -0,0 +1,120 @@ +/* + * Copyright (C) 2016 Álinson Santos Xavier + * + * 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 . + */ + +package org.isoron.uhabits.unit.commands; + +import android.support.test.runner.AndroidJUnit4; +import android.test.suitebuilder.annotation.SmallTest; + +import org.isoron.uhabits.BaseTest; +import org.isoron.uhabits.commands.EditHabitCommand; +import org.isoron.uhabits.models.Habit; +import org.isoron.uhabits.unit.HabitFixtures; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; + +import static junit.framework.Assert.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class EditHabitCommandTest extends BaseTest +{ + + private EditHabitCommand command; + private Habit habit; + private Habit modified; + private Long id; + + @Before + public void setup() + { + super.setup(); + + habit = HabitFixtures.createShortHabit(); + habit.name = "original"; + habit.freqDen = 1; + habit.freqNum = 1; + habit.save(); + + id = habit.getId(); + + modified = new Habit(habit); + modified.name = "modified"; + } + + @Test + public void executeUndoRedo() + { + command = new EditHabitCommand(habit, modified); + + int originalScore = habit.scores.getTodayValue(); + assertThat(habit.name, equalTo("original")); + + command.execute(); + refreshHabit(); + assertThat(habit.name, equalTo("modified")); + assertThat(habit.scores.getTodayValue(), equalTo(originalScore)); + + command.undo(); + refreshHabit(); + assertThat(habit.name, equalTo("original")); + assertThat(habit.scores.getTodayValue(), equalTo(originalScore)); + + command.execute(); + refreshHabit(); + assertThat(habit.name, equalTo("modified")); + assertThat(habit.scores.getTodayValue(), equalTo(originalScore)); + } + + @Test + public void executeUndoRedo_withModifiedInterval() + { + modified.freqNum = 1; + modified.freqDen = 7; + command = new EditHabitCommand(habit, modified); + + int originalScore = habit.scores.getTodayValue(); + assertThat(habit.name, equalTo("original")); + + command.execute(); + refreshHabit(); + assertThat(habit.name, equalTo("modified")); + assertThat(habit.scores.getTodayValue(), greaterThan(originalScore)); + + command.undo(); + refreshHabit(); + assertThat(habit.name, equalTo("original")); + assertThat(habit.scores.getTodayValue(), equalTo(originalScore)); + + command.execute(); + refreshHabit(); + assertThat(habit.name, equalTo("modified")); + assertThat(habit.scores.getTodayValue(), greaterThan(originalScore)); + } + + private void refreshHabit() + { + habit = Habit.get(id); + assertTrue(habit != null); + } +} diff --git a/app/src/main/java/org/isoron/uhabits/commands/EditHabitCommand.java b/app/src/main/java/org/isoron/uhabits/commands/EditHabitCommand.java index 853bd6254..7a7787d6a 100644 --- a/app/src/main/java/org/isoron/uhabits/commands/EditHabitCommand.java +++ b/app/src/main/java/org/isoron/uhabits/commands/EditHabitCommand.java @@ -39,28 +39,31 @@ public class EditHabitCommand extends Command !this.original.freqNum.equals(this.modified.freqNum)); } + @Override public void execute() { - Habit habit = Habit.get(savedId); - if(habit == null) throw new RuntimeException("Habit not found"); - - habit.copyAttributes(modified); - habit.save(); - if (hasIntervalChanged) - { - habit.checkmarks.deleteNewerThan(0); - habit.streaks.deleteNewerThan(0); - habit.scores.invalidateNewerThan(0); - } + copyAttributes(this.modified); } + @Override public void undo() + { + copyAttributes(this.original); + } + + private void copyAttributes(Habit model) { Habit habit = Habit.get(savedId); if(habit == null) throw new RuntimeException("Habit not found"); - habit.copyAttributes(original); + habit.copyAttributes(model); habit.save(); + + invalidateIfNeeded(habit); + } + + private void invalidateIfNeeded(Habit habit) + { if (hasIntervalChanged) { habit.checkmarks.deleteNewerThan(0);