示例#1
0
文件: habit.go 项目: elos/elos
// init performs the necessary initliazation for the *HabitCommand
// It ensures we have a UI, DB and UserID, so those can be treated
// as invariants throughought the rest of the code.
//
// Additionally it loads this user's habit list.
func (c *HabitCommand) init() int {
	if c.UI == nil {
		// can't use errorf, because the UI is not defined
		return failure
	}

	if c.UserID == "" {
		c.errorf("no UserID provided")
		return failure
	}

	if c.DB == nil {
		c.errorf("no database")
		return failure
	}

	q := c.DB.Query(models.HabitKind)
	q.Select(data.AttrMap{
		"owner_id": c.UserID,
	})
	iter, err := q.Execute()
	if err != nil {
		c.errorf("while querying for habits: %s", err)
		return failure
	}

	c.habits = make([]*models.Habit, 0)
	habit := models.NewHabit()

	for iter.Next(habit) {
		c.habits = append(c.habits, habit)
		habit = models.NewHabit()
	}

	if err := iter.Close(); err != nil {
		c.errorf("while querying for habits: %s", err)
		return failure
	}

	return success
}
示例#2
0
文件: habit_test.go 项目: elos/elos
// --- `elos habit new` {{{
func TestHabitNew(t *testing.T) {
	ui, db, _, c := newMockHabitCommand(t)

	habitName := "MyHabit"

	input := strings.Join([]string{
		habitName,
	}, "\n")

	ui.InputReader = bytes.NewBufferString(input)

	t.Log("running: `elos habit new`")
	code := c.Run([]string{"new"})
	t.Log("command `new` terminated")

	errput := ui.ErrorWriter.String()
	output := ui.OutputWriter.String()
	t.Logf("Error output:\n%s", errput)
	t.Logf("Output:\n%s", output)

	// verify there were no errors
	if errput != "" {
		t.Fatalf("Expected no error output, got: %s", errput)
	}

	// verify success
	if code != success {
		t.Fatalf("Expected successful exit code along with empty error output.")
	}

	// verify some of the output
	if !strings.Contains(output, "Name") {
		t.Fatalf("Output should have contained 'Name' for inputting the name of the new habit")
	}

	h := models.NewHabit()
	if err := db.PopulateByField("name", habitName, h); err != nil {
		t.Fatalf("Error looking for habit with name: '%s': %s", habitName, err)
	}

	if tag, err := h.Tag(db); err != nil {
		t.Fatalf("Error while looking up tag for a habit: %s", err)
	} else {
		if tag.Name != habitName {
			t.Fatalf("Should also create a tag name with the proper name")
		}
	}
}