generated from bool64/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtable_test.go
65 lines (56 loc) · 1.18 KB
/
table_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
package dbsteps_test
import (
"testing"
"time"
"github.com/godogx/dbsteps"
"github.com/stretchr/testify/assert"
"github.com/swaggest/form/v5"
)
func TestMapper_SliceFromTable(t *testing.T) {
type Emb struct {
B string `db:"b"`
Map map[string]int `db:"m"`
}
type item struct {
A int `db:"a"`
Emb
}
data := [][]string{
{"a", "b"},
{"1", "b1"},
{"2", "b2"},
}
m := &dbsteps.TableMapper{
Decoder: form.NewDecoder(),
}
m.Decoder.SetTagName("db")
res, err := m.SliceFromTable(data, new(item))
assert.NoError(t, err)
result, ok := res.([]item)
assert.True(t, ok)
assert.Len(t, result, 2)
assert.Equal(t, 1, result[0].A)
assert.Equal(t, "b1", result[0].B)
assert.Equal(t, 2, result[1].A)
assert.Equal(t, "b2", result[1].B)
}
func TestTableMapper_Encode(t *testing.T) {
tm := dbsteps.TableMapper{}
for _, tc := range []struct {
v interface{}
s string
}{
{"abc", "abc"},
{123, "123"},
{123.45, "123.45"},
{nil, "NULL"},
{(*time.Time)(nil), "NULL"},
{time.Time{}, "0001-01-01T00:00:00Z"},
{&time.Time{}, "0001-01-01T00:00:00Z"},
{new(int), "0"},
} {
s, err := tm.Encode(tc.v)
assert.NoError(t, err)
assert.Equal(t, tc.s, s)
}
}