forked from shenghui0779/yiigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
110 lines (87 loc) · 2.75 KB
/
util_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package yiigo
import (
"context"
"errors"
"fmt"
"log"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestSteps(t *testing.T) {
arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20}
for index, step := range Steps(len(arr), 6) {
ids := arr[step.Head:step.Tail]
log.Printf("step[%d], slice: %d\n", index, ids)
}
}
func TestMarshalNoEscapeHTML(t *testing.T) {
data := map[string]string{"url": "https://github.com/shenghui0779/yiigo?id=996&name=yiigo"}
b, err := MarshalNoEscapeHTML(data)
assert.Nil(t, err)
assert.Equal(t, string(b), `{"url":"https://github.com/shenghui0779/yiigo?id=996&name=yiigo"}`)
}
func TestVersionCompare(t *testing.T) {
ok, err := VersionCompare("1.0.0", "1.0.0")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = VersionCompare("1.0.0", "1.0.1")
assert.Nil(t, err)
assert.False(t, ok)
ok, err = VersionCompare("=1.0.0", "1.0.0")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = VersionCompare("=1.0.0", "1.0.1")
assert.Nil(t, err)
assert.False(t, ok)
ok, err = VersionCompare("!=4.0.4", "4.0.0")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = VersionCompare("!=4.0.4", "4.0.4")
assert.Nil(t, err)
assert.False(t, ok)
ok, err = VersionCompare(">2.0.0", "2.0.1")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = VersionCompare(">2.0.0", "1.0.1")
assert.Nil(t, err)
assert.False(t, ok)
ok, err = VersionCompare(">=1.0.0&<2.0.0", "1.0.2")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = VersionCompare(">=1.0.0&<2.0.0", "2.0.1")
assert.Nil(t, err)
assert.False(t, ok)
ok, err = VersionCompare("<2.0.0|>3.0.0", "1.0.2")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = VersionCompare("<2.0.0|>3.0.0", "3.0.1")
assert.Nil(t, err)
assert.True(t, ok)
ok, err = VersionCompare("<2.0.0|>3.0.0", "2.0.1")
assert.Nil(t, err)
assert.False(t, ok)
}
func TestRetry(t *testing.T) {
now := time.Now()
err := Retry(context.Background(), func(ctx context.Context) error {
fmt.Println("Retry...")
return errors.New("something wrong")
}, 3, time.Second)
assert.NotNil(t, err)
assert.Equal(t, 2, int(time.Since(now).Seconds()))
}
func TestIsUniqueDuplicateError(t *testing.T) {
errMySQL := errors.New("Duplicate entry 'value' for key 'key_name'")
assert.True(t, IsUniqueDuplicateError(errMySQL))
errPgSQL := errors.New(`duplicate key value violates unique constraint "constraint_name"`)
assert.True(t, IsUniqueDuplicateError(errPgSQL))
errSQLite := errors.New("UNIQUE constraint failed: table_name.column_name")
assert.True(t, IsUniqueDuplicateError(errSQLite))
}
func TestExcelColumnIndex(t *testing.T) {
assert.Equal(t, 0, ExcelColumnIndex("A"))
assert.Equal(t, 1, ExcelColumnIndex("B"))
assert.Equal(t, 26, ExcelColumnIndex("AA"))
assert.Equal(t, 27, ExcelColumnIndex("AB"))
}