-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathview_test.go
174 lines (163 loc) · 4.33 KB
/
view_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package tblfmt
import (
"bytes"
"fmt"
"reflect"
"regexp"
"strconv"
"strings"
"testing"
// _ "github.com/lib/pq"
"github.com/xo/tblfmt/internal"
"github.com/xo/tblfmt/testdata"
)
func TestNewCrosstabView(t *testing.T) {
tests := loadViewTests(t, "crosstab")
for i, test := range tests {
view, err := NewCrosstabView(test.Rset(), WithParams(test.params...))
if err != nil {
t.Fatalf("test %d expected no error, got: %v", i, err)
}
checkView(t, i, test, view)
}
}
/*
func TestNewCrosstabView_psql(t *testing.T) {
db, err := sql.Open("postgres", "postgres://postgres:P4ssw0rd@localhost")
if err != nil {
t.Fatal(err)
}
tests := loadViewTests(t, "crosstab")
for i, test := range tests {
resultSet, err := db.Query(test.PsqlQuery())
if err != nil {
t.Fatal(err)
}
view, err := NewCrosstabView(resultSet, WithParams(test.params...))
if err != nil {
t.Fatalf("test %d expected no error, got: %v", i, err)
}
checkView(t, i, test, view)
}
}
*/
func checkView(t *testing.T, testNum int, test viewTest, view ResultSet) {
cols, err := view.Columns()
if err != nil {
t.Fatalf("test %d expected no error, got: %v", testNum, err)
}
if !reflect.DeepEqual(cols, test.expCols) {
t.Errorf("test %d expected columns to be %v, got: %v", testNum, test.expCols, cols)
}
clen := len(cols)
var vals [][]interface{}
for view.Next() {
r := make([]interface{}, clen)
for i := 0; i < clen; i++ {
r[i] = new(interface{})
}
if err := view.Scan(r...); err != nil {
t.Fatalf("test %d expected no error, got: %v", testNum, err)
}
vals = append(vals, r)
}
if err := view.Err(); err != nil {
t.Fatalf("test %d expected no error, got: %v", testNum, err)
}
// log.Printf(">>> test.expVals: %+v", test.expVals)
if len(vals) != len(test.expVals) {
t.Fatalf("test %d expected len(vals) == len(test.expVals): %d != %d", testNum, len(vals), len(test.expVals))
}
for i := 0; i < len(test.expVals); i++ {
row := make([]interface{}, len(vals[i]))
for j := 0; j < len(vals[i]); j++ {
row[j] = *(vals[i][j].(*interface{}))
}
rs := fmt.Sprintf("%v", row)
es := fmt.Sprintf("%v", test.expVals[i])
if rs != es {
t.Errorf("test %d expected row %d to match result\ngot: %v\nexp: %v", testNum, i, row, test.expVals[i])
}
}
}
func loadViewTests(t *testing.T, name string) []viewTest {
buf, err := testdata.Testdata.ReadFile(name + ".txt")
if err != nil {
t.Fatal(err)
}
var tests []viewTest
for _, b := range bytes.Split(buf, []byte("\n\n")) {
z := bytes.Split(b, []byte("--\n"))
if len(z) != 3 {
t.Fatalf("t should be 3, got: %d", len(z))
}
s := strings.Split(string(z[0]), ` \crosstabview`)
if len(s) != 2 {
t.Fatalf("s should be len 2, got: %d", len(s))
}
cols, vals, err := parseViewTest(z[1])
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
expCols, expVals, err := parseViewTest(z[2])
if err != nil {
t.Fatalf("expected no error, got: %v", err)
}
tests = append(tests, viewTest{
q: strings.TrimSpace(string(z[0])),
params: strings.Split(strings.TrimSpace(s[1]), " "),
cols: cols,
vals: vals,
expCols: expCols,
expVals: expVals,
})
}
return tests
}
type viewTest struct {
q string
params []string
cols []string
vals [][]interface{}
expCols []string
expVals [][]interface{}
}
func (test viewTest) Rset() *internal.RS {
return internal.New(test.cols, test.vals)
}
var azRE = regexp.MustCompile(`[^0-9\-]`)
func (test viewTest) PsqlQuery() string {
var vals []string
for _, v := range test.vals {
var z []string
for _, x := range v {
s := fmt.Sprintf("%v", x)
if azRE.MatchString(s) {
s = "'" + s + "'"
}
z = append(z, s)
}
vals = append(vals, "("+strings.Join(z, ",")+")")
}
return fmt.Sprintf(`select * from (values %s) as t (%s)`, strings.Join(vals, ", "), strings.Join(test.cols, ", "))
}
func parseViewTest(buf []byte) ([]string, [][]interface{}, error) {
lines := strings.Split(strings.TrimSpace(string(buf)), "\n")
cols := strings.Split(lines[0], "|")
var vals [][]interface{}
for _, line := range lines[1:] {
var row []interface{}
for _, c := range strings.Split(line, "|") {
switch i, err := strconv.Atoi(c); {
case err == nil:
row = append(row, i)
case c == "":
row = append(row, nil)
default:
row = append(row, c)
}
}
vals = append(vals, row)
}
return cols, vals, nil
}