From 4dc8201dcb12ed7af0b9acb9450ff279cfa55f1b Mon Sep 17 00:00:00 2001 From: Alinson Xavier Date: Thu, 31 Mar 2016 07:20:20 -0400 Subject: [PATCH] Add tests for CreateHabitCommand --- .../unit/commands/CreateHabitCommandTest.java | 88 +++++++++++++++++++ .../uhabits/commands/CreateHabitCommand.java | 2 +- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 app/src/androidTest/java/org/isoron/uhabits/unit/commands/CreateHabitCommandTest.java diff --git a/app/src/androidTest/java/org/isoron/uhabits/unit/commands/CreateHabitCommandTest.java b/app/src/androidTest/java/org/isoron/uhabits/unit/commands/CreateHabitCommandTest.java new file mode 100644 index 000000000..aeaa3948e --- /dev/null +++ b/app/src/androidTest/java/org/isoron/uhabits/unit/commands/CreateHabitCommandTest.java @@ -0,0 +1,88 @@ +/* + * 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.CreateHabitCommand; +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 java.util.Collections; +import java.util.List; + +import static junit.framework.Assert.assertFalse; +import static junit.framework.Assert.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.equalTo; + +@RunWith(AndroidJUnit4.class) +@SmallTest +public class CreateHabitCommandTest extends BaseTest +{ + + private CreateHabitCommand command; + private Habit model; + + @Before + public void setup() + { + super.setup(); + + model = new Habit(); + model.name = "New habit"; + command = new CreateHabitCommand(model); + + HabitFixtures.purgeHabits(); + } + + @Test + public void executeUndoRedo() + { + assertTrue(Habit.getAll(true).isEmpty()); + + command.execute(); + + List allHabits = Habit.getAll(true); + assertThat(allHabits.size(), equalTo(1)); + + Habit habit = allHabits.get(0); + Long id = habit.getId(); + assertThat(habit.name, equalTo(model.name)); + + command.undo(); + assertTrue(Habit.getAll(true).isEmpty()); + + command.execute(); + allHabits = Habit.getAll(true); + assertThat(allHabits.size(), equalTo(1)); + + habit = allHabits.get(0); + Long newId = habit.getId(); + assertThat(id, equalTo(newId)); + assertThat(habit.name, equalTo(model.name)); + } +} diff --git a/app/src/main/java/org/isoron/uhabits/commands/CreateHabitCommand.java b/app/src/main/java/org/isoron/uhabits/commands/CreateHabitCommand.java index 96275b024..7cc9ad51c 100644 --- a/app/src/main/java/org/isoron/uhabits/commands/CreateHabitCommand.java +++ b/app/src/main/java/org/isoron/uhabits/commands/CreateHabitCommand.java @@ -53,7 +53,7 @@ public class CreateHabitCommand extends Command Habit habit = Habit.get(savedId); if(habit == null) throw new RuntimeException("Habit not found"); - habit.delete(); + habit.cascadeDelete(); } @Override