domain_test.TestUser_Validate   B
last analyzed

Complexity

Conditions 7

Size

Total Lines 52
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 37
nop 1
dl 0
loc 52
rs 7.592
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
package domain_test
2
3
import (
4
	"testing"
5
6
	"github.com/memnix/memnix-rest/domain"
7
)
8
9
func TestUser_ToPublicUser(t *testing.T) {
10
	user := &domain.User{
11
		Username:   "testuser",
12
		Email:      "[email protected]",
13
		Avatar:     "https://example.com/avatar.png",
14
		Permission: domain.PermissionUser,
15
	}
16
17
	user.ID = 1
18
19
	expected := domain.PublicUser{
20
		Username:   "testuser",
21
		Email:      "[email protected]",
22
		Avatar:     "https://example.com/avatar.png",
23
		ID:         1,
24
		Permission: domain.PermissionUser,
25
	}
26
27
	publicUser := user.ToPublicUser()
28
29
	if publicUser != expected {
30
		t.Errorf("Expected public user to be %v, but got %v", expected, publicUser)
31
	}
32
}
33
34
func TestUser_HasPermission(t *testing.T) {
35
	useCases := []struct {
36
		user     *domain.User
37
		name     string
38
		perm     domain.Permission
39
		expected bool
40
	}{
41
		{
42
			name: "UserHasPermission",
43
			user: &domain.User{
44
				Permission: domain.PermissionUser,
45
			},
46
			perm:     domain.PermissionUser,
47
			expected: true,
48
		},
49
		{
50
			name: "UserDoesNotHavePermission",
51
			user: &domain.User{
52
				Permission: domain.PermissionUser,
53
			},
54
			perm:     domain.PermissionAdmin,
55
			expected: false,
56
		},
57
		{
58
			name: "AdminHasPermission",
59
			user: &domain.User{
60
				Permission: domain.PermissionAdmin,
61
			},
62
			perm:     domain.PermissionUser,
63
			expected: true,
64
		},
65
		{
66
			name: "AdminHasAdminPermission",
67
			user: &domain.User{
68
				Permission: domain.PermissionAdmin,
69
			},
70
			perm:     domain.PermissionAdmin,
71
			expected: true,
72
		},
73
	}
74
75
	for _, uc := range useCases {
76
		t.Run(uc.name, func(t *testing.T) {
77
			result := uc.user.HasPermission(uc.perm)
78
			if result != uc.expected {
79
				t.Errorf("Expected result to be %v, but got %v", uc.expected, result)
80
			}
81
		})
82
	}
83
}
84
85
func TestRegister_Validate(t *testing.T) {
86
	testCases := []struct {
87
		register  *domain.Register
88
		name      string
89
		shouldErr bool
90
	}{
91
		{
92
			name: "Valid Register",
93
			register: &domain.Register{
94
				Username: "testuser",
95
				Email:    "[email protected]",
96
				Password: "passwordP@ssw0rd",
97
			},
98
			shouldErr: false,
99
		},
100
		{
101
			name: "No Username Register",
102
			register: &domain.Register{
103
				Username: "",
104
				Email:    "[email protected]",
105
				Password: "password",
106
			},
107
			shouldErr: true,
108
		},
109
		{
110
			name: "No Email Register",
111
			register: &domain.Register{
112
				Username: "testuser",
113
				Email:    "",
114
				Password: "password",
115
			},
116
			shouldErr: true,
117
		},
118
		{
119
			name: "No Password Register",
120
			register: &domain.Register{
121
				Username: "testuser",
122
				Email:    "[email protected]",
123
				Password: "",
124
			},
125
			shouldErr: true,
126
		},
127
	}
128
129
	for _, tc := range testCases {
130
		t.Run(tc.name, func(t *testing.T) {
131
			err := tc.register.Validate()
132
			if tc.shouldErr && err == nil {
133
				t.Errorf("Expected error, but got nil")
134
			}
135
			if !tc.shouldErr && err != nil {
136
				t.Errorf("Expected no error, but got %v", err)
137
			}
138
		})
139
	}
140
}
141
142
func TestRegister_ToUser(t *testing.T) {
143
	register := &domain.Register{
144
		Username: "testuser",
145
		Email:    "[email protected]",
146
		Password: "password",
147
	}
148
149
	expected := domain.User{
150
		Username:   "testuser",
151
		Email:      "[email protected]",
152
		Password:   "password",
153
		Permission: domain.PermissionUser,
154
	}
155
156
	user := register.ToUser()
157
158
	if user.Username != expected.Username {
159
		t.Errorf("Expected user username to be %s, but got %s", expected.Username, user.Username)
160
	}
161
162
	if user.Email != expected.Email {
163
		t.Errorf("Expected user email to be %s, but got %s", expected.Email, user.Email)
164
	}
165
166
	if user.Password != expected.Password {
167
		t.Errorf("Expected user password to be %s, but got %s", expected.Password, user.Password)
168
	}
169
170
	if user.Permission != expected.Permission {
171
		t.Errorf("Expected user permission to be %v, but got %v", expected.Permission, user.Permission)
172
	}
173
}
174
175
func TestUser_TableName(t *testing.T) {
176
	user := &domain.User{}
177
	expected := "users"
178
	result := user.TableName()
179
	if result != expected {
180
		t.Errorf("Expected table name to be %s, but got %s", expected, result)
181
	}
182
}
183
184
func TestUser_Validate(t *testing.T) {
185
	useCases := []struct {
186
		user      *domain.User
187
		name      string
188
		shouldErr bool
189
	}{
190
		{
191
			name: "Valid User",
192
			user: &domain.User{
193
				Username: "testuser",
194
				Email:    "[email protected]",
195
				Password: "password",
196
			},
197
			shouldErr: false,
198
		},
199
		{
200
			name: "No Username User",
201
			user: &domain.User{
202
				Username: "",
203
				Email:    "[email protected]",
204
				Password: "password",
205
			},
206
			shouldErr: true,
207
		},
208
		{
209
			name: "No Email User",
210
			user: &domain.User{
211
				Username: "testuser",
212
				Email:    "",
213
				Password: "password",
214
			},
215
			shouldErr: true,
216
		},
217
		{
218
			name: "No Password User",
219
			user: &domain.User{
220
				Username: "testuser",
221
				Email:    "[email protected]",
222
				Password: "",
223
			},
224
			shouldErr: true,
225
		},
226
	}
227
228
	for _, uc := range useCases {
229
		t.Run(uc.name, func(t *testing.T) {
230
			err := uc.user.Validate()
231
			if uc.shouldErr && err == nil {
232
				t.Errorf("Expected error, but got nil")
233
			}
234
			if !uc.shouldErr && err != nil {
235
				t.Errorf("Expected no error, but got %v", err)
236
			}
237
		})
238
	}
239
}
240