forked from maddyblue/sqlfmt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlfmt_test.go
82 lines (74 loc) · 1.55 KB
/
sqlfmt_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
package sqlfmt
import (
"fmt"
"testing"
)
func TestFormatSQL(t *testing.T) {
type Test struct {
SQL string
WantSQL string
}
var testsData = []Test{
{
SQL: `SELECT count(*) count, winner, counter * (60 * 5) as counter FROM (SELECT winner, round(length / (60 * 5)) as counter FROM players WHERE build = $1 AND (hero = $2 OR region = $3)) GROUP BY winner, counter;`,
WantSQL:`SELECT
count(*) AS count, winner, counter * 60 * 5 AS counter
FROM
(
SELECT
winner, round(length / (60 * 5)) AS counter
FROM
players
WHERE
build = $1 AND (hero = $2 OR region = $3)
)
GROUP BY
winner, counter;`,
},
{
SQL:`INSERT INTO players(build, hero, region, winner, length) VALUES ($1, $2, $3, $4, $5);`,
WantSQL: `INSERT
INTO
players (build, hero, region, winner, length)
VALUES
($1, $2, $3, $4, $5);`,
},
{
SQL: `UPDATE players SET count = 0 WHERE build = $1 AND (hero = $2 OR region = $3) LIMIT 1;`,
WantSQL: `UPDATE
players
SET
count = 0
WHERE
build = $1 AND (hero = $2 OR region = $3)
LIMIT
1;`,
},
{
SQL:`SELECT * from test where a = 1`,
WantSQL:`SELECT
*
FROM
test
WHERE
a = 1;`,
},
}
for i, item := range testsData {
formatSQL, err := FormatSQL(item.SQL)
if err != nil {
t.Fatal(err)
}
if formatSQL != item.WantSQL {
//os.WriteFile(fmt.Sprintf("%d.sql",i),[]byte(formatSQL),0644)
t.Fatalf("format sql %d failed: %s",i,formatSQL)
}
}
}
func TestSimpleFormatSQL(t *testing.T) {
formatSQL, err := FormatSQL("SELECT * FROM test WHERE A >= 4")
if err != nil {
t.Fatal(err)
}
fmt.Println(formatSQL)
}