@@ -3,10 +3,66 @@ package golang
33import (
44 "testing"
55
6+ "github.com/sqlc-dev/sqlc/internal/codegen/golang/opts"
67 "github.com/sqlc-dev/sqlc/internal/metadata"
78 "github.com/sqlc-dev/sqlc/internal/plugin"
89)
910
11+ func boolPtr (b bool ) * bool { return & b }
12+
13+ func catalogSchemaRequest () * plugin.GenerateRequest {
14+ col := func (name , typ string ) * plugin.Column {
15+ return & plugin.Column {Name : name , Type : & plugin.Identifier {Name : typ }}
16+ }
17+ table := func (schema , name string , cols ... * plugin.Column ) * plugin.Schema {
18+ return & plugin.Schema {
19+ Name : schema ,
20+ Tables : []* plugin.Table {
21+ {Rel : & plugin.Identifier {Schema : schema , Name : name }, Columns : cols },
22+ },
23+ }
24+ }
25+ return & plugin.GenerateRequest {
26+ Settings : & plugin.Settings {Engine : "postgresql" },
27+ Catalog : & plugin.Catalog {
28+ DefaultSchema : "public" ,
29+ Schemas : []* plugin.Schema {
30+ table ("pg_catalog" , "pg_class" , col ("oid" , "oid" )),
31+ table ("information_schema" , "tables" , col ("table_name" , "text" )),
32+ table ("public" , "users" , col ("id" , "int4" )),
33+ },
34+ },
35+ }
36+ }
37+
38+ func TestBuildStructs_OmitCatalogSchema (t * testing.T ) {
39+ req := catalogSchemaRequest ()
40+
41+ t .Run ("omit=true skips pg_catalog and information_schema" , func (t * testing.T ) {
42+ structs := buildStructs (req , & opts.Options {OmitCatalogSchema : boolPtr (true )})
43+ for _ , s := range structs {
44+ if s .Table != nil && (s .Table .Schema == "pg_catalog" || s .Table .Schema == "information_schema" ) {
45+ t .Errorf ("unexpected catalog struct: %s.%s" , s .Table .Schema , s .Table .Name )
46+ }
47+ }
48+ })
49+
50+ t .Run ("omit=false includes pg_catalog and information_schema" , func (t * testing.T ) {
51+ structs := buildStructs (req , & opts.Options {OmitCatalogSchema : boolPtr (false )})
52+ schemas := make (map [string ]bool )
53+ for _ , s := range structs {
54+ if s .Table != nil {
55+ schemas [s .Table .Schema ] = true
56+ }
57+ }
58+ for _ , want := range []string {"pg_catalog" , "information_schema" , "public" } {
59+ if ! schemas [want ] {
60+ t .Errorf ("expected structs for schema %q, got none" , want )
61+ }
62+ }
63+ })
64+ }
65+
1066func TestPutOutColumns_ForZeroColumns (t * testing.T ) {
1167 tests := []struct {
1268 cmd string
0 commit comments