This repository has been archived by the owner on Feb 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
/
integration_test.go
66 lines (54 loc) · 1.54 KB
/
integration_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
package sqlfmt_test
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/jackc/sqlfmt"
)
func TestIntegration(t *testing.T) {
// TODO - put these notes in testdata input files when sqlfmt handles comments
// {inputFile: "b_expr.sql"}, // b_expr is duplicated subset of a_expr -- test its clauses
// {inputFile: "in.sql"}, // TODO - fix formatting when spacing / new line is improved
fileInfos, err := ioutil.ReadDir("testdata")
if err != nil {
t.Fatal(err)
}
for _, fi := range fileInfos {
if fi.Name()[len(fi.Name())-10:] != ".input.sql" {
continue
}
testName := fi.Name()[:len(fi.Name())-10]
inputPath := filepath.Join("testdata", fi.Name())
goldenPath := filepath.Join("testdata", testName+".golden.sql")
input, err := ioutil.ReadFile(inputPath)
if err != nil {
t.Errorf("%s: %v", testName, err)
continue
}
expected, err := ioutil.ReadFile(goldenPath)
if err != nil {
t.Errorf("%s: %v", testName, err)
continue
}
lexer := sqlfmt.NewSqlLexer(string(input))
stmt, err := sqlfmt.Parse(lexer)
if err != nil {
t.Errorf("%s: Given %s, %v", testName, inputPath, err)
continue
}
var outBuf bytes.Buffer
r := sqlfmt.NewTextRenderer(&outBuf)
stmt.RenderTo(r)
if outBuf.String() != string(expected) {
actualFileName := filepath.Join("tmp", fmt.Sprintf("%s.sql", testName))
err = ioutil.WriteFile(actualFileName, outBuf.Bytes(), os.ModePerm)
if err != nil {
t.Fatal(err)
}
t.Errorf("%s: Unexpected output written to %s", testName, actualFileName)
}
}
}