// init performs some verification that the TagCommand object // is valid (has a non-null database & UI and a user id). // // It loads all of the UserID's tags into the tags 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 *TagCommand) 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 tags iter, err := c.DB.Query(models.TagKind).Select(data.AttrMap{ "owner_id": c.UserID, }).Execute() if err != nil { c.errorf("data retrieval: querying tags") return failure } t := models.NewTag() tags := make([]*models.Tag, 0) for iter.Next(t) { tags = append(tags, t) t = models.NewTag() } if err := iter.Close(); err != nil { c.errorf("data retrieval: querying tags") return failure } c.tags = tags sort.Sort(tag.ByName(c.tags)) return success }
func newTestTag(t *testing.T, db data.DB, u *models.User) *models.Tag { tg := models.NewTag() tg.SetID(db.NewID()) tg.SetOwner(u) if err := db.Save(tg); err != nil { t.Fatal(err) } return tg }
func (c *TagCommand) runNew(args []string) int { t := models.NewTag() t.SetID(c.DB.NewID()) t.OwnerId = c.UserID var err error t.Name, err = stringInput(c.UI, "Name") if err != nil { c.errorf("Input Error: %s", err) return failure } if err := c.DB.Save(t); err != nil { c.errorf("Error saving tag: %s", err) return failure } return success }
func (c *XCommand) runTaskTime(args []string) int { db := c.DB iter, _ := db.Query(models.TagKind).Select(data.AttrMap{"owner_id": c.UserID}).Execute() t := models.NewTag() for iter.Next(t) { tasks, _ := tag.TasksFor(db, t) var totalTime time.Duration for _, t := range tasks { totalTime += task.TimeSpent(t) } c.UI.Output(t.Name) c.UI.Output(fmt.Sprintf("\t%s", totalTime)) } iter.Close() return success }
// TestTagNew test the `new` subcommand func TestTagNew(t *testing.T) { ui, db, _, c := newMockTagCommand(t) tagName := "asdkfjlasdjfasfdA" // just input the name ui.InputReader = bytes.NewBufferString(tagName + "\n") t.Log("running: `elos tag 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 a 'Name' for asking for the tag's name") } // check that it was created tg := models.NewTag() if err := db.PopulateByField("name", tagName, tg); err != nil { t.Fatal(err) } }