-
Notifications
You must be signed in to change notification settings - Fork 2
/
text_test.go
61 lines (54 loc) · 1.26 KB
/
text_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
package sol
import "testing"
func TestParamsRegex(t *testing.T) {
example := `SELECT
generate_series::date AS "Date"
FROM generate_series(
:start_date::timestamp,
:end_date::timestamp,
:interval1
)`
matches := paramsRegex.FindAllString(example, -1)
if len(matches) != 6 { // This includes false matches
t.Fatalf(
"unexpected number of matches: 6 != %d (%v)",
len(matches), matches,
)
}
}
func TestText(t *testing.T) {
expect := NewTester(t, defaultDialect{})
// Give parameters as Values
expect.SQL(
Text(
`SELECT * FROM users WHERE id > :id OR name::varchar LIKE :name`,
Values{"name": "A", "id": 2},
),
`SELECT * FROM users WHERE id > $1 OR name::varchar LIKE $2`,
2, "A",
)
expect.SQL(
Text(`SELECT * FROM users WHERE id > :id OR name = :name`).Values(
Values{"name": "A", "id": 2},
),
`SELECT * FROM users WHERE id > $1 OR name = $2`,
2, "A",
)
// Give parameters as struct types
testuser := struct {
UserID int `db:"id"`
Name string
}{
UserID: 2,
Name: "A",
}
expect.SQL(
Text(`SELECT * FROM users WHERE id > :id OR name = :name`).Values(
testuser,
),
`SELECT * FROM users WHERE id > $1 OR name = $2`,
2, "A",
)
// Missing values
expect.Error(Text(`SELECT * FROM users WHERE id > :id`))
}