Skip to content

Commit

Permalink
Append query options to context instead of overwriting. (#860)
Browse files Browse the repository at this point in the history
* Append query options to context instead of overwriting.

* lint.

* Address feedback.

* Use asserts.
  • Loading branch information
aaron276h authored Dec 22, 2022
1 parent 52427d3 commit 69f8366
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
4 changes: 1 addition & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ func WithStdAsync(wait bool) QueryOption {
}

func Context(parent context.Context, options ...QueryOption) context.Context {
opt := QueryOptions{
settings: make(Settings),
}
opt := queryOptions(parent)
for _, f := range options {
f(&opt)
}
Expand Down
35 changes: 35 additions & 0 deletions context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package clickhouse

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestContext(t *testing.T) {
t.Run("call context multiple times making sure query options are persisted across calls",
func(t *testing.T) {
ctx := Context(context.Background(), WithQueryID("a"))
ctx = Context(ctx, WithQuotaKey("b"))
ctx = Context(ctx, WithSettings(Settings{
"c": "d",
}))

opts := queryOptions(ctx)
assert.Equal(t, "a", opts.queryID)
assert.Equal(t, "b", opts.quotaKey)
assert.Equal(t, "d", opts.settings["c"])
},
)

t.Run("call context multiple times making sure query options are persisted across calls",
func(t *testing.T) {
ctx := Context(context.Background(), WithQueryID("a"))
ctx = Context(ctx, WithQueryID("b"))

opts := queryOptions(ctx)
assert.Equal(t, "b", opts.queryID)
},
)
}

0 comments on commit 69f8366

Please sign in to comment.