From c5293b9705001cb8346fcf7f719e0ac1e6cb39d4 Mon Sep 17 00:00:00 2001 From: Aaron Harlap Date: Thu, 22 Dec 2022 10:38:57 -0500 Subject: [PATCH 1/4] Append query options to context instead of overwriting. --- context.go | 4 +--- context_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 context_test.go diff --git a/context.go b/context.go index 588382edf9..bb56c6e14c 100644 --- a/context.go +++ b/context.go @@ -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) } diff --git a/context_test.go b/context_test.go new file mode 100644 index 0000000000..6428fd106d --- /dev/null +++ b/context_test.go @@ -0,0 +1,28 @@ +package clickhouse + +import ( + "context" + "github.com/stretchr/testify/require" + "testing" +) + +func TestContext(t *testing.T) { + // Call context multiple times making sure query options are persisted across calls. + ctx := Context(context.Background(), WithQueryID("a")) + ctx = Context(ctx, WithQuotaKey("b")) + ctx = Context(ctx, WithSettings(Settings{ + "c": "d", + })) + + opts := queryOptions(ctx) + require.Equal(t, "a", opts.queryID) + require.Equal(t, "b", opts.quotaKey) + require.Equal(t, "d", opts.settings["c"]) + + // Call context multiple times with the same query options, making sure the latest is persisted. + ctx = Context(context.Background(), WithQueryID("a")) + ctx = Context(ctx, WithQueryID("b")) + + opts = queryOptions(ctx) + require.Equal(t, "b", opts.queryID) +} From fe6a235e73a9b2d69092d0cb24965f3ad98015c5 Mon Sep 17 00:00:00 2001 From: Aaron Harlap Date: Thu, 22 Dec 2022 11:02:40 -0500 Subject: [PATCH 2/4] lint. --- context_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/context_test.go b/context_test.go index 6428fd106d..247f479d0c 100644 --- a/context_test.go +++ b/context_test.go @@ -2,8 +2,9 @@ package clickhouse import ( "context" - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" ) func TestContext(t *testing.T) { From cef7fb740559d4c6780c6b4ad8c3ae022f7d48e0 Mon Sep 17 00:00:00 2001 From: Aaron Harlap Date: Thu, 22 Dec 2022 12:00:24 -0500 Subject: [PATCH 3/4] Address feedback. --- context_test.go | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/context_test.go b/context_test.go index 247f479d0c..4b07d33c25 100644 --- a/context_test.go +++ b/context_test.go @@ -8,22 +8,28 @@ import ( ) func TestContext(t *testing.T) { - // Call context multiple times making sure query options are persisted across calls. - ctx := Context(context.Background(), WithQueryID("a")) - ctx = Context(ctx, WithQuotaKey("b")) - ctx = Context(ctx, WithSettings(Settings{ - "c": "d", - })) + 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) - require.Equal(t, "a", opts.queryID) - require.Equal(t, "b", opts.quotaKey) - require.Equal(t, "d", opts.settings["c"]) + opts := queryOptions(ctx) + require.Equal(t, "a", opts.queryID) + require.Equal(t, "b", opts.quotaKey) + require.Equal(t, "d", opts.settings["c"]) + }, + ) - // Call context multiple times with the same query options, making sure the latest is persisted. - ctx = Context(context.Background(), WithQueryID("a")) - ctx = Context(ctx, WithQueryID("b")) + 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) - require.Equal(t, "b", opts.queryID) + opts := queryOptions(ctx) + require.Equal(t, "b", opts.queryID) + }, + ) } From 4959659da8b56f13ca477f8aafb0a3fdd9581425 Mon Sep 17 00:00:00 2001 From: Aaron Harlap Date: Thu, 22 Dec 2022 12:36:08 -0500 Subject: [PATCH 4/4] Use asserts. --- context_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/context_test.go b/context_test.go index 4b07d33c25..9e087fa47d 100644 --- a/context_test.go +++ b/context_test.go @@ -4,7 +4,7 @@ import ( "context" "testing" - "github.com/stretchr/testify/require" + "github.com/stretchr/testify/assert" ) func TestContext(t *testing.T) { @@ -17,9 +17,9 @@ func TestContext(t *testing.T) { })) opts := queryOptions(ctx) - require.Equal(t, "a", opts.queryID) - require.Equal(t, "b", opts.quotaKey) - require.Equal(t, "d", opts.settings["c"]) + assert.Equal(t, "a", opts.queryID) + assert.Equal(t, "b", opts.quotaKey) + assert.Equal(t, "d", opts.settings["c"]) }, ) @@ -29,7 +29,7 @@ func TestContext(t *testing.T) { ctx = Context(ctx, WithQueryID("b")) opts := queryOptions(ctx) - require.Equal(t, "b", opts.queryID) + assert.Equal(t, "b", opts.queryID) }, ) }