Skip to content

Commit f91b313

Browse files
committed
Update unit tests in parity with the existing code
1 parent 17cd0b1 commit f91b313

3 files changed

Lines changed: 198 additions & 85 deletions

File tree

go/database/sql/connection.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ func (conn *sqlCommenterConn) withComment(ctx context.Context, query string) str
130130
conn.options.Tags.Application = bi.Path
131131
}
132132
}
133-
commentsMap[core.Application] = conn.options.Tags.Application
133+
if conn.options.Tags.Application != "" {
134+
commentsMap[core.Application] = conn.options.Tags.Application
135+
}
134136
}
135137

136138
var commentsString string = ""

go/database/sql/connection_test.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package sql
2+
3+
import (
4+
"testing"
5+
"context"
6+
"github.com/google/sqlcommenter/go/core"
7+
)
8+
9+
func TestWithComment_NoContext(t *testing.T) {
10+
testBasicConn := &mockConn{}
11+
testCases := []struct {
12+
desc string
13+
commenterOptions core.CommenterOptions
14+
query string
15+
wantQuery string
16+
} {
17+
{
18+
desc: "empty commenter options",
19+
commenterOptions: core.CommenterOptions{},
20+
query: "SELECT 1;",
21+
wantQuery: "SELECT 1;",
22+
},
23+
{
24+
desc: "only enable DBDriver",
25+
commenterOptions: core.CommenterOptions{
26+
Config: core.CommenterConfig{EnableDBDriver: true},
27+
},
28+
query: "SELECT 1;",
29+
wantQuery: "SELECT 1/*db_driver=database%2Fsql%3A*/;",
30+
},
31+
{
32+
desc: "enable DBDriver and pass static tag driver name",
33+
commenterOptions: core.CommenterOptions{
34+
Config: core.CommenterConfig{EnableDBDriver: true},
35+
Tags: core.StaticTags{DriverName: "postgres"},
36+
},
37+
query: "SELECT 1;",
38+
wantQuery: "SELECT 1/*db_driver=database%2Fsql%3Apostgres*/;",
39+
},
40+
{
41+
desc: "enable DBDriver and pass all static tags",
42+
commenterOptions: core.CommenterOptions{
43+
Config: core.CommenterConfig{EnableDBDriver: true},
44+
Tags: core.StaticTags{DriverName: "postgres", Application: "app-1"},
45+
},
46+
query: "SELECT 1;",
47+
wantQuery: "SELECT 1/*db_driver=database%2Fsql%3Apostgres*/;",
48+
},
49+
{
50+
desc: "enable other tags and pass all static tags",
51+
commenterOptions: core.CommenterOptions{
52+
Config: core.CommenterConfig{EnableDBDriver: true, EnableApplication: true, EnableFramework: true},
53+
Tags: core.StaticTags{DriverName: "postgres", Application: "app-1"},
54+
},
55+
query: "SELECT 1;",
56+
wantQuery: "SELECT 1/*application=app-1,db_driver=database%2Fsql%3Apostgres*/;",
57+
},
58+
}
59+
for _, tc := range testCases {
60+
testConn := newSQLCommenterConn(testBasicConn, tc.commenterOptions)
61+
ctx := context.Background()
62+
if got, want := testConn.withComment(ctx, tc.query), tc.wantQuery; got != want {
63+
t.Errorf("testConn.withComment(ctx, %q) = %q, want = %q", tc.query, got, want)
64+
}
65+
}
66+
}
67+
68+
func TestWithComment_WithContext(t *testing.T) {
69+
testBasicConn := &mockConn{}
70+
testCases := []struct {
71+
desc string
72+
commenterOptions core.CommenterOptions
73+
ctx context.Context
74+
query string
75+
wantQuery string
76+
} {
77+
{
78+
desc: "empty commenter options",
79+
commenterOptions: core.CommenterOptions{},
80+
ctx: getContextWithKeyValue(
81+
map[string]string {
82+
"route": "listData",
83+
"framework": "custom-golang",
84+
},
85+
),
86+
query: "SELECT 1;",
87+
wantQuery: "SELECT 1;",
88+
},
89+
{
90+
desc: "only all options but context has few tags",
91+
commenterOptions: core.CommenterOptions{
92+
Config: core.CommenterConfig{
93+
EnableDBDriver: true,
94+
EnableRoute: true,
95+
EnableFramework: true,
96+
EnableController: true,
97+
EnableAction: true,
98+
EnableTraceparent: true,
99+
EnableApplication: true,
100+
},
101+
Tags: core.StaticTags{DriverName: "postgres", Application: "app-1"},
102+
},
103+
ctx: getContextWithKeyValue(
104+
map[string]string {
105+
"route": "listData",
106+
"framework": "custom-golang",
107+
},
108+
),
109+
query: "SELECT 1;",
110+
wantQuery: "SELECT 1/*application=app-1,db_driver=database%2Fsql%3Apostgres,framework=custom-golang,route=listData*/;",
111+
},
112+
{
113+
desc: "only all options but context contains all tags",
114+
commenterOptions: core.CommenterOptions{
115+
Config: core.CommenterConfig{
116+
EnableDBDriver: true,
117+
EnableRoute: true,
118+
EnableFramework: true,
119+
EnableController: true,
120+
EnableAction: true,
121+
EnableTraceparent: true,
122+
EnableApplication: true,
123+
},
124+
Tags: core.StaticTags{DriverName: "postgres", Application: "app-1"},
125+
},
126+
ctx: getContextWithKeyValue(
127+
map[string]string {
128+
"route": "listData",
129+
"framework": "custom-golang",
130+
"controller": "custom-controller",
131+
"action": "any action",
132+
},
133+
),
134+
query: "SELECT 1;",
135+
wantQuery: "SELECT 1/*action=any+action,application=app-1,db_driver=database%2Fsql%3Apostgres,framework=custom-golang,route=listData*/;",
136+
},
137+
}
138+
for _, tc := range testCases {
139+
testConn := newSQLCommenterConn(testBasicConn, tc.commenterOptions)
140+
if got, want := testConn.withComment(tc.ctx, tc.query), tc.wantQuery; got != want {
141+
t.Errorf("testConn.withComment(ctx, %q) = %q, want = %q", tc.query, got, want)
142+
}
143+
}
144+
}
145+
146+
func getContextWithKeyValue(vals map[string]string) context.Context {
147+
ctx := context.Background()
148+
for k, v := range vals {
149+
ctx = context.WithValue(ctx, k, v)
150+
}
151+
return ctx
152+
}

go/database/sql/gosql_test.go

Lines changed: 43 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -14,88 +14,47 @@
1414

1515
package sql
1616

17-
// import (
18-
// "context"
19-
// "net/http"
20-
// "regexp"
21-
// "testing"
17+
import (
18+
"database/sql/driver"
19+
)
20+
21+
type mockConn struct {
22+
prepareStmt driver.Stmt
23+
prepareErr error
24+
25+
closeErr error
26+
27+
beginTx driver.Tx
28+
beginErr error
29+
}
30+
31+
func (c *mockConn) Prepare(query string) (driver.Stmt, error) {
32+
if c.prepareErr != nil {
33+
return nil, c.prepareErr
34+
}
35+
return c.prepareStmt, nil
36+
}
37+
38+
func (c *mockConn) Close() error {
39+
return c.closeErr
40+
}
41+
42+
func (c *mockConn) Begin() (driver.Tx, error) {
43+
if c.beginErr != nil {
44+
return nil, c.beginErr
45+
}
46+
return c.beginTx, nil
47+
}
48+
49+
type mockDriver struct {
50+
conn driver.Conn
51+
openError error
52+
}
53+
54+
func (d *mockDriver) Open(name string) (driver.Conn, error) {
55+
if d.openError != nil {
56+
return nil, d.openError
57+
}
58+
return d.conn, nil
59+
}
2260

23-
// "github.com/DATA-DOG/go-sqlmock"
24-
// "github.com/google/sqlcommenter/go/core"
25-
// httpnet "github.com/google/sqlcommenter/go/net/http"
26-
// "go.opentelemetry.io/otel/exporters/stdout/stdouttrace"
27-
// sdktrace "go.opentelemetry.io/otel/sdk/trace"
28-
// )
29-
30-
// func TestDisabled(t *testing.T) {
31-
// mockDB, _, err := sqlmock.New()
32-
// if err != nil {
33-
// t.Fatalf("MockSQL failed with unexpected error: %s", err)
34-
// }
35-
// db := DB{DB: mockDB, driverName: "mocksql", options: core.CommenterOptions{}}
36-
// query := "SELECT 2"
37-
// if got, want := db.withComment(context.Background(), query), query; got != want {
38-
// t.Errorf("db.withComment(context.Background(), %q) = %q, want = %q", query, got, want)
39-
// }
40-
// }
41-
42-
// func TestHTTP_Net(t *testing.T) {
43-
// mockDB, _, err := sqlmock.New()
44-
// if err != nil {
45-
// t.Fatalf("MockSQL failed with unexpected error: %s", err)
46-
// }
47-
48-
// db := DB{DB: mockDB, driverName: "mocksql", options: core.CommenterOptions{EnableDBDriver: true, EnableRoute: true, EnableFramework: true, EnableApplication: true, Application: "app"}, application: "app"}
49-
// r, err := http.NewRequest("GET", "hello/1", nil)
50-
// if err != nil {
51-
// t.Errorf("http.NewRequest('GET', 'hello/1', nil) returned unexpected error: %v", err)
52-
// }
53-
54-
// ctx := core.ContextInject(r.Context(), httpnet.NewHTTPRequestExtractor(r, nil))
55-
// got := db.withComment(ctx, "Select 1")
56-
// want := "Select 1/*application=app,db_driver=database%2Fsql%3Amocksql,framework=net%2Fhttp,route=hello%2F1*/"
57-
// if got != want {
58-
// t.Errorf("db.withComment(ctx, 'Select 1') got %q, wanted %q", got, want)
59-
// }
60-
// }
61-
62-
// func TestQueryWithSemicolon(t *testing.T) {
63-
// mockDB, _, err := sqlmock.New()
64-
// if err != nil {
65-
// t.Fatalf("MockSQL failed with unexpected error: %s", err)
66-
// }
67-
68-
// db := DB{DB: mockDB, driverName: "mocksql", options: core.CommenterOptions{EnableDBDriver: true}}
69-
// got := db.withComment(context.Background(), "Select 1;")
70-
// want := "Select 1/*db_driver=database%2Fsql%3Amocksql*/;"
71-
// if got != want {
72-
// t.Errorf("db.withComment(context.Background(), 'Select 1;') got %q, wanted %q", got, want)
73-
// }
74-
// }
75-
76-
// func TestOtelIntegration(t *testing.T) {
77-
// mockDB, _, err := sqlmock.New()
78-
// if err != nil {
79-
// t.Fatalf("MockSQL failed with unexpected error: %s", err)
80-
// }
81-
82-
// db := DB{DB: mockDB, driverName: "mocksql", options: core.CommenterOptions{EnableTraceparent: true}}
83-
// exp, _ := stdouttrace.New(stdouttrace.WithPrettyPrint())
84-
// bsp := sdktrace.NewSimpleSpanProcessor(exp) // You should use batch span processor in prod
85-
// tp := sdktrace.NewTracerProvider(
86-
// sdktrace.WithSampler(sdktrace.AlwaysSample()),
87-
// sdktrace.WithSpanProcessor(bsp),
88-
// )
89-
// ctx, _ := tp.Tracer("").Start(context.Background(), "parent-span-name")
90-
91-
// got := db.withComment(ctx, "Select 1;")
92-
// wantRegex := "Select 1/\\*traceparent=\\d{1,2}-[a-zA-Z0-9_]{32}-[a-zA-Z0-9_]{16}-\\d{1,2}\\*/;"
93-
// r, err := regexp.Compile(wantRegex)
94-
// if err != nil {
95-
// t.Errorf("regex.Compile() failed with error: %v", err)
96-
// }
97-
98-
// if !r.MatchString(got) {
99-
// t.Errorf("%q does not match the given regex %q", got, wantRegex)
100-
// }
101-
// }

0 commit comments

Comments
 (0)