| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"database/sql" |
| 5 |
"database/sql/driver" |
| 6 |
"errors" |
| 7 |
"math" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
) |
| 12 |
|
| 13 |
func TestIsInvalidDBNameChar(t *testing.T) { |
| 14 |
for _, db := range []string{"db", "dbName", "db_name", "db1", "1dbname", "db$name"} { |
| 15 |
if fields := strings.FieldsFunc(db, IsInvalidDBNameChar); len(fields) != 1 { |
| 16 |
t.Fatalf("failed to parse db name %v", db) |
| 17 |
} |
| 18 |
} |
| 19 |
} |
| 20 |
|
| 21 |
func TestCheckTruth(t *testing.T) { |
| 22 |
checkTruthTests := []struct { |
| 23 |
v string |
| 24 |
out bool |
| 25 |
}{ |
| 26 |
{"123", true}, |
| 27 |
{"true", true}, |
| 28 |
{"", false}, |
| 29 |
{"false", false}, |
| 30 |
{"False", false}, |
| 31 |
{"FALSE", false}, |
| 32 |
{"\u0046alse", false}, |
| 33 |
} |
| 34 |
|
| 35 |
for _, test := range checkTruthTests { |
| 36 |
t.Run(test.v, func(t *testing.T) { |
| 37 |
if out := CheckTruth(test.v); out != test.out { |
| 38 |
t.Errorf("CheckTruth(%s) want: %t, got: %t", test.v, test.out, out) |
| 39 |
} |
| 40 |
}) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
func TestToStringKey(t *testing.T) { |
| 45 |
cases := []struct { |
| 46 |
values []interface{} |
| 47 |
key string |
| 48 |
}{ |
| 49 |
{[]interface{}{"a"}, "a"}, |
| 50 |
{[]interface{}{1, 2, 3}, "1_2_3"}, |
| 51 |
{[]interface{}{1, nil, 3}, "1_nil_3"}, |
| 52 |
{[]interface{}{[]interface{}{1, 2, 3}}, "[1 2 3]"}, |
| 53 |
{[]interface{}{[]interface{}{"1", "2", "3"}}, "[1 2 3]"}, |
| 54 |
{[]interface{}{[]interface{}{"1", nil, "3"}}, "[1 <nil> 3]"}, |
| 55 |
} |
| 56 |
for _, c := range cases { |
| 57 |
if key := ToStringKey(c.values...); key != c.key { |
| 58 |
t.Errorf("%v: expected %v, got %v", c.values, c.key, key) |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
func TestContains(t *testing.T) { |
| 64 |
containsTests := []struct { |
| 65 |
name string |
| 66 |
elems []string |
| 67 |
elem string |
| 68 |
out bool |
| 69 |
}{ |
| 70 |
{"exists", []string{"1", "2", "3"}, "1", true}, |
| 71 |
{"not exists", []string{"1", "2", "3"}, "4", false}, |
| 72 |
} |
| 73 |
for _, test := range containsTests { |
| 74 |
t.Run(test.name, func(t *testing.T) { |
| 75 |
if out := Contains(test.elems, test.elem); test.out != out { |
| 76 |
t.Errorf("Contains(%v, %s) want: %t, got: %t", test.elems, test.elem, test.out, out) |
| 77 |
} |
| 78 |
}) |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
type ModifyAt sql.NullTime |
| 83 |
|
| 84 |
// Value return a Unix time. |
| 85 |
func (n ModifyAt) Value() (driver.Value, error) { |
| 86 |
if !n.Valid { |
| 87 |
return nil, nil |
| 88 |
} |
| 89 |
return n.Time.Unix(), nil |
| 90 |
} |
| 91 |
|
| 92 |
func TestAssertEqual(t *testing.T) { |
| 93 |
now := time.Now() |
| 94 |
assertEqualTests := []struct { |
| 95 |
name string |
| 96 |
src, dst interface{} |
| 97 |
out bool |
| 98 |
}{ |
| 99 |
{"error equal", errors.New("1"), errors.New("1"), true}, |
| 100 |
{"error not equal", errors.New("1"), errors.New("2"), false}, |
| 101 |
{"driver.Valuer equal", ModifyAt{Time: now, Valid: true}, ModifyAt{Time: now, Valid: true}, true}, |
| 102 |
{"driver.Valuer not equal", ModifyAt{Time: now, Valid: true}, ModifyAt{Time: now.Add(time.Second), Valid: true}, false}, |
| 103 |
{"driver.Valuer equal (ptr to nil ptr)", (*ModifyAt)(nil), &ModifyAt{}, false}, |
| 104 |
} |
| 105 |
for _, test := range assertEqualTests { |
| 106 |
t.Run(test.name, func(t *testing.T) { |
| 107 |
if out := AssertEqual(test.src, test.dst); test.out != out { |
| 108 |
t.Errorf("AssertEqual(%v, %v) want: %t, got: %t", test.src, test.dst, test.out, out) |
| 109 |
} |
| 110 |
}) |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
func TestToString(t *testing.T) { |
| 115 |
tests := []struct { |
| 116 |
name string |
| 117 |
in interface{} |
| 118 |
out string |
| 119 |
}{ |
| 120 |
{"int", math.MaxInt64, "9223372036854775807"}, |
| 121 |
{"int8", int8(math.MaxInt8), "127"}, |
| 122 |
{"int16", int16(math.MaxInt16), "32767"}, |
| 123 |
{"int32", int32(math.MaxInt32), "2147483647"}, |
| 124 |
{"int64", int64(math.MaxInt64), "9223372036854775807"}, |
| 125 |
{"uint", uint(math.MaxUint64), "18446744073709551615"}, |
| 126 |
{"uint8", uint8(math.MaxUint8), "255"}, |
| 127 |
{"uint16", uint16(math.MaxUint16), "65535"}, |
| 128 |
{"uint32", uint32(math.MaxUint32), "4294967295"}, |
| 129 |
{"uint64", uint64(math.MaxUint64), "18446744073709551615"}, |
| 130 |
{"string", "abc", "abc"}, |
| 131 |
{"other", true, ""}, |
| 132 |
} |
| 133 |
for _, test := range tests { |
| 134 |
t.Run(test.name, func(t *testing.T) { |
| 135 |
if out := ToString(test.in); test.out != out { |
| 136 |
t.Fatalf("ToString(%v) want: %s, got: %s", test.in, test.out, out) |
| 137 |
} |
| 138 |
}) |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
func TestRTrimSlice(t *testing.T) { |
| 143 |
tests := []struct { |
| 144 |
name string |
| 145 |
input []int |
| 146 |
trimLen int |
| 147 |
expected []int |
| 148 |
}{ |
| 149 |
{ |
| 150 |
name: "Trim two elements from end", |
| 151 |
input: []int{1, 2, 3, 4, 5}, |
| 152 |
trimLen: 2, |
| 153 |
expected: []int{1, 2, 3}, |
| 154 |
}, |
| 155 |
{ |
| 156 |
name: "Trim entire slice", |
| 157 |
input: []int{1, 2, 3}, |
| 158 |
trimLen: 3, |
| 159 |
expected: []int{}, |
| 160 |
}, |
| 161 |
{ |
| 162 |
name: "Trim length greater than slice length", |
| 163 |
input: []int{1, 2, 3}, |
| 164 |
trimLen: 5, |
| 165 |
expected: []int{}, |
| 166 |
}, |
| 167 |
{ |
| 168 |
name: "Zero trim length", |
| 169 |
input: []int{1, 2, 3}, |
| 170 |
trimLen: 0, |
| 171 |
expected: []int{1, 2, 3}, |
| 172 |
}, |
| 173 |
{ |
| 174 |
name: "Trim one element from end", |
| 175 |
input: []int{1, 2, 3}, |
| 176 |
trimLen: 1, |
| 177 |
expected: []int{1, 2}, |
| 178 |
}, |
| 179 |
{ |
| 180 |
name: "Empty slice", |
| 181 |
input: []int{}, |
| 182 |
trimLen: 2, |
| 183 |
expected: []int{}, |
| 184 |
}, |
| 185 |
{ |
| 186 |
name: "Negative trim length (should be treated as zero)", |
| 187 |
input: []int{1, 2, 3}, |
| 188 |
trimLen: -1, |
| 189 |
expected: []int{1, 2, 3}, |
| 190 |
}, |
| 191 |
} |
| 192 |
|
| 193 |
for _, testcase := range tests { |
| 194 |
t.Run(testcase.name, func(t *testing.T) { |
| 195 |
result := RTrimSlice(testcase.input, testcase.trimLen) |
| 196 |
if !AssertEqual(result, testcase.expected) { |
| 197 |
t.Errorf("RTrimSlice(%v, %d) = %v; want %v", testcase.input, testcase.trimLen, result, testcase.expected) |
| 198 |
} |
| 199 |
}) |
| 200 |
} |
| 201 |
} |