...

Source file src/go.mongodb.org/mongo-driver/internal/logger/context_test.go

Documentation: go.mongodb.org/mongo-driver/internal/logger

     1  // Copyright (C) MongoDB, Inc. 2023-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package logger_test
     8  
     9  import (
    10  	"context"
    11  	"testing"
    12  
    13  	"go.mongodb.org/mongo-driver/internal/assert"
    14  	"go.mongodb.org/mongo-driver/internal/logger"
    15  )
    16  
    17  func TestContext_WithOperationName(t *testing.T) {
    18  	t.Parallel()
    19  
    20  	tests := []struct {
    21  		name   string
    22  		ctx    context.Context
    23  		opName string
    24  		ok     bool
    25  	}{
    26  		{
    27  			name:   "simple",
    28  			ctx:    context.Background(),
    29  			opName: "foo",
    30  			ok:     true,
    31  		},
    32  	}
    33  
    34  	for _, tt := range tests {
    35  		tt := tt // Capture the range variable.
    36  
    37  		t.Run(tt.name, func(t *testing.T) {
    38  			t.Parallel()
    39  
    40  			ctx := logger.WithOperationName(tt.ctx, tt.opName)
    41  
    42  			opName, ok := logger.OperationName(ctx)
    43  			assert.Equal(t, tt.ok, ok)
    44  
    45  			if ok {
    46  				assert.Equal(t, tt.opName, opName)
    47  			}
    48  		})
    49  	}
    50  }
    51  
    52  func TestContext_OperationName(t *testing.T) {
    53  	t.Parallel()
    54  
    55  	tests := []struct {
    56  		name   string
    57  		ctx    context.Context
    58  		opName interface{}
    59  		ok     bool
    60  	}{
    61  		{
    62  			name:   "nil",
    63  			ctx:    context.Background(),
    64  			opName: nil,
    65  			ok:     false,
    66  		},
    67  		{
    68  			name:   "string type",
    69  			ctx:    context.Background(),
    70  			opName: "foo",
    71  			ok:     true,
    72  		},
    73  		{
    74  			name:   "non-string type",
    75  			ctx:    context.Background(),
    76  			opName: int32(1),
    77  			ok:     false,
    78  		},
    79  	}
    80  
    81  	for _, tt := range tests {
    82  		tt := tt // Capture the range variable.
    83  
    84  		t.Run(tt.name, func(t *testing.T) {
    85  			t.Parallel()
    86  
    87  			ctx := context.Background()
    88  
    89  			if opNameStr, ok := tt.opName.(string); ok {
    90  				ctx = logger.WithOperationName(tt.ctx, opNameStr)
    91  			}
    92  
    93  			opName, ok := logger.OperationName(ctx)
    94  			assert.Equal(t, tt.ok, ok)
    95  
    96  			if ok {
    97  				assert.Equal(t, tt.opName, opName)
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func TestContext_WithOperationID(t *testing.T) {
   104  	t.Parallel()
   105  
   106  	tests := []struct {
   107  		name string
   108  		ctx  context.Context
   109  		opID int32
   110  		ok   bool
   111  	}{
   112  		{
   113  			name: "non-zero",
   114  			ctx:  context.Background(),
   115  			opID: 1,
   116  			ok:   true,
   117  		},
   118  	}
   119  
   120  	for _, tt := range tests {
   121  		tt := tt // Capture the range variable.
   122  
   123  		t.Run(tt.name, func(t *testing.T) {
   124  			t.Parallel()
   125  
   126  			ctx := logger.WithOperationID(tt.ctx, tt.opID)
   127  
   128  			opID, ok := logger.OperationID(ctx)
   129  			assert.Equal(t, tt.ok, ok)
   130  
   131  			if ok {
   132  				assert.Equal(t, tt.opID, opID)
   133  			}
   134  		})
   135  	}
   136  }
   137  
   138  func TestContext_OperationID(t *testing.T) {
   139  	t.Parallel()
   140  
   141  	tests := []struct {
   142  		name string
   143  		ctx  context.Context
   144  		opID interface{}
   145  		ok   bool
   146  	}{
   147  		{
   148  			name: "nil",
   149  			ctx:  context.Background(),
   150  			opID: nil,
   151  			ok:   false,
   152  		},
   153  		{
   154  			name: "i32 type",
   155  			ctx:  context.Background(),
   156  			opID: int32(1),
   157  			ok:   true,
   158  		},
   159  		{
   160  			name: "non-i32 type",
   161  			ctx:  context.Background(),
   162  			opID: "foo",
   163  			ok:   false,
   164  		},
   165  	}
   166  
   167  	for _, tt := range tests {
   168  		tt := tt // Capture the range variable.
   169  
   170  		t.Run(tt.name, func(t *testing.T) {
   171  			t.Parallel()
   172  
   173  			ctx := context.Background()
   174  
   175  			if opIDI32, ok := tt.opID.(int32); ok {
   176  				ctx = logger.WithOperationID(tt.ctx, opIDI32)
   177  			}
   178  
   179  			opName, ok := logger.OperationID(ctx)
   180  			assert.Equal(t, tt.ok, ok)
   181  
   182  			if ok {
   183  				assert.Equal(t, tt.opID, opName)
   184  			}
   185  		})
   186  	}
   187  }
   188  

View as plain text