-
Notifications
You must be signed in to change notification settings - Fork 1
/
tmpl_update_test.go
133 lines (129 loc) · 3.95 KB
/
tmpl_update_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
package templates
import (
"github.com/google/uuid"
"reflect"
"testing"
"time"
)
func TestTmpl_UpdateQuery(t *testing.T) {
tests := []struct {
name string
testStruct interface{}
expected string
}{
{
name: "exampleStruct",
testStruct: exampleStruct{},
expected: `UPDATE example_struct SET
amount = $1,another_amount = $2,some_useful_thing = $3,updated_at = now() AT TIME ZONE 'UTC'
WHERE id=$4 returning id`,
},
{
name: "exampleStruct ref",
testStruct: &exampleStruct{},
expected: `UPDATE example_struct SET
amount = $1,another_amount = $2,some_useful_thing = $3,updated_at = now() AT TIME ZONE 'UTC'
WHERE id=$4 returning id`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tmpl, err := GetTmpl(tt.testStruct)
if err != nil {
t.Fatal(err)
}
if got, _ := tmpl.UpdateQuery(); got != tt.expected {
t.Errorf("query = %v, expected %v", got, tt.expected)
}
})
}
}
func TestTmpl_Update(t *testing.T) {
id, err := uuid.ParseBytes([]byte("342923c6-c073-42dd-b944-c7095b0576a0"))
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
testStruct interface{}
fields []string
expectedString string
expectedValues []interface{}
}{
{
name: "exampleStruct",
testStruct: exampleStruct{ID: id, Amount: 2, AnotherAmount: 3},
fields: []string{"example_struct.amount", "example_struct.another_amount"},
expectedString: `UPDATE example_struct SET
amount = $1,another_amount = $2,updated_at = now() AT TIME ZONE 'UTC'
WHERE id=$3 returning id`,
expectedValues: []interface{}{int64(2), int64(3), id},
},
{
name: "exampleStruct ref",
testStruct: &exampleStruct{ID: id, Amount: 2, AnotherAmount: 3, UpdatedAt: SetTimeDiff(time.Hour)},
fields: []string{"example_struct.amount", "example_struct.another_amount", "example_struct.updated_at"},
expectedString: `UPDATE example_struct SET
amount = $1,another_amount = $2,updated_at = now() AT TIME ZONE 'UTC' + 3600 * interval '1 second'
WHERE id=$3 returning id`,
expectedValues: []interface{}{int64(2), int64(3), id},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t1 *testing.T) {
query, values, err := Update(tt.testStruct, tt.fields...)
if query != tt.expectedString {
t1.Errorf("Update() got string = %v, expected %v", query, tt.expectedString)
}
if !reflect.DeepEqual(values, tt.expectedValues) {
t1.Errorf("Update() got values = %v, expected %v", values, tt.expectedValues)
}
if err != nil {
t.Fatal(err)
}
})
}
}
func TestTmpl_Update_All(t *testing.T) {
id, err := uuid.ParseBytes([]byte("342923c6-c073-42dd-b944-c7095b0576a0"))
if err != nil {
t.Fatal(err)
}
tests := []struct {
name string
testStruct interface{}
expectedString string
expectedValues []interface{}
}{
{
name: "exampleStruct",
testStruct: exampleStruct{ID: id, Amount: 2, SomeUsefulThing: "check"},
expectedString: `UPDATE example_struct SET
amount = $1,another_amount = $2,some_useful_thing = $3,updated_at = now() AT TIME ZONE 'UTC'
WHERE id=$4 returning id`,
expectedValues: []interface{}{int64(2), int64(0), "check", id},
},
{
name: "exampleStruct ref",
testStruct: &exampleStruct{ID: id, Amount: 2, AnotherAmount: 3, SomeUsefulThing: "check"},
expectedString: `UPDATE example_struct SET
amount = $1,another_amount = $2,some_useful_thing = $3,updated_at = now() AT TIME ZONE 'UTC'
WHERE id=$4 returning id`,
expectedValues: []interface{}{int64(2), int64(3), "check", id},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t1 *testing.T) {
query, values, err := Update(tt.testStruct)
if query != tt.expectedString {
t1.Errorf("Update() got string = %v, expected %v", query, tt.expectedString)
}
if !reflect.DeepEqual(values, tt.expectedValues) {
t1.Errorf("Update() got values = %v, expected %v", values, tt.expectedValues)
}
if err != nil {
t.Fatal(err)
}
})
}
}