Commit 009cf6ed by Brian Conway

Condense model unit tests with Default* generation funtions

parent a2e24771
Copyright (c) 2015-2016 Brian Conway <bconway@rcesoftware.com>
Copyright (c) 2015-2017 Brian Conway <bconway@rcesoftware.com>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
......@@ -11,4 +11,3 @@ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
......@@ -2,8 +2,14 @@ package main
import "testing"
func DefaultJob() Job {
uid := "00000000-0000-9000-0000-000000000000"
return Job{UserId: &uid}
}
func TestJobsValidateMissingUserId(t *testing.T) {
j := Job{}
j := DefaultJob()
j.UserId = nil
if err := j.Validate(); err != nil && err.Error() != JobReqUserId {
t.Errorf("\nRan: Job.Validate(%#v)\nExpected: %v\nGot: %v", j, JobReqUserId, err.Error())
......@@ -11,8 +17,7 @@ func TestJobsValidateMissingUserId(t *testing.T) {
}
func TestJobsValidatePassing(t *testing.T) {
uid := "00000000-0000-9000-0000-000000000000"
j := Job{UserId: &uid}
j := DefaultJob()
if err := j.Validate(); err != nil {
t.Errorf("\nRan: Job.Validate(%#v)\nExpected: %v\nGot: %v", j, nil, err.Error())
......
......@@ -2,10 +2,22 @@ package main
import "testing"
func TestUsersValidateMissingUserTypeId(t *testing.T) {
func DefaultUser() User {
ut := "user"
return User{UserType: &ut, FirstName: "First", LastName: "Last",
Email: "email@example.com", Login: "flast", BcryptPassword: "$2a$10$..."}
}
func DefaultUserWithPass() User {
ut := "user"
pass := "mypass"
u := User{FirstName: "First", LastName: "Last", Email: "email@example.com",
return User{UserType: &ut, FirstName: "First", LastName: "Last", Email: "email@example.com",
Login: "flast", Password: &pass}
}
func TestUsersValidateMissingUserTypeId(t *testing.T) {
u := DefaultUser()
u.UserType = nil
if err := u.Validate(); err != nil && err.Error() != UserReqUserTypeId {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, UserReqUserTypeId, err.Error())
......@@ -13,10 +25,8 @@ func TestUsersValidateMissingUserTypeId(t *testing.T) {
}
func TestUsersValidateMissingFirstName(t *testing.T) {
ut := "user"
pass := "mypass"
u := User{UserType: &ut, LastName: "Last", Email: "email@example.com", Login: "flast",
Password: &pass}
u := DefaultUser()
u.FirstName = ""
if err := u.Validate(); err != nil && err.Error() != UserReqFirstName {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, UserReqFirstName, err.Error())
......@@ -24,10 +34,8 @@ func TestUsersValidateMissingFirstName(t *testing.T) {
}
func TestUsersValidateMissingLastName(t *testing.T) {
ut := "user"
pass := "mypass"
u := User{UserType: &ut, FirstName: "First", Email: "email@example.com", Login: "flast",
Password: &pass}
u := DefaultUser()
u.LastName = ""
if err := u.Validate(); err != nil && err.Error() != UserReqLastName {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, UserReqLastName, err.Error())
......@@ -35,10 +43,8 @@ func TestUsersValidateMissingLastName(t *testing.T) {
}
func TestUsersValidateMissingEmail(t *testing.T) {
ut := "user"
pass := "mypass"
u := User{UserType: &ut, FirstName: "First", LastName: "Last", Login: "flast",
Password: &pass}
u := DefaultUser()
u.Email = ""
if err := u.Validate(); err != nil && err.Error() != UserReqEmail {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, UserReqEmail, err.Error())
......@@ -46,29 +52,25 @@ func TestUsersValidateMissingEmail(t *testing.T) {
}
func TestUsersValidateMissingLogin(t *testing.T) {
ut := "user"
pass := "mypass"
u := User{UserType: &ut, FirstName: "First", LastName: "Last", Password: &pass}
u := DefaultUser()
u.Login = ""
if err := u.Validate(); err != nil && err.Error() != UserReqEmail {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, UserReqEmail, err.Error())
if err := u.Validate(); err != nil && err.Error() != UserReqLogin {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, UserReqLogin, err.Error())
}
}
func TestUsersValidateMissingPasswordBcrypt(t *testing.T) {
ut := "user"
u := User{UserType: &ut, FirstName: "First", LastName: "Last", Login: "flast"}
u := DefaultUser()
u.BcryptPassword = ""
if err := u.Validate(); err != nil && err.Error() != UserReqEmail {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, UserReqEmail, err.Error())
if err := u.Validate(); err != nil && err.Error() != UserReqPassword {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, UserReqPassword, err.Error())
}
}
func TestUsersValidatePassingPassword(t *testing.T) {
ut := "user"
pass := "mypass"
u := User{UserType: &ut, FirstName: "First", LastName: "Last", Email: "email@example.com",
Login: "flast", Password: &pass}
u := DefaultUserWithPass()
if err := u.Validate(); err != nil {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, nil, err.Error())
......@@ -76,9 +78,7 @@ func TestUsersValidatePassingPassword(t *testing.T) {
}
func TestUsersValidatePassingBcrypt(t *testing.T) {
ut := "user"
u := User{UserType: &ut, FirstName: "First", LastName: "Last", Email: "email@example.com",
Login: "flast", BcryptPassword: "$2a$10$..."}
u := DefaultUser()
if err := u.Validate(); err != nil {
t.Errorf("\nRan: User.Validate(%#v)\nExpected: %v\nGot: %v", u, nil, err.Error())
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment