File: sqlstruct_test.go

package info (click to toggle)
golang-github-kisielk-sqlstruct 0.0~git20150917.0.0b86a3e-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 64 kB
  • ctags: 29
  • sloc: makefile: 2
file content (161 lines) | stat: -rw-r--r-- 3,524 bytes parent folder | download | duplicates (3)
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
// 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 EmbeddedType struct {
	FieldE string `sql:"field_e"`
}

type testType struct {
	FieldA  string `sql:"field_a"`
	FieldB  string `sql:"-"`       // Ignored
	FieldC  string `sql:"field_C"` // Different letter case
	Field_D string // Field name is used
	EmbeddedType
}

type testType2 struct {
	FieldA   string `sql:"field_a"`
	FieldSec string `sql:"field_sec"`
}

// 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, field_d, field_e"
	c := Columns(v)

	if c != e {
		t.Errorf("expected %q got %q", e, c)
	}
}

func TestColumnsAliased(t *testing.T) {
	var t1 testType
	var t2 testType2

	expected := "t1.field_a AS t1_field_a, t1.field_c AS t1_field_c, "
	expected += "t1.field_d AS t1_field_d, t1.field_e AS t1_field_e"
	actual := ColumnsAliased(t1, "t1")

	if expected != actual {
		t.Errorf("Expected %q got %q", expected, actual)
	}

	expected = "t2.field_a AS t2_field_a, t2.field_sec AS t2_field_sec"
	actual = ColumnsAliased(t2, "t2")

	if expected != actual {
		t.Errorf("Expected %q got %q", expected, actual)
	}
}

func TestScan(t *testing.T) {
	rows := testRows{}
	rows.addValue("field_a", "a")
	rows.addValue("field_b", "b")
	rows.addValue("field_c", "c")
	rows.addValue("field_d", "d")
	rows.addValue("field_e", "e")

	e := testType{"a", "", "c", "d", EmbeddedType{"e"}}

	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)
	}
}

func TestScanAliased(t *testing.T) {
	rows := testRows{}
	rows.addValue("t1_field_a", "a")
	rows.addValue("t1_field_b", "b")
	rows.addValue("t1_field_c", "c")
	rows.addValue("t1_field_d", "d")
	rows.addValue("t1_field_e", "e")
	rows.addValue("t2_field_a", "a2")
	rows.addValue("t2_field_sec", "sec")

	expected := testType{"a", "", "c", "d", EmbeddedType{"e"}}
	var actual testType
	err := ScanAliased(&actual, rows, "t1")
	if err != nil {
		t.Errorf("unexpected error: %s", err)
	}

	if expected != actual {
		t.Errorf("expected %q got %q", expected, actual)
	}

	expected2 := testType2{"a2", "sec"}
	var actual2 testType2

	err = ScanAliased(&actual2, rows, "t2")
	if err != nil {
		t.Errorf("unexpected error: %s", err)
	}

	if expected2 != actual2 {
		t.Errorf("expected %q got %q", expected2, actual2)
	}
}

func TestToSnakeCase(t *testing.T) {
	var s string
	s = ToSnakeCase("FirstName")
	if "first_name" != s {
		t.Errorf("expected first_name got %q", s)
	}

	s = ToSnakeCase("First")
	if "first" != s {
		t.Errorf("expected first got %q", s)
	}

	s = ToSnakeCase("firstName")
	if "first_name" != s {
		t.Errorf("expected first_name got %q", s)
	}
}