// runGoals runs the 'goals' subcommand, which prints the user's goals func (c *TodoCommand) runGoals() int { tasks, err := tag.TasksFor(c.DB, c.UserID, "GOAL") if err != nil { c.errorf("retrieving GOAL tasks: %s", err) return failure } taskIds := make(map[data.ID]bool) for i := range tasks { if !task.IsComplete(tasks[i]) { taskIds[tasks[i].ID()] = true } } if len(taskIds) == 0 { c.UI.Output("No goals set. Use `elos todo goal` to add a goal.") return success } c.UI.Output("Current Goals:") c.printTaskList(func(t *models.Task) bool { _, ok := taskIds[t.ID()] return ok }) return success }
// init performs some verification that the TodoCommand object // is valid (has a non-null database & UI and a user id). // // It loads all of the UserID's tasks into the tasks field of the // TodoCommand object. // // It loads all of the UserID's tags into the tags field of the // TodoCommand object. // // A 0 return value indicates success, a 1 indiciates failure. The // init command handles appropriate error printing the the UI. func (c *TodoCommand) init() int { // ensure that we have a interface if c.UI == nil { return failure // we can't c.errorf because the user interface isn't defined } // ensure that we have a database if c.DB == nil { c.errorf("initialization: no database") return failure } // ensure that we have a user id if c.UserID == "" { c.errorf("initialization: no user id") return failure } // Load the tasks iter, err := c.DB.Query(data.Kind(models.Kind_TASK.String())). Select(data.AttrMap{ "owner_id": c.UserID, }). Execute() if err != nil { c.errorf("data retrieval: querying tasks: %v", err) return failure } t := new(models.Task) tasks := make([]*models.Task, 0) for iter.Next(t) { if !task.IsComplete(t) { tasks = append(tasks, t) } t = new(models.Task) } if err := iter.Close(); err != nil { c.errorf("data retrieval: querying tasks") return failure } c.tasks = tasks sort.Sort(task.BySalience(c.tasks)) return success }
// TestTodoComplete tests the `complete` subcommand func TestTodoComplete(t *testing.T) { ui, db, user, c := newMockTodoCommand(t) // setup that there is one task tsk := newTestTask(t, db, user) // load the input ui.InputReader = bytes.NewBuffer([]byte("0\n")) // run the effect of `elos todo complete` code := c.Run([]string{"complete"}) 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, "0)") { t.Fatalf("Output should have contained a 0) for listing tasks") } if !strings.Contains(output, "Which number?") { t.Fatalf("Output should have asked for a task number") } t.Log("Checking that the task was completed") if err := db.PopulateByID(tsk); err != nil { t.Fatal(err) } t.Logf("Here's the task:\n%+v", tsk) if !task.IsComplete(tsk) { t.Fatalf("Expected the task to be complete") } }
// runToday executes the "elos todo today" command. // // Today prints the tasks that are were completed today func (c *TodoCommand) runToday() int { iter, err := c.DB.Query(data.Kind(models.Kind_TASK.String())).Select(data.AttrMap{ "owner_id": c.UserID, }).Execute() if err != nil { c.errorf("querying tasks: %s", err) } t := new(models.Task) i := 0 for iter.Next(t) { if task.IsComplete(t) && dayEquivalent(t.CompletedAt.Time().Local(), time.Now()) { c.UI.Output(fmt.Sprintf("%d) %s", i, String(t))) i++ } } if i == 0 { c.UI.Output("You have completed no tasks today") } return success }