-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbentity_test.go
89 lines (83 loc) · 2.33 KB
/
dbentity_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 main
import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"
)
func TestNewDbEntity(t *testing.T) {
type args struct {
id int64
name string
}
tests := []struct {
name string
args args
wantEntity DbEntity
wantErr bool
}{
{"zero id", args{0, ""}, DbEntity{}, true},
{"negative id", args{-1, ""}, DbEntity{}, true},
{"overflow id", args{1 << 32, ""}, DbEntity{}, true},
{"empty name", args{1, ""}, DbEntity{}, true},
{"bad name", args{1, " \t "}, DbEntity{}, true},
{"valid", args{3, "Foods"}, DbEntity{EntityID(3), "Foods"}, false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotEntity, err := NewDbEntity(tt.args.id, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("NewDbEntity() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(gotEntity, tt.wantEntity) {
t.Errorf("NewDbEntity() gotEntity = %v, want %v", gotEntity, tt.wantEntity)
}
})
}
}
func TestDbEntity_GetId(t *testing.T) {
e := DbEntity{}
assert.Equal(t, uint32(0), e.GetId())
e = DbEntity{ID: 1134, DbName: "Something"}
assert.Equal(t, uint32(1134), e.GetId())
}
func TestDbEntity_GetName(t *testing.T) {
e := DbEntity{}
assert.Equal(t, "", e.GetName())
e.DbName = "chicken & Biscuits"
assert.Equal(t, "chicken & Biscuits", e.GetName())
}
func Test_validateEntity(t *testing.T) {
type args struct {
id int64
name string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{"zero value", args{0, ""}, "", true},
{"empty name", args{1, ""}, "", true},
{"whitespace name", args{1, " \t"}, "", true},
{"invalid name", args{1, "a"}, "", true},
{"invalid name w/space", args{1, " a "}, "", true},
{"excessive id", args{int64(1) << 34, "Sol"}, "", true},
{"1, Sol", args{1, "Sol"}, "Sol", false},
{"1, Sol w/spaces", args{1, " \t Sol \t "}, "Sol", false},
{"~0, sOl sYsTeM w/spaces", args{(int64(1) << 32) - 1, " \t sOl sYsTeM\t "}, "sOl sYsTeM", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := validateEntity(tt.args.id, tt.args.name)
if (err != nil) != tt.wantErr {
t.Errorf("validateEntity() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("validateEntity() got = %v, want %v", got, tt.want)
}
})
}
}