github.com/blend/go-sdk@v1.20240719.1/logger/context_test.go (about)

     1  /*
     2  
     3  Copyright (c) 2024 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package logger
     9  
    10  import (
    11  	"context"
    12  	"testing"
    13  	"time"
    14  
    15  	"github.com/blend/go-sdk/assert"
    16  )
    17  
    18  func TestContextWithTimestamp(t *testing.T) {
    19  	assert := assert.New(t)
    20  
    21  	ts := time.Date(2019, 8, 16, 12, 11, 10, 9, time.UTC)
    22  	assert.Equal(ts, GetTimestamp(WithTimestamp(context.Background(), ts)))
    23  	assert.True(GetTimestamp(context.Background()).IsZero())
    24  }
    25  
    26  func TestContextWithRestricted(t *testing.T) {
    27  	assert := assert.New(t)
    28  
    29  	assert.Equal(true, GetRestricted(WithRestricted(context.Background(), true)))
    30  }
    31  
    32  func TestContextWithPath(t *testing.T) {
    33  	assert := assert.New(t)
    34  
    35  	path := []string{"one", "two"}
    36  	path2 := []string{"two", "three"}
    37  	assert.Equal(path, GetPath(WithPath(context.Background(), path...)))
    38  	assert.Equal(path, GetPath(WithPath(WithPath(context.Background(), path2...), path...)))
    39  	assert.Nil(GetPath(context.Background()))
    40  }
    41  
    42  func TestContextWithSetLabels(t *testing.T) {
    43  	assert := assert.New(t)
    44  
    45  	labels := Labels{"one": "two"}
    46  	labels2 := Labels{"two": "three"}
    47  	assert.Equal(labels, GetLabels(WithSetLabels(context.Background(), labels)))
    48  	assert.Equal(labels, GetLabels(WithSetLabels(WithSetLabels(context.Background(), labels2), labels)))
    49  	assert.NotNil(GetLabels(context.Background()))
    50  	assert.Empty(GetLabels(context.Background()))
    51  }
    52  
    53  func TestContextWithAnnotation(t *testing.T) {
    54  	assert := assert.New(t)
    55  
    56  	ctx := context.Background()
    57  
    58  	ctx = WithAnnotation(ctx, "one", "two")
    59  	expectedAnnotations := Annotations{"one": "two"}
    60  	assert.Equal(expectedAnnotations, GetAnnotations(ctx))
    61  
    62  	ctx = WithAnnotation(ctx, "two", 3)
    63  	expectedAnnotations = Annotations{
    64  		"one": "two",
    65  		"two": 3,
    66  	}
    67  	assert.Equal(expectedAnnotations, GetAnnotations(ctx))
    68  }
    69  
    70  func TestContextWithLabels_Mutating(t *testing.T) {
    71  	its := assert.New(t)
    72  
    73  	ctx := context.Background()
    74  	l0 := Labels{"one": "two", "three": "four"}
    75  	ctx0 := WithLabels(ctx, l0)
    76  	l1 := Labels{"one": "not-two", "two": "three"}
    77  	ctx1 := WithLabels(ctx0, l1)
    78  
    79  	l2 := GetLabels(ctx1)
    80  
    81  	l2["foo"] = "bar"
    82  
    83  	its.Equal("not-two", l2["one"])
    84  	its.Equal("three", l2["two"])
    85  	its.Equal("four", l2["three"])
    86  	its.Equal("bar", l2["foo"])
    87  
    88  	its.Equal("two", l0["one"])
    89  	its.Empty(l0["foo"])
    90  }
    91  
    92  func TestContextWithLabel_Mutating(t *testing.T) {
    93  	its := assert.New(t)
    94  
    95  	original := Labels{"four": "five"}
    96  	ctx := WithSetLabels(context.Background(), original)
    97  	its.Equal("five", GetLabels(ctx)["four"])
    98  
    99  	ctx0 := WithLabel(ctx, "one", "two")
   100  	ctx1 := WithLabel(ctx0, "three", "four")
   101  	ctx2 := WithLabel(ctx1, "four", "not-five")
   102  
   103  	l2 := GetLabels(ctx2)
   104  
   105  	its.Equal("two", l2["one"])
   106  	its.Equal("four", l2["three"])
   107  	its.Equal("not-five", l2["four"])
   108  
   109  	its.Equal("five", original["four"])
   110  }