github.com/snowflakedb/gosnowflake@v1.9.0/ctx_test.go (about)

     1  // Copyright (c) 2021-2022 Snowflake Computing Inc. All rights reserved.
     2  
     3  package gosnowflake
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"testing"
     9  )
    10  
    11  func TestCtxVal(t *testing.T) {
    12  	type favContextKey string
    13  
    14  	f := func(ctx context.Context, k favContextKey) error {
    15  		if v := ctx.Value(k); v != nil {
    16  			return nil
    17  		}
    18  		return fmt.Errorf("key not found: %v", k)
    19  	}
    20  
    21  	k := favContextKey("language")
    22  	ctx := context.WithValue(context.Background(), k, "Go")
    23  
    24  	k2 := favContextKey("data")
    25  	ctx2 := context.WithValue(ctx, k2, "Snowflake")
    26  	if err := f(ctx, k); err != nil {
    27  		t.Error(err)
    28  	}
    29  	if err := f(ctx, "color"); err == nil {
    30  		t.Error("should not have been found in context")
    31  	}
    32  
    33  	if err := f(ctx2, k); err != nil {
    34  		t.Error(err)
    35  	}
    36  	if err := f(ctx2, k2); err != nil {
    37  		t.Error(err)
    38  	}
    39  }
    40  
    41  func TestLogEntryCtx(t *testing.T) {
    42  	var log = logger
    43  	var ctx1 = context.WithValue(context.Background(), SFSessionIDKey, "sessID1")
    44  	var ctx2 = context.WithValue(context.Background(), SFSessionUserKey, "admin")
    45  
    46  	fs1 := context2Fields(ctx1)
    47  	fs2 := context2Fields(ctx2)
    48  	l1 := log.WithFields(*fs1)
    49  	l2 := log.WithFields(*fs2)
    50  	l1.Info("Hello 1")
    51  	l2.Warning("Hello 2")
    52  }