Created
September 27, 2019 16:22
-
-
Save phuctm97/fcdd386bc0c2de2585205a6fbd2f1cf0 to your computer and use it in GitHub Desktop.
Go RESTful Series
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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