github.com/newrelic/newrelic-client-go@v1.1.0/pkg/contextkeys/contextkeys_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package contextkeys
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestGetAccountID_ReturnsAccountIDIfFound(t *testing.T) {
    14  	testCtx := context.WithValue(context.Background(), keys.accountID, "some-account-id")
    15  	accountID, ok := GetAccountID(testCtx)
    16  
    17  	assert.Equal(t, true, ok)
    18  	assert.Equal(t, "some-account-id", accountID)
    19  }
    20  
    21  func TestGetAccountID_DefaultReturnValueWhenNoAccountIDFound(t *testing.T) {
    22  	defaultValue, ok := GetAccountID(context.Background())
    23  
    24  	assert.Equal(t, false, ok)
    25  	assert.Equal(t, "", defaultValue)
    26  }
    27  
    28  func TestSetAccountID_SetsTheAccountID(t *testing.T) {
    29  	ctx := SetAccountID(context.Background(), "some-account-id")
    30  	result, ok := GetAccountID(ctx)
    31  
    32  	assert.Equal(t, true, ok)
    33  	assert.Equal(t, "some-account-id", result)
    34  }
    35  
    36  func TestSetAccountID_CreatesContextIfNil(t *testing.T) {
    37  	ctx := SetAccountID(nil, "some-account-id")
    38  	assert.NotNil(t, ctx)
    39  
    40  	result, ok := GetAccountID(ctx)
    41  	assert.Equal(t, true, ok)
    42  	assert.Equal(t, "some-account-id", result)
    43  }
    44  
    45  func TestContextKeys_SetAccountID_SetsValues(t *testing.T) {
    46  	x := contextKeys{}
    47  	ctx := x.SetAccountID(context.Background(), "some-account-id")
    48  	result, ok := x.GetAccountID(ctx)
    49  
    50  	assert.Equal(t, true, ok)
    51  	assert.Equal(t, "some-account-id", result)
    52  }
    53  
    54  func TestContextKeys_SetAccountID_CreatesContextIfNil(t *testing.T) {
    55  	x := contextKeys{}
    56  	ctx := x.SetAccountID(nil, "some-account-id")
    57  	assert.NotNil(t, ctx)
    58  
    59  	result, ok := x.GetAccountID(ctx)
    60  	assert.Equal(t, true, ok)
    61  	assert.Equal(t, "some-account-id", result)
    62  }
    63  
    64  func TestContextKeys_GetAccountID_ReturnsAccountIDIfFound(t *testing.T) {
    65  	x := contextKeys{}
    66  	testCtx := context.WithValue(context.Background(), keys.accountID, "some-account-id")
    67  	accountID, ok := x.GetAccountID(testCtx)
    68  
    69  	assert.Equal(t, true, ok)
    70  	assert.Equal(t, "some-account-id", accountID)
    71  }
    72  
    73  func TestContextKeys_GetAccountID_DefaultReturnValueWhenNoAccountIDFound(t *testing.T) {
    74  	x := contextKeys{}
    75  	defaultValue, ok := x.GetAccountID(context.Background())
    76  
    77  	assert.Equal(t, false, ok)
    78  	assert.Equal(t, "", defaultValue)
    79  }