-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathenrich_queryid_test.go
More file actions
47 lines (41 loc) · 1.73 KB
/
Copy pathenrich_queryid_test.go
File metadata and controls
47 lines (41 loc) · 1.73 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
package dbsql
import (
"context"
"testing"
"github.com/databricks/databricks-sql-go/driverctx"
"github.com/stretchr/testify/assert"
)
// TestEnrichQueryId pins the two non-obvious queryId-enrichment semantics:
// (1) a caller-set queryId is never overwritten, and (2) when the context has
// no queryId, the derived id is always applied via NewContextWithQueryId —
// even when empty — so a registered QueryIdCallback fires.
func TestEnrichQueryId(t *testing.T) {
t.Run("preserves a caller-set queryId", func(t *testing.T) {
ctx := driverctx.NewContextWithQueryId(context.Background(), "caller-set")
ctx = enrichQueryId(ctx, "statement-guid")
assert.Equal(t, "caller-set", driverctx.QueryIdFromContext(ctx),
"a queryId the caller already set must not be overwritten")
})
t.Run("fills the statement id when the context has none", func(t *testing.T) {
ctx := enrichQueryId(context.Background(), "statement-guid")
assert.Equal(t, "statement-guid", driverctx.QueryIdFromContext(ctx))
})
t.Run("fires the QueryIdCallback even with an empty derived id", func(t *testing.T) {
var fired bool
var got string
ctx := driverctx.NewContextWithQueryIdCallback(context.Background(), func(id string) {
fired = true
got = id
})
// No handle -> empty statement id. The callback must still fire.
_ = enrichQueryId(ctx, "")
assert.True(t, fired, "QueryIdCallback must fire on the no-queryId path even with an empty id")
assert.Equal(t, "", got)
})
t.Run("fires the QueryIdCallback with the derived id", func(t *testing.T) {
var got string
ctx := driverctx.NewContextWithQueryIdCallback(context.Background(), func(id string) { got = id })
_ = enrichQueryId(ctx, "statement-guid")
assert.Equal(t, "statement-guid", got)
})
}