|
| 1 | +package main_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "go/ast" |
| 6 | + "go/importer" |
| 7 | + "go/parser" |
| 8 | + "go/token" |
| 9 | + "go/types" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | + |
| 13 | + "github.com/sqlc-dev/plugin-sdk-go/plugin" |
| 14 | + sqlcpluginbulkgo "github.com/tomtwinkle/sqlc-plugin-bulk-go" |
| 15 | + "gotest.tools/v3/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func TestGenerate(t *testing.T) { |
| 19 | + t.Parallel() |
| 20 | + |
| 21 | + type Args struct { |
| 22 | + req *plugin.GenerateRequest |
| 23 | + } |
| 24 | + type Expected struct { |
| 25 | + err error |
| 26 | + } |
| 27 | + |
| 28 | + tests := map[string]struct { |
| 29 | + arrange func(*testing.T) (Args, Expected) |
| 30 | + }{ |
| 31 | + "valid:Normal INSERT Query": { |
| 32 | + arrange: func(t *testing.T) (Args, Expected) { |
| 33 | + req := &plugin.GenerateRequest{ |
| 34 | + SqlcVersion: "1.0.0", |
| 35 | + PluginOptions: []byte(`{"package": "sqlc"}`), |
| 36 | + Queries: []*plugin.Query{ |
| 37 | + { |
| 38 | + Name: "InsertUser", |
| 39 | + Text: "INSERT INTO users (id, name) VALUES (?, ?)", |
| 40 | + Params: []*plugin.Parameter{ |
| 41 | + {Column: &plugin.Column{Name: "id"}}, |
| 42 | + {Column: &plugin.Column{Name: "name"}}, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + } |
| 47 | + return Args{req: req}, Expected{err: nil} |
| 48 | + }, |
| 49 | + }, |
| 50 | + "valid:Extra suffix INSERT Query": { |
| 51 | + arrange: func(t *testing.T) (Args, Expected) { |
| 52 | + req := &plugin.GenerateRequest{ |
| 53 | + SqlcVersion: "1.0.0", |
| 54 | + PluginOptions: []byte(`{"package": "sqlc"}`), |
| 55 | + Queries: []*plugin.Query{ |
| 56 | + { |
| 57 | + Name: "InsertUser", |
| 58 | + Text: "INSERT INTO users (id, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE id = id", |
| 59 | + Params: []*plugin.Parameter{ |
| 60 | + {Column: &plugin.Column{Name: "id"}}, |
| 61 | + {Column: &plugin.Column{Name: "name"}}, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + } |
| 66 | + return Args{req: req}, Expected{err: nil} |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | + |
| 71 | + for name, tc := range tests { |
| 72 | + t.Run(name, func(t *testing.T) { |
| 73 | + t.Parallel() |
| 74 | + args, want := tc.arrange(t) |
| 75 | + |
| 76 | + got, err := sqlcpluginbulkgo.Generate(t.Context(), args.req) |
| 77 | + if want.err != nil { |
| 78 | + assert.ErrorContains(t, err, want.err.Error()) |
| 79 | + return |
| 80 | + } |
| 81 | + assert.NilError(t, err) |
| 82 | + assert.Assert(t, len(got.Files) > 0, "Expected at least one generated file, got none") |
| 83 | + |
| 84 | + // TODO: Need the code generated by sqlc-gen-go |
| 85 | + // assertGeneratedCodeIsValid(t, got.Files) |
| 86 | + t.Log(string(bytes.Replace(got.Files[0].Contents, []byte("\\n"), []byte("\n"), -1))) |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +func assertGeneratedCodeIsValid(t *testing.T, files []*plugin.File) { |
| 92 | + t.Helper() |
| 93 | + |
| 94 | + fset := token.NewFileSet() |
| 95 | + var parsedFiles []*ast.File |
| 96 | + |
| 97 | + for _, file := range files { |
| 98 | + if !strings.HasSuffix(file.Name, ".go") { |
| 99 | + continue |
| 100 | + } |
| 101 | + |
| 102 | + node, err := parser.ParseFile(fset, file.Name, file.Contents, parser.ParseComments) |
| 103 | + if err != nil { |
| 104 | + t.Fatalf("Failed to parse file %s: %v", file.Name, err) |
| 105 | + } |
| 106 | + parsedFiles = append(parsedFiles, node) |
| 107 | + } |
| 108 | + if len(parsedFiles) == 0 { |
| 109 | + return |
| 110 | + } |
| 111 | + conf := types.Config{ |
| 112 | + Importer: importer.Default(), |
| 113 | + Error: func(err error) { |
| 114 | + t.Fatalf("Failed to type check generated code: %v", err) |
| 115 | + }, |
| 116 | + } |
| 117 | + pkgPath := parsedFiles[0].Name.Name |
| 118 | + _, err := conf.Check(pkgPath, fset, parsedFiles, nil) |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("Type checking failed for generated code: %v", err) |
| 121 | + } |
| 122 | +} |
0 commit comments