-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueries_test.go
68 lines (63 loc) · 1.81 KB
/
queries_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
package queries
import (
"reflect"
"testing"
)
func TestIsReservedName(t *testing.T) {
testCases := []struct {
name string
expected bool
}{
{name: "MI", expected: true},
{name: "SS", expected: true},
{name: "not_reserved", expected: false},
{name: "another_not_reserved", expected: false},
{name: "", expected: false},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := isReservedName(tc.name)
if result != tc.expected {
t.Errorf("isReservedName(%s) = %v; expected %v", tc.name, result, tc.expected)
}
})
}
}
func TestNewQuery(t *testing.T) {
testCases := []struct {
name string
inputQuery string
expectedRaw string
expectedOrd string
expectedMap map[string]int
}{
{
name: "Test 1",
inputQuery: "SELECT * FROM users WHERE id = :id AND name = :name",
expectedRaw: "SELECT * FROM users WHERE id = :id AND name = :name",
expectedOrd: "SELECT * FROM users WHERE id = $1 AND name = $2",
expectedMap: map[string]int{"id": 1, "name": 2},
},
{
name: "Test 2",
inputQuery: "INSERT INTO users (full_name, age) VALUES (:full_name, :age)",
expectedRaw: "INSERT INTO users (full_name, age) VALUES (:full_name, :age)",
expectedOrd: "INSERT INTO users (full_name, age) VALUES ($1, $2)",
expectedMap: map[string]int{"full_name": 1, "age": 2},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
q := NewQuery(tc.inputQuery)
if q.Raw != tc.expectedRaw {
t.Errorf("Raw: got %s, expected %s", q.Raw, tc.expectedRaw)
}
if q.OrdinalQuery != tc.expectedOrd {
t.Errorf("OrdinalQuery: got %s, expected %s", q.OrdinalQuery, tc.expectedOrd)
}
if !reflect.DeepEqual(q.Mapping, tc.expectedMap) {
t.Errorf("Mapping: got %v, expected %v", q.Mapping, tc.expectedMap)
}
})
}
}