-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.go
112 lines (104 loc) · 3.23 KB
/
parser_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
111
112
package exql_test
import (
"fmt"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"github.com/loilo-inc/exql/v2"
"github.com/stretchr/testify/assert"
)
func TestParser_ParseTable(t *testing.T) {
t.Run("should return error when rows.Error() return error", func(t *testing.T) {
mockDb, mock, err := sqlmock.New()
assert.NoError(t, err)
defer mockDb.Close()
p := exql.NewParser()
mock.ExpectQuery(`show columns from users`).WillReturnRows(
sqlmock.NewRows([]string{"field", "type"}).
AddRow("id", "int(11)").
RowError(0, fmt.Errorf("err")))
table, err := p.ParseTable(mockDb, "users")
assert.Nil(t, table)
assert.EqualError(t, err, "err")
})
}
func TestParser_ParseType(t *testing.T) {
assertType := func(s string, nullable bool, tp interface{}) {
ret, err := exql.ParseType(s, nullable)
assert.NoError(t, err)
assert.Equal(t, ret, tp)
}
t.Run("int", func(t *testing.T) {
list := [][]interface{}{
{"int", "int64", "int64", "null.Int64", "null.Int64"},
{"tinyint", "int64", "int64", "null.Int64", "null.Int64"},
{"smallint", "int64", "int64", "null.Int64", "null.Int64"},
{"mediumint", "int64", "int64", "null.Int64", "null.Int64"},
{"bigint", "int64", "uint64", "null.Int64", "null.Uint64"},
}
for _, v := range list {
title := v[0].(string)
t.Run(title, func(t *testing.T) {
assertType(fmt.Sprintf("%s(1)", title), false, v[1])
assertType(fmt.Sprintf("%s(1) unsigned", title), false, v[2])
assertType(fmt.Sprintf("%s(1)", title), true, v[3])
assertType(fmt.Sprintf("%s(1) unsigned", title), true, v[4])
})
}
})
t.Run("float", func(t *testing.T) {
assertType("float", false, "float32")
assertType("float", true, "null.Float32")
})
t.Run("double", func(t *testing.T) {
assertType("double", false, "float64")
assertType("double", true, "null.Float64")
})
t.Run("date", func(t *testing.T) {
list := [][]interface{}{
{"date", "time.Time", "null.Time"},
{"datetime", "time.Time", "null.Time"},
{"datetime(6)", "time.Time", "null.Time"},
{"timestamp", "time.Time", "null.Time"},
{"timestamp(6)", "time.Time", "null.Time"},
{"time", "string", "null.String"},
{"time(6)", "string", "null.String"},
}
for _, v := range list {
t := v[0].(string)
assertType(t, false, v[1].(string))
assertType(t, true, v[2].(string))
}
})
t.Run("string", func(t *testing.T) {
list := [][]interface{}{
{"text", "string", "null.String"},
{"tinytext", "string", "null.String"},
{"mediumtext", "string", "null.String"},
{"longtext", "string", "null.String"},
{"char(10)", "string", "null.String"},
{"varchar(255)", "string", "null.String"},
}
for _, v := range list {
t := v[0].(string)
assertType(t, false, v[1].(string))
assertType(t, true, v[2].(string))
}
})
t.Run("blob", func(t *testing.T) {
list := [][]interface{}{
{"blob", "[]byte", "null.Bytes"},
{"tinyblob", "[]byte", "null.Bytes"},
{"mediumblob", "[]byte", "null.Bytes"},
{"longblob", "[]byte", "null.Bytes"},
}
for _, v := range list {
t := v[0].(string)
assertType(t, false, v[1].(string))
assertType(t, true, v[2].(string))
}
})
t.Run("json", func(t *testing.T) {
assertType("json", false, "json.RawMessage")
assertType("json", true, "null.JSON")
})
}