dialect/sql: Add context.Context to sql.Selector (#1185)

This commit is contained in:
Marwan Sulaiman
2021-01-18 10:32:42 -05:00
committed by GitHub
parent 0ac3526d30
commit ddb25280cd
2 changed files with 37 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
package sql
import (
"context"
"fmt"
"strconv"
"strings"
@@ -1400,3 +1401,16 @@ func TestSelector_OrderByExpr(t *testing.T) {
require.Equal(t, "SELECT * FROM `users` WHERE `age` > ? ORDER BY `name`, CASE WHEN id=? THEN id WHEN id=? THEN name END DESC", query)
require.Equal(t, []interface{}{28, 1, 2}, args)
}
func TestBuilderContext(t *testing.T) {
type key string
want := "myval"
ctx := context.WithValue(context.Background(), key("mykey"), want)
sel := Dialect(dialect.Postgres).Select().WithContext(ctx)
if got := sel.Context().Value(key("mykey")).(string); got != want {
t.Fatalf("expected selector context key to be %q but got %q", want, got)
}
if got := sel.Clone().Context().Value(key("mykey")).(string); got != want {
t.Fatalf("expected cloned selector context key to be %q but got %q", want, got)
}
}