-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_test.go
89 lines (84 loc) · 2.04 KB
/
user_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package model_cassandra
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/surahman/mcq-platform/pkg/validator"
)
func TestValidateUser(t *testing.T) {
testCases := []struct {
name string
input *UserAccount
expectErr require.ErrorAssertionFunc
expectedLen int
}{
// ----- test cases start ----- //
{
name: "Empty",
input: &UserAccount{},
expectErr: require.Error,
expectedLen: 5,
}, {
name: "Valid",
input: &UserAccount{
UserLoginCredentials: UserLoginCredentials{
Username: "username1",
Password: "password-1",
},
FirstName: "first name",
LastName: "last name",
Email: "[email protected]",
},
expectErr: require.NoError,
expectedLen: 0,
}, {
name: "Invalid username",
input: &UserAccount{
UserLoginCredentials: UserLoginCredentials{
Username: "username 1",
Password: "password-1",
},
FirstName: "first name",
LastName: "last name",
Email: "[email protected]",
},
expectErr: require.Error,
expectedLen: 1,
}, {
name: "Invalid password",
input: &UserAccount{
UserLoginCredentials: UserLoginCredentials{
Username: "username1",
Password: "password-1password-1password-1password-1",
},
FirstName: "first name",
LastName: "last name",
Email: "[email protected]",
},
expectErr: require.Error,
expectedLen: 1,
}, {
name: "Invalid email",
input: &UserAccount{
UserLoginCredentials: UserLoginCredentials{
Username: "username1",
Password: "password-1",
},
FirstName: "first name",
LastName: "last name",
Email: "username@email",
},
expectErr: require.Error,
expectedLen: 1,
},
// ----- test cases end ----- //
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
err := validator.ValidateStruct(testCase.input)
testCase.expectErr(t, err)
if err != nil {
require.Equal(t, testCase.expectedLen, len(err.(*validator.ErrorValidation).Errors))
}
})
}
}