forked from kisielk/sqlstruct
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sqlstruct_test.go
76 lines (63 loc) · 1.46 KB
/
sqlstruct_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
// Copyright 2012 Kamil Kisiel. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlstruct
import (
"reflect"
"testing"
)
type testType struct {
FieldA string `sql:"field_a"`
FieldB string // Not used
FieldC string `sql:"field_c"`
}
// testRows is a mock version of sql.Rows which can only scan strings
type testRows struct {
columns []string
values []interface{}
}
func (r testRows) Scan(dest ...interface{}) error {
for i := range r.values {
v := reflect.ValueOf(dest[i])
if v.Kind() != reflect.Ptr {
panic("Not a pointer!")
}
switch dest[i].(type) {
case *string:
*(dest[i].(*string)) = r.values[i].(string)
default:
// Do nothing. We assume the tests only use strings here
}
}
return nil
}
func (r testRows) Columns() ([]string, error) {
return r.columns, nil
}
func (r *testRows) addValue(c string, v interface{}) {
r.columns = append(r.columns, c)
r.values = append(r.values, v)
}
func TestColumns(t *testing.T) {
var v testType
e := "field_a, field_c"
c := Columns(v)
if c != e {
t.Errorf("expected %q got %q", e, c)
}
}
func TestScan(t *testing.T) {
rows := testRows{}
rows.addValue("field_a", "a")
rows.addValue("field_b", "b")
rows.addValue("field_c", "c")
e := testType{"a", "", "c"}
var r testType
err := Scan(&r, rows)
if err != nil {
t.Errorf("unexpected error: %s", err)
}
if r != e {
t.Errorf("expected %q got %q", e, r)
}
}