Skip to content

Commit 48f638b

Browse files
committed
test(analyzer): Update endtoend tests for new analyzer
Add a `contexts` key to exec.json to opt certain tests into or out of database-backed analysis. Fix many incorrect test cases that didn't run against an actual database.
1 parent cba2aac commit 48f638b

File tree

420 files changed

+3542
-786
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

420 files changed

+3542
-786
lines changed

examples/authors/sqlc.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ sql:
66
queries: postgresql/query.sql
77
engine: postgresql
88
database:
9+
analyzer: false
910
managed: true
1011
rules:
1112
- sqlc/db-prepare

examples/booktest/sqlc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"queries": "postgresql/query.sql",
1212
"engine": "postgresql",
1313
"database": {
14-
"managed": true
14+
"managed": true,
15+
"analyzer": false
1516
},
1617
"rules": [
1718
"sqlc/db-prepare"

examples/jets/sqlc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"queries": "postgresql/query-building.sql",
1212
"engine": "postgresql",
1313
"database": {
14+
"analyzer": false,
1415
"managed": true
1516
},
1617
"rules": [

examples/ondeck/sqlc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
"queries": "postgresql/query",
1212
"engine": "postgresql",
1313
"database": {
14-
"managed": true
14+
"managed": true,
15+
"analyzer": false
1516
},
1617
"rules": [
1718
"sqlc/db-prepare"

internal/endtoend/endtoend_test.go

Lines changed: 123 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ import (
1212

1313
"github.com/google/go-cmp/cmp"
1414
"github.com/google/go-cmp/cmp/cmpopts"
15+
"golang.org/x/exp/slices"
1516

1617
"github.com/sqlc-dev/sqlc/internal/cmd"
18+
"github.com/sqlc-dev/sqlc/internal/config"
1719
"github.com/sqlc-dev/sqlc/internal/opts"
1820
)
1921

2022
func TestExamples(t *testing.T) {
21-
t.Parallel()
23+
// t.Parallel()
2224
ctx := context.Background()
2325

2426
examples, err := filepath.Abs(filepath.Join("..", "..", "examples"))
@@ -37,10 +39,14 @@ func TestExamples(t *testing.T) {
3739
}
3840
tc := replay.Name()
3941
t.Run(tc, func(t *testing.T) {
40-
t.Parallel()
42+
// t.Parallel()
4143
path := filepath.Join(examples, tc)
4244
var stderr bytes.Buffer
43-
output, err := cmd.Generate(ctx, cmd.Env{}, path, "", &stderr)
45+
opts := &cmd.Options{
46+
Env: cmd.Env{},
47+
Stderr: &stderr,
48+
}
49+
output, err := cmd.Generate(ctx, path, "", opts)
4450
if err != nil {
4551
t.Fatalf("sqlc generate failed: %s", stderr.String())
4652
}
@@ -68,18 +74,27 @@ func BenchmarkExamples(b *testing.B) {
6874
path := filepath.Join(examples, tc)
6975
for i := 0; i < b.N; i++ {
7076
var stderr bytes.Buffer
71-
cmd.Generate(ctx, cmd.Env{}, path, "", &stderr)
77+
opts := &cmd.Options{
78+
Env: cmd.Env{},
79+
Stderr: &stderr,
80+
}
81+
cmd.Generate(ctx, path, "", opts)
7282
}
7383
})
7484
}
7585
}
7686

87+
type textContext struct {
88+
Mutate func(*config.Config)
89+
Enabled func() bool
90+
}
91+
7792
func TestReplay(t *testing.T) {
7893
// Ensure that this environment variable is always set to true when running
7994
// end-to-end tests
8095
os.Setenv("SQLC_DUMMY_VALUE", "true")
8196

82-
t.Parallel()
97+
// t.Parallel()
8398
ctx := context.Background()
8499
var dirs []string
85100
err := filepath.Walk("testdata", func(path string, info os.FileInfo, err error) error {
@@ -95,52 +110,103 @@ func TestReplay(t *testing.T) {
95110
if err != nil {
96111
t.Fatal(err)
97112
}
113+
114+
contexts := map[string]textContext{
115+
"base": {
116+
Mutate: func(c *config.Config) {},
117+
Enabled: func() bool { return true },
118+
},
119+
"managed-db": {
120+
Mutate: func(c *config.Config) {
121+
c.Cloud.Project = "01HAQMMECEYQYKFJN8MP16QC41" // TODO: Read from environment
122+
for i := range c.SQL {
123+
c.SQL[i].Database = &config.Database{
124+
Managed: true,
125+
}
126+
}
127+
},
128+
Enabled: func() bool {
129+
if len(os.Getenv("CI")) > 0 {
130+
return false
131+
}
132+
return len(os.Getenv("SQLC_AUTH_TOKEN")) > 0
133+
},
134+
},
135+
}
136+
98137
for _, replay := range dirs {
99138
tc := replay
100-
t.Run(tc, func(t *testing.T) {
101-
t.Parallel()
139+
for name, testctx := range contexts {
140+
name := name
141+
testctx := testctx
102142

103-
var stderr bytes.Buffer
104-
var output map[string]string
105-
var err error
143+
if !testctx.Enabled() {
144+
continue
145+
}
106146

107-
path, _ := filepath.Abs(tc)
108-
args := parseExec(t, path)
109-
expected := expectedStderr(t, path)
147+
t.Run(filepath.Join(name, tc), func(t *testing.T) {
148+
t.Parallel()
149+
var stderr bytes.Buffer
150+
var output map[string]string
151+
var err error
110152

111-
if args.Process != "" {
112-
_, err := osexec.LookPath(args.Process)
113-
if err != nil {
114-
t.Skipf("executable not found: %s %s", args.Process, err)
153+
path, _ := filepath.Abs(tc)
154+
args := parseExec(t, path)
155+
156+
if args.Process != "" {
157+
_, err := osexec.LookPath(args.Process)
158+
if err != nil {
159+
t.Skipf("executable not found: %s %s", args.Process, err)
160+
}
115161
}
116-
}
117162

118-
env := cmd.Env{
119-
Debug: opts.DebugFromString(args.Env["SQLCDEBUG"]),
120-
NoRemote: true,
121-
}
122-
switch args.Command {
123-
case "diff":
124-
err = cmd.Diff(ctx, env, path, "", &stderr)
125-
case "generate":
126-
output, err = cmd.Generate(ctx, env, path, "", &stderr)
127-
if err == nil {
128-
cmpDirectory(t, path, output)
163+
if len(args.Contexts) > 0 {
164+
if !slices.Contains(args.Contexts, name) {
165+
t.Skipf("unsupported context: %s", name)
166+
}
129167
}
130-
case "vet":
131-
err = cmd.Vet(ctx, env, path, "", &stderr)
132-
default:
133-
t.Fatalf("unknown command")
134-
}
135168

136-
if len(expected) == 0 && err != nil {
137-
t.Fatalf("sqlc %s failed: %s", args.Command, stderr.String())
138-
}
169+
expected := expectedStderr(t, path)
170+
opts := cmd.Options{
171+
Env: cmd.Env{
172+
Debug: opts.DebugFromString(args.Env["SQLCDEBUG"]),
173+
NoRemote: true,
174+
},
175+
Stderr: &stderr,
176+
MutateConfig: testctx.Mutate,
177+
}
139178

140-
if diff := cmp.Diff(expected, stderr.String()); diff != "" {
141-
t.Errorf("stderr differed (-want +got):\n%s", diff)
142-
}
143-
})
179+
switch args.Command {
180+
case "diff":
181+
err = cmd.Diff(ctx, path, "", &opts)
182+
case "generate":
183+
output, err = cmd.Generate(ctx, path, "", &opts)
184+
if err == nil {
185+
cmpDirectory(t, path, output)
186+
}
187+
case "vet":
188+
err = cmd.Vet(ctx, path, "", &opts)
189+
default:
190+
t.Fatalf("unknown command")
191+
}
192+
193+
if len(expected) == 0 && err != nil {
194+
t.Fatalf("sqlc %s failed: %s", args.Command, stderr.String())
195+
}
196+
197+
var diff string
198+
for _, expectedErr := range strings.Split(expected, "---\n") {
199+
diff = cmp.Diff(strings.TrimSpace(expectedErr), strings.TrimSpace(stderr.String()))
200+
if diff == "" {
201+
break
202+
}
203+
}
204+
if diff != "" {
205+
t.Log(stderr.String()) // TODO: Remove
206+
t.Fatalf("stderr differed (-want +got):\n%s", diff)
207+
}
208+
})
209+
}
144210
}
145211
}
146212

@@ -192,16 +258,13 @@ func cmpDirectory(t *testing.T, dir string, actual map[string]string) {
192258
t.Errorf("%s contents differ", dir)
193259
for name, contents := range expected {
194260
name := name
195-
tn := strings.Replace(name, dir+"/", "", -1)
196-
t.Run(tn, func(t *testing.T) {
197-
if actual[name] == "" {
198-
t.Errorf("%s is empty", name)
199-
return
200-
}
201-
if diff := cmp.Diff(contents, actual[name]); diff != "" {
202-
t.Errorf("%s differed (-want +got):\n%s", name, diff)
203-
}
204-
})
261+
if actual[name] == "" {
262+
t.Errorf("%s is empty", name)
263+
return
264+
}
265+
if diff := cmp.Diff(contents, actual[name]); diff != "" {
266+
t.Errorf("%s differed (-want +got):\n%s", name, diff)
267+
}
205268
}
206269
}
207270
}
@@ -220,9 +283,10 @@ func expectedStderr(t *testing.T, dir string) string {
220283
}
221284

222285
type exec struct {
223-
Command string `json:"command"`
224-
Process string `json:"process"`
225-
Env map[string]string `json:"env"`
286+
Command string `json:"command"`
287+
Process string `json:"process"`
288+
Contexts []string `json:"contexts"`
289+
Env map[string]string `json:"env"`
226290
}
227291

228292
func parseExec(t *testing.T, dir string) exec {
@@ -266,7 +330,11 @@ func BenchmarkReplay(b *testing.B) {
266330
path, _ := filepath.Abs(tc)
267331
for i := 0; i < b.N; i++ {
268332
var stderr bytes.Buffer
269-
cmd.Generate(ctx, cmd.Env{}, path, "", &stderr)
333+
opts := &cmd.Options{
334+
Env: cmd.Env{},
335+
Stderr: &stderr,
336+
}
337+
cmd.Generate(ctx, path, "", opts)
270338
}
271339
})
272340
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
CREATE TABLE bar (id serial not null);
2-
1+
CREATE TABLE bar (id serial not null);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["managed-db"]
3+
}

internal/endtoend/testdata/func_return/postgresql/pgx/v5/go/models.go renamed to internal/endtoend/testdata/coalesce_as/postgresql/pganalyze/go/models.go

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/coalesce_as/postgresql/pganalyze/go/query.sql.go

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- name: SumBaz :many
2+
SELECT bar, coalesce(sum(baz), 0) as quantity
3+
FROM foo
4+
GROUP BY 1;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE foo (
2+
bar text,
3+
baz bigint
4+
);
5+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"version": "1",
3+
"cloud": {
4+
"project": "01HAQMMECEYQYKFJN8MP16QC41"
5+
},
6+
"packages": [
7+
{
8+
"path": "go",
9+
"engine": "postgresql",
10+
"sql_package": "pgx/v5",
11+
"database": {
12+
"managed": true
13+
},
14+
"name": "querytest",
15+
"schema": "schema.sql",
16+
"queries": "query.sql"
17+
}
18+
]
19+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["base"]
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["base"]
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"contexts": ["base"]
3+
}

0 commit comments

Comments
 (0)