-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathresult_test.go
More file actions
143 lines (133 loc) · 3.43 KB
/
Copy pathresult_test.go
File metadata and controls
143 lines (133 loc) · 3.43 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package golang
import (
"testing"
"github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
"github.com/sqlc-dev/sqlc/internal/metadata"
"github.com/sqlc-dev/sqlc/internal/plugin"
)
func boolPtr(b bool) *bool { return &b }
func catalogSchemaRequest() *plugin.GenerateRequest {
col := func(name, typ string) *plugin.Column {
return &plugin.Column{Name: name, Type: &plugin.Identifier{Name: typ}}
}
table := func(schema, name string, cols ...*plugin.Column) *plugin.Schema {
return &plugin.Schema{
Name: schema,
Tables: []*plugin.Table{
{Rel: &plugin.Identifier{Schema: schema, Name: name}, Columns: cols},
},
}
}
return &plugin.GenerateRequest{
Settings: &plugin.Settings{Engine: "postgresql"},
Catalog: &plugin.Catalog{
DefaultSchema: "public",
Schemas: []*plugin.Schema{
table("pg_catalog", "pg_class", col("oid", "oid")),
table("information_schema", "tables", col("table_name", "text")),
table("public", "users", col("id", "int4")),
},
},
}
}
func TestBuildStructs_OmitCatalogSchema(t *testing.T) {
req := catalogSchemaRequest()
t.Run("unset (default) skips pg_catalog and information_schema", func(t *testing.T) {
structs := buildStructs(req, &opts.Options{})
for _, s := range structs {
if s.Table != nil && (s.Table.Schema == "pg_catalog" || s.Table.Schema == "information_schema") {
t.Errorf("unexpected catalog struct: %s.%s", s.Table.Schema, s.Table.Name)
}
}
})
t.Run("omit=true skips pg_catalog and information_schema", func(t *testing.T) {
structs := buildStructs(req, &opts.Options{OmitCatalogSchema: boolPtr(true)})
for _, s := range structs {
if s.Table != nil && (s.Table.Schema == "pg_catalog" || s.Table.Schema == "information_schema") {
t.Errorf("unexpected catalog struct: %s.%s", s.Table.Schema, s.Table.Name)
}
}
})
t.Run("omit=false includes pg_catalog and information_schema", func(t *testing.T) {
structs := buildStructs(req, &opts.Options{OmitCatalogSchema: boolPtr(false)})
schemas := make(map[string]bool)
for _, s := range structs {
if s.Table != nil {
schemas[s.Table.Schema] = true
}
}
for _, want := range []string{"pg_catalog", "information_schema", "public"} {
if !schemas[want] {
t.Errorf("expected structs for schema %q, got none", want)
}
}
})
}
func TestPutOutColumns_ForZeroColumns(t *testing.T) {
tests := []struct {
cmd string
want bool
}{
{
cmd: metadata.CmdExec,
want: false,
},
{
cmd: metadata.CmdExecResult,
want: false,
},
{
cmd: metadata.CmdExecRows,
want: false,
},
{
cmd: metadata.CmdExecLastId,
want: false,
},
{
cmd: metadata.CmdMany,
want: true,
},
{
cmd: metadata.CmdOne,
want: true,
},
{
cmd: metadata.CmdCopyFrom,
want: false,
},
{
cmd: metadata.CmdBatchExec,
want: false,
},
{
cmd: metadata.CmdBatchMany,
want: true,
},
{
cmd: metadata.CmdBatchOne,
want: true,
},
}
for _, tc := range tests {
t.Run(tc.cmd, func(t *testing.T) {
query := &plugin.Query{
Cmd: tc.cmd,
Columns: []*plugin.Column{},
}
got := putOutColumns(query)
if got != tc.want {
t.Errorf("putOutColumns failed. want %v, got %v", tc.want, got)
}
})
}
}
func TestPutOutColumns_AlwaysTrueWhenQueryHasColumns(t *testing.T) {
query := &plugin.Query{
Cmd: metadata.CmdMany,
Columns: []*plugin.Column{{}},
}
if putOutColumns(query) != true {
t.Error("should be true when we have columns")
}
}