Skip to content

Instantly share code, notes, and snippets.

@phuctm97
Created September 27, 2019 16:22
Show Gist options
  • Save phuctm97/fcdd386bc0c2de2585205a6fbd2f1cf0 to your computer and use it in GitHub Desktop.
Save phuctm97/fcdd386bc0c2de2585205a6fbd2f1cf0 to your computer and use it in GitHub Desktop.
Go RESTful Series
package users
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestValidateUser(t *testing.T) {
tests := []struct {
user User
errs []error
}{
{
User{
Username: "hasInvalidCh@r",
Email: "invalidemail",
FullName: "Good Full Name",
},
[]error{ErrUsernameContainsInvalidChar, ErrEmailIsInvalid},
},
{
User{
Username: "1beginWithNumber",
Email: "[email protected]",
FullName: "",
},
[]error{ErrUsernameBeginsWithInvalidChar, ErrFullNameIsTooShort},
},
{
User{
Username: "this-username_is.valid.but-too_long",
Email: "invalid@email",
FullName: "Good Full Name",
},
[]error{ErrUsernameIsTooLong, ErrEmailIsInvalid},
},
{
User{
Username: "username.dot-dash_underscore",
Email: "[email protected]",
FullName: "Good Full Name",
Bio: "Good Bio",
},
nil,
},
}
for _, test := range tests {
errs := ValidateUser(&test.user)
if test.errs == nil {
assert.Nil(t, errs)
} else {
assert.ElementsMatch(t, errs, test.errs)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment