-
Notifications
You must be signed in to change notification settings - Fork 0
/
template_set_test.go
105 lines (88 loc) · 2.71 KB
/
template_set_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
package sqlnt
import (
"github.com/stretchr/testify/assert"
"testing"
)
type MySet struct {
Select NamedTemplate `SELECT *
FROM {{tableName}}
WHERE col_a = :a`
Insert NamedTemplate `sql:"INSERT INTO {{tableName}} (col_a,col_b,col_c) VALUES (:a,:b,:c)"`
Delete NamedTemplate `sql:"DELETE FROM {{tableName}} WHERE col_a = :a"`
delete NamedTemplate // unexported fields not used
}
type MySet2 struct {
MySet
}
type MySet3 struct {
MySet2
}
func TestNewTemplateSet(t *testing.T) {
ts, err := NewTemplateSet[MySet](testTokenOption)
assert.NoError(t, err)
assert.NotNil(t, ts.Select)
assert.Equal(t, "SELECT *\nFROM foo\nWHERE col_a = ?", ts.Select.Statement())
assert.NotNil(t, ts.Insert)
assert.Equal(t, "INSERT INTO foo (col_a,col_b,col_c) VALUES (?,?,?)", ts.Insert.Statement())
assert.NotNil(t, ts.Delete)
assert.Equal(t, "DELETE FROM foo WHERE col_a = ?", ts.Delete.Statement())
assert.Nil(t, ts.delete)
}
func TestNewTemplateSet_Anonymous(t *testing.T) {
ts, err := NewTemplateSet[struct {
Select NamedTemplate `SELECT *
FROM {{tableName}}
WHERE col_a = :a`
}](testTokenOption)
assert.NoError(t, err)
assert.NotNil(t, ts.Select)
assert.Equal(t, "SELECT *\nFROM foo\nWHERE col_a = ?", ts.Select.Statement())
}
func TestNewTemplateSet_Nested(t *testing.T) {
ts, err := NewTemplateSet[MySet2](testTokenOption)
assert.NoError(t, err)
assert.NotNil(t, ts.Select)
}
func TestNewTemplateSet_NestedDouble(t *testing.T) {
ts, err := NewTemplateSet[MySet3](testTokenOption)
assert.NoError(t, err)
assert.NotNil(t, ts.Select)
}
func TestNewTemplateSet_Error_NotStruct(t *testing.T) {
_, err := NewTemplateSet[string](testTokenOption)
assert.Error(t, err)
assert.Equal(t, "not a struct", err.Error())
}
type BadSet1 struct {
Select NamedTemplate // doesn't have 'sql' tag
}
func TestNewTemplateSet_Error_NoSqlTag(t *testing.T) {
_, err := NewTemplateSet[BadSet1](testTokenOption)
assert.Error(t, err)
assert.Equal(t, "field 'Select' does not have 'sql' tag", err.Error())
}
type BadSet2 struct {
Select NamedTemplate `sql:"{{unknown_token}}"`
}
func TestNewTemplateSet_Error_BadTemplate(t *testing.T) {
_, err := NewTemplateSet[BadSet2](testTokenOption)
assert.Error(t, err)
assert.Equal(t, "unknown token: unknown_token", err.Error())
}
type BadSet3 struct {
BadSet2
}
func TestNewTemplateSet_Error_NestedBadTemplate(t *testing.T) {
_, err := NewTemplateSet[BadSet3](testTokenOption)
assert.Error(t, err)
assert.Equal(t, "unknown token: unknown_token", err.Error())
}
func TestMustCreateTemplateSet(t *testing.T) {
ts := MustCreateTemplateSet[MySet](testTokenOption)
assert.NotNil(t, ts.Select)
}
func TestMustCreateTemplateSet_Panics(t *testing.T) {
assert.Panics(t, func() {
_ = MustCreateTemplateSet[BadSet1]()
})
}