This repository was archived by the owner on Jan 28, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 109
/
Copy pathrpad_lpad_test.go
109 lines (95 loc) · 3.44 KB
/
rpad_lpad_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
package function
import (
"testing"
"github.com/src-d/go-mysql-server/sql"
"github.com/src-d/go-mysql-server/sql/expression"
"github.com/stretchr/testify/require"
)
func TestLPad(t *testing.T) {
f, err := NewPad(
lPadType,
expression.NewGetField(0, sql.Text, "str", false),
expression.NewGetField(1, sql.Int64, "len", false),
expression.NewGetField(2, sql.Text, "padStr", false),
)
require.NoError(t, err)
testCases := []struct {
name string
row sql.Row
expected interface{}
err bool
}{
{"null string", sql.NewRow(nil, 1, "bar"), nil, false},
{"null len", sql.NewRow("foo", nil, "bar"), nil, false},
{"null padStr", sql.NewRow("foo", 1, nil), nil, false},
{"negative length", sql.NewRow("foo", -1, "bar"), "", false},
{"length 0", sql.NewRow("foo", 0, "bar"), "", false},
{"invalid length", sql.NewRow("foo", "a", "bar"), "", true},
{"empty padStr and len < len(str)", sql.NewRow("foo", 1, ""), "f", false},
{"empty padStr and len > len(str)", sql.NewRow("foo", 4, ""), "", false},
{"empty padStr and len == len(str)", sql.NewRow("foo", 3, ""), "foo", false},
{"non empty padStr and len < len(str)", sql.NewRow("foo", 1, "abcd"), "f", false},
{"non empty padStr and len == len(str)", sql.NewRow("foo", 3, "abcd"), "foo", false},
{"padStr repeats exactly once", sql.NewRow("foo", 6, "abc"), "abcfoo", false},
{"padStr does not repeat once", sql.NewRow("foo", 5, "abc"), "abfoo", false},
{"padStr repeats many times", sql.NewRow("foo", 10, "abc"), "abcabcafoo", false},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
t.Helper()
require := require.New(t)
ctx := sql.NewEmptyContext()
v, err := f.Eval(ctx, tt.row)
if tt.err {
require.Error(err)
} else {
require.NoError(err)
require.Equal(tt.expected, v)
}
})
}
}
func TestRPad(t *testing.T) {
f, err := NewPad(
rPadType,
expression.NewGetField(0, sql.Text, "str", false),
expression.NewGetField(1, sql.Int64, "len", false),
expression.NewGetField(2, sql.Text, "padStr", false),
)
require.NoError(t, err)
testCases := []struct {
name string
row sql.Row
expected interface{}
err bool
}{
{"null string", sql.NewRow(nil, 1, "bar"), nil, false},
{"null len", sql.NewRow("foo", nil, "bar"), nil, false},
{"null padStr", sql.NewRow("foo", 1, nil), nil, false},
{"negative length", sql.NewRow("foo", -1, "bar"), "", false},
{"length 0", sql.NewRow("foo", 0, "bar"), "", false},
{"invalid length", sql.NewRow("foo", "a", "bar"), "", true},
{"empty padStr and len < len(str)", sql.NewRow("foo", 1, ""), "f", false},
{"empty padStr and len > len(str)", sql.NewRow("foo", 4, ""), "", false},
{"empty padStr and len == len(str)", sql.NewRow("foo", 3, ""), "foo", false},
{"non empty padStr and len < len(str)", sql.NewRow("foo", 1, "abcd"), "f", false},
{"non empty padStr and len == len(str)", sql.NewRow("foo", 3, "abcd"), "foo", false},
{"padStr repeats exactly once", sql.NewRow("foo", 6, "abc"), "fooabc", false},
{"padStr does not repeat once", sql.NewRow("foo", 5, "abc"), "fooab", false},
{"padStr repeats many times", sql.NewRow("foo", 10, "abc"), "fooabcabca", false},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
t.Helper()
require := require.New(t)
ctx := sql.NewEmptyContext()
v, err := f.Eval(ctx, tt.row)
if tt.err {
require.Error(err)
} else {
require.NoError(err)
require.Equal(tt.expected, v)
}
})
}
}