func TestUser_SignUp_failsIfUserHasSignedUpAlready(t *testing.T) { password := ess.Password() password.UnmarshalText([]byte("password")) history := []*ess.Event{ ess.NewEvent("user.signed-up"). Add("username", "test-user"). Add("password", password.String()). Add("email", "*****@*****.**"). Add("name", "John Doe"), } user := NewUser("username") for _, event := range history { user.HandleEvent(event) } err := user.SignUp("Jane Doe", "*****@*****.**", password.String()) if err == nil { t.Fatal("Expected an error") } verr, ok := err.(*ess.ValidationError) if !ok { t.Fatalf("err.(type) = %T; want %T", err, verr) } if got, want := len(verr.Errors["username"]), 1; got != want { t.Errorf(`len(verr.Errors["username"]) = %v; want %v`, got, want) } if got, want := verr.Errors["username"][0], "not_unique"; got != want { t.Errorf(`verr.Errors["username"][0] = %v; want %v`, got, want) } }
package main import ( "fmt" "log" "net/http" "github.com/dhamidi/ess" ) var SignUp = ess.NewCommandDefinition("sign-up"). Id("username", ess.Id()). Field("name", ess.TrimmedString()). Field("email", ess.EmailAddress()). Field("password", ess.Password()). Target(NewUserFromCommand) func NewUserFromCommand(command *ess.Command) ess.Aggregate { return NewUser(command.Get("username").String()) } type User struct { id string events ess.EventPublisher signedUp bool } func NewUser(username string) *User { return &User{ id: username, }