// promptNewPerson provides the input prompts necessary to construct a new person. // // Use this to implement the 'new' subcommand, and for any subcommand which requires // creating a new person as part of it's functionality. // // It returns a person and status code. A 0 status code indicates success, any other // status indicates failure, and the caller should exit immediately. promptNewPerson // will have taken care of printing the error output. func (c *PeopleCommand) promptNewPerson() (*models.Person, int) { p := models.NewPerson() p.SetID(c.DB.NewID()) p.CreatedAt = time.Now() var inputErr error if p.FirstName, inputErr = stringInput(c.UI, "First Name:"); inputErr != nil { c.errorf("input error: %s", inputErr) return nil, failure } if p.LastName, inputErr = stringInput(c.UI, "Last Name:"); inputErr != nil { c.errorf("input error: %s", inputErr) return nil, failure } p.OwnerId = c.UserID p.UpdatedAt = time.Now() if err := c.DB.Save(p); err != nil { c.errorf("error saving person: %s", err) return nil, failure } return p, success }
func newTestPerson(t *testing.T, db data.DB, u *models.User) *models.Person { person := models.NewPerson() person.SetID(db.NewID()) person.CreatedAt = time.Now() person.OwnerId = u.ID().String() person.UpdatedAt = time.Now() if err := db.Save(person); err != nil { t.Fatalf("newTestPerson Error: %s", err) } return person }
// init performs the necessary initliazation for the *PeopleCommand // 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 people list. func (c *PeopleCommand) 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.PersonKind) q.Select(data.AttrMap{ "owner_id": c.UserID, }) iter, err := q.Execute() if err != nil { c.errorf("while querying for people: %s", err) return failure } c.people = make([]*models.Person, 0) person := models.NewPerson() for iter.Next(person) { c.people = append(c.people, person) person = models.NewPerson() } if err := iter.Close(); err != nil { c.errorf("while querying for people: %s", err) return failure } return success }
// --- `elos people new` {{{ func TestPeopleNew(t *testing.T) { ui, db, _, c := newMockPeopleCommand(t) input := strings.Join([]string{ "Nick", // First Name "Landolfi", // Last Name }, "\n") ui.InputReader = bytes.NewBufferString(input) t.Log("running: `elos people 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, "First Name") { t.Fatalf("Output should have contained 'First Name' for first_name trait") } person := models.NewPerson() if err := db.PopulateByField("first_name", "Nick", person); err != nil { t.Fatal("Error looking for the new person: %s", err) } // verify the last name was set properly if person.LastName != "Landolfi" { t.Fatal("Last name should have been Landolfi, not '%s'", person.LastName) } }