|
| 1 | +package nrpgx5 |
| 2 | + |
| 3 | +import ( |
| 4 | + "go/token" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/dave/dst" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestCreateParseConfig(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + name string |
| 14 | + importPath string |
| 15 | + }{ |
| 16 | + {name: "pgx", importPath: PgxImportPath}, |
| 17 | + {name: "pgxpool", importPath: PgxPoolImportPath}, |
| 18 | + } |
| 19 | + |
| 20 | + for _, tt := range tests { |
| 21 | + t.Run(tt.name, func(t *testing.T) { |
| 22 | + connStr := &dst.BasicLit{Kind: token.STRING, Value: `"postgres://localhost/mydb"`} |
| 23 | + got := CreateParseConfig(connStr, tt.importPath) |
| 24 | + |
| 25 | + assert.NotNil(t, got) |
| 26 | + assert.Equal(t, token.DEFINE, got.Tok) |
| 27 | + assert.Len(t, got.Lhs, 2) |
| 28 | + |
| 29 | + configIdent, ok := got.Lhs[0].(*dst.Ident) |
| 30 | + assert.True(t, ok) |
| 31 | + assert.Equal(t, configVar, configIdent.Name) |
| 32 | + |
| 33 | + errIdent, ok := got.Lhs[1].(*dst.Ident) |
| 34 | + assert.True(t, ok) |
| 35 | + assert.Equal(t, "err", errIdent.Name) |
| 36 | + |
| 37 | + call, ok := got.Rhs[0].(*dst.CallExpr) |
| 38 | + assert.True(t, ok) |
| 39 | + assert.Len(t, call.Args, 1) |
| 40 | + |
| 41 | + funIdent, ok := call.Fun.(*dst.Ident) |
| 42 | + assert.True(t, ok) |
| 43 | + assert.Equal(t, "ParseConfig", funIdent.Name) |
| 44 | + assert.Equal(t, tt.importPath, funIdent.Path) |
| 45 | + |
| 46 | + // Connection string is cloned, not aliased — original must be untouched. |
| 47 | + arg, ok := call.Args[0].(*dst.BasicLit) |
| 48 | + assert.True(t, ok) |
| 49 | + assert.Equal(t, connStr.Value, arg.Value) |
| 50 | + assert.NotSame(t, connStr, arg) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestCreateTracerAssignment(t *testing.T) { |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + receiver dst.Expr |
| 59 | + assertOnLhs func(t *testing.T, lhs *dst.SelectorExpr) |
| 60 | + }{ |
| 61 | + { |
| 62 | + name: "direct config receiver (pgx)", |
| 63 | + receiver: dst.NewIdent(configVar), |
| 64 | + assertOnLhs: func(t *testing.T, lhs *dst.SelectorExpr) { |
| 65 | + assert.Equal(t, "Tracer", lhs.Sel.Name) |
| 66 | + ident, ok := lhs.X.(*dst.Ident) |
| 67 | + assert.True(t, ok) |
| 68 | + assert.Equal(t, configVar, ident.Name) |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "nested config.ConnConfig receiver (pgxpool)", |
| 73 | + receiver: &dst.SelectorExpr{ |
| 74 | + X: dst.NewIdent(configVar), |
| 75 | + Sel: dst.NewIdent("ConnConfig"), |
| 76 | + }, |
| 77 | + assertOnLhs: func(t *testing.T, lhs *dst.SelectorExpr) { |
| 78 | + assert.Equal(t, "Tracer", lhs.Sel.Name) |
| 79 | + inner, ok := lhs.X.(*dst.SelectorExpr) |
| 80 | + assert.True(t, ok) |
| 81 | + assert.Equal(t, "ConnConfig", inner.Sel.Name) |
| 82 | + ident, ok := inner.X.(*dst.Ident) |
| 83 | + assert.True(t, ok) |
| 84 | + assert.Equal(t, configVar, ident.Name) |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(tt.name, func(t *testing.T) { |
| 91 | + got := CreateTracerAssignment(tt.receiver) |
| 92 | + |
| 93 | + assert.NotNil(t, got) |
| 94 | + assert.Equal(t, token.ASSIGN, got.Tok) |
| 95 | + assert.Len(t, got.Lhs, 1) |
| 96 | + |
| 97 | + lhsSel, ok := got.Lhs[0].(*dst.SelectorExpr) |
| 98 | + assert.True(t, ok) |
| 99 | + tt.assertOnLhs(t, lhsSel) |
| 100 | + |
| 101 | + call, ok := got.Rhs[0].(*dst.CallExpr) |
| 102 | + assert.True(t, ok) |
| 103 | + |
| 104 | + funIdent, ok := call.Fun.(*dst.Ident) |
| 105 | + assert.True(t, ok) |
| 106 | + assert.Equal(t, "NewTracer", funIdent.Name) |
| 107 | + assert.Equal(t, Nrpgx5ImportPath, funIdent.Path) |
| 108 | + }) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestCreateConnectWithConfig(t *testing.T) { |
| 113 | + tests := []struct { |
| 114 | + name string |
| 115 | + varName string |
| 116 | + fun dst.Expr |
| 117 | + wantMethod string |
| 118 | + wantPath string |
| 119 | + }{ |
| 120 | + { |
| 121 | + name: "pgx.ConnectConfig", |
| 122 | + varName: "conn", |
| 123 | + fun: &dst.Ident{Name: "ConnectConfig", Path: PgxImportPath}, |
| 124 | + wantMethod: "ConnectConfig", |
| 125 | + wantPath: PgxImportPath, |
| 126 | + }, |
| 127 | + { |
| 128 | + name: "pgxpool.NewWithConfig", |
| 129 | + varName: "pool", |
| 130 | + fun: &dst.Ident{Name: "NewWithConfig", Path: PgxPoolImportPath}, |
| 131 | + wantMethod: "NewWithConfig", |
| 132 | + wantPath: PgxPoolImportPath, |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + for _, tt := range tests { |
| 137 | + t.Run(tt.name, func(t *testing.T) { |
| 138 | + ctxExpr := &dst.Ident{Name: "ctx"} |
| 139 | + got := CreateConnectWithConfig(tt.varName, ctxExpr, tt.fun) |
| 140 | + |
| 141 | + assert.NotNil(t, got) |
| 142 | + assert.Equal(t, token.DEFINE, got.Tok) |
| 143 | + assert.Len(t, got.Lhs, 2) |
| 144 | + |
| 145 | + lhsIdent, ok := got.Lhs[0].(*dst.Ident) |
| 146 | + assert.True(t, ok) |
| 147 | + assert.Equal(t, tt.varName, lhsIdent.Name) |
| 148 | + |
| 149 | + errIdent, ok := got.Lhs[1].(*dst.Ident) |
| 150 | + assert.True(t, ok) |
| 151 | + assert.Equal(t, "err", errIdent.Name) |
| 152 | + |
| 153 | + call, ok := got.Rhs[0].(*dst.CallExpr) |
| 154 | + assert.True(t, ok) |
| 155 | + |
| 156 | + funIdent, ok := call.Fun.(*dst.Ident) |
| 157 | + assert.True(t, ok) |
| 158 | + assert.Equal(t, tt.wantMethod, funIdent.Name) |
| 159 | + assert.Equal(t, tt.wantPath, funIdent.Path) |
| 160 | + |
| 161 | + assert.Len(t, call.Args, 2) |
| 162 | + configIdent, ok := call.Args[1].(*dst.Ident) |
| 163 | + assert.True(t, ok) |
| 164 | + assert.Equal(t, configVar, configIdent.Name) |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
0 commit comments