Skip to content

Commit 3c17e93

Browse files
committed
Test all queries in endtoend
1 parent 857e878 commit 3c17e93

18 files changed

Lines changed: 264 additions & 81 deletions

internal/endtoend/fmt_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
"testing"
10+
11+
pg_query "github.com/pganalyze/pg_query_go/v4"
12+
"github.com/sqlc-dev/sqlc/internal/engine/postgresql"
13+
"github.com/sqlc-dev/sqlc/internal/sql/ast"
14+
)
15+
16+
func TestFormat(t *testing.T) {
17+
t.Parallel()
18+
var queries []string
19+
err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
20+
if err != nil {
21+
return err
22+
}
23+
if !strings.Contains(path, filepath.Join("pgx/v5")) {
24+
return nil
25+
}
26+
if info.Name() == "query.sql" {
27+
queries = append(queries, path)
28+
return filepath.SkipDir
29+
}
30+
return nil
31+
})
32+
if err != nil {
33+
t.Fatal(err)
34+
}
35+
parse := postgresql.NewParser()
36+
for _, q := range queries {
37+
q := q
38+
t.Run(filepath.Dir(q), func(t *testing.T) {
39+
contents, err := os.ReadFile(q)
40+
if err != nil {
41+
t.Fatal(err)
42+
}
43+
for i, query := range bytes.Split(bytes.TrimSpace(contents), []byte(";")) {
44+
if len(query) <= 1 {
45+
continue
46+
}
47+
query := query
48+
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
49+
expected, err := pg_query.Fingerprint(string(query))
50+
if err != nil {
51+
t.Fatal(err)
52+
}
53+
stmts, err := parse.Parse(bytes.NewReader(query))
54+
if err != nil {
55+
t.Fatal(err)
56+
}
57+
if len(stmts) != 1 {
58+
t.Fatal("expected one statement")
59+
}
60+
out := ast.Format(stmts[0].Raw)
61+
actual, err := pg_query.Fingerprint(out)
62+
if err != nil {
63+
t.Error(err)
64+
}
65+
if expected != actual {
66+
t.Errorf("- %s", expected)
67+
t.Errorf("- %s", string(query))
68+
t.Errorf("+ %s", actual)
69+
t.Errorf("+ %s", out)
70+
}
71+
})
72+
}
73+
})
74+
}
75+
}

internal/engine/postgresql/print_test.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

internal/sql/ast/bool_expr.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,19 @@ type BoolExpr struct {
1010
func (n *BoolExpr) Pos() int {
1111
return n.Location
1212
}
13+
14+
func (n *BoolExpr) Format(buf *TrackedBuffer) {
15+
if n == nil {
16+
return
17+
}
18+
if items(n.Args) {
19+
switch n.Boolop {
20+
case BoolExprTypeAnd:
21+
buf.join(n.Args, " AND ")
22+
case BoolExprTypeOr:
23+
buf.join(n.Args, " OR ")
24+
case BoolExprTypeNot:
25+
buf.join(n.Args, " NOT ")
26+
}
27+
}
28+
}

internal/sql/ast/delete_stmt.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,34 @@ type DeleteStmt struct {
1212
func (n *DeleteStmt) Pos() int {
1313
return 0
1414
}
15+
16+
func (n *DeleteStmt) Format(buf *TrackedBuffer) {
17+
if n == nil {
18+
return
19+
}
20+
21+
if n.WithClause != nil {
22+
buf.astFormat(n.WithClause)
23+
buf.WriteString(" ")
24+
}
25+
26+
buf.WriteString("DELETE FROM ")
27+
if items(n.Relations) {
28+
buf.astFormat(n.Relations)
29+
}
30+
31+
if set(n.WhereClause) {
32+
buf.WriteString(" WHERE ")
33+
buf.astFormat(n.WhereClause)
34+
}
35+
36+
if set(n.LimitCount) {
37+
buf.WriteString(" LIMIT ")
38+
buf.astFormat(n.LimitCount)
39+
}
40+
41+
if items(n.ReturningList) {
42+
buf.WriteString(" RETURNING ")
43+
buf.astFormat(n.ReturningList)
44+
}
45+
}

internal/sql/ast/func_call.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,17 @@ type FuncCall struct {
1717
func (n *FuncCall) Pos() int {
1818
return n.Location
1919
}
20+
21+
func (n *FuncCall) Format(buf *TrackedBuffer) {
22+
if n == nil {
23+
return
24+
}
25+
buf.astFormat(n.Func)
26+
buf.WriteString("(")
27+
if n.AggStar {
28+
buf.WriteString("*")
29+
} else {
30+
buf.astFormat(n.Args)
31+
}
32+
buf.WriteString(")")
33+
}

internal/sql/ast/func_name.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,16 @@ type FuncName struct {
99
func (n *FuncName) Pos() int {
1010
return 0
1111
}
12+
13+
func (n *FuncName) Format(buf *TrackedBuffer) {
14+
if n == nil {
15+
return
16+
}
17+
if n.Schema != "" {
18+
buf.WriteString(n.Schema)
19+
buf.WriteString(".")
20+
}
21+
if n.Name != "" {
22+
buf.WriteString(n.Name)
23+
}
24+
}

internal/sql/ast/insert_stmt.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package ast
22

3-
import "github.com/sqlc-dev/sqlc/internal/debug"
4-
53
type InsertStmt struct {
64
Relation *RangeVar
75
Cols *List
@@ -28,7 +26,6 @@ func (n *InsertStmt) Format(buf *TrackedBuffer) {
2826

2927
buf.WriteString("INSERT INTO ")
3028
if n.Relation != nil {
31-
debug.Dump(n.Relation)
3229
buf.astFormat(n.Relation)
3330
}
3431
if items(n.Cols) {

internal/sql/ast/join_expr.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ type JoinExpr struct {
1414
func (n *JoinExpr) Pos() int {
1515
return 0
1616
}
17+
18+
func (n *JoinExpr) Format(buf *TrackedBuffer) {
19+
if n == nil {
20+
return
21+
}
22+
buf.astFormat(n.Larg)
23+
buf.WriteString(" JOIN ")
24+
buf.astFormat(n.Rarg)
25+
buf.WriteString(" ON ")
26+
buf.astFormat(n.Quals)
27+
}

internal/sql/ast/list.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,5 @@ func (n *List) Format(buf *TrackedBuffer) {
1212
if n == nil {
1313
return
1414
}
15-
for i, item := range n.Items {
16-
if i > 0 {
17-
buf.WriteRune(',')
18-
}
19-
buf.astFormat(item)
20-
}
15+
buf.join(n, ",")
2116
}

internal/sql/ast/multi_assign_ref.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,10 @@ type MultiAssignRef struct {
99
func (n *MultiAssignRef) Pos() int {
1010
return 0
1111
}
12+
13+
func (n *MultiAssignRef) Format(buf *TrackedBuffer) {
14+
if n == nil {
15+
return
16+
}
17+
buf.astFormat(n.Source)
18+
}

0 commit comments

Comments
 (0)