...

Source file src/go.opentelemetry.io/otel/attribute/kv_test.go

Documentation: go.opentelemetry.io/otel/attribute

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package attribute_test
    16  
    17  import (
    18  	"testing"
    19  
    20  	"github.com/google/go-cmp/cmp"
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"go.opentelemetry.io/otel/attribute"
    24  )
    25  
    26  func TestKeyValueConstructors(t *testing.T) {
    27  	tt := []struct {
    28  		name     string
    29  		actual   attribute.KeyValue
    30  		expected attribute.KeyValue
    31  	}{
    32  		{
    33  			name:   "Bool",
    34  			actual: attribute.Bool("k1", true),
    35  			expected: attribute.KeyValue{
    36  				Key:   "k1",
    37  				Value: attribute.BoolValue(true),
    38  			},
    39  		},
    40  		{
    41  			name:   "Int64",
    42  			actual: attribute.Int64("k1", 123),
    43  			expected: attribute.KeyValue{
    44  				Key:   "k1",
    45  				Value: attribute.Int64Value(123),
    46  			},
    47  		},
    48  		{
    49  			name:   "Float64",
    50  			actual: attribute.Float64("k1", 123.5),
    51  			expected: attribute.KeyValue{
    52  				Key:   "k1",
    53  				Value: attribute.Float64Value(123.5),
    54  			},
    55  		},
    56  		{
    57  			name:   "String",
    58  			actual: attribute.String("k1", "123.5"),
    59  			expected: attribute.KeyValue{
    60  				Key:   "k1",
    61  				Value: attribute.StringValue("123.5"),
    62  			},
    63  		},
    64  		{
    65  			name:   "Int",
    66  			actual: attribute.Int("k1", 123),
    67  			expected: attribute.KeyValue{
    68  				Key:   "k1",
    69  				Value: attribute.IntValue(123),
    70  			},
    71  		},
    72  	}
    73  
    74  	for _, test := range tt {
    75  		t.Run(test.name, func(t *testing.T) {
    76  			if diff := cmp.Diff(test.actual, test.expected, cmp.AllowUnexported(attribute.Value{})); diff != "" {
    77  				t.Fatal(diff)
    78  			}
    79  		})
    80  	}
    81  }
    82  
    83  func TestKeyValueValid(t *testing.T) {
    84  	tests := []struct {
    85  		desc  string
    86  		valid bool
    87  		kv    attribute.KeyValue
    88  	}{
    89  		{
    90  			desc:  "uninitialized KeyValue should be invalid",
    91  			valid: false,
    92  			kv:    attribute.KeyValue{},
    93  		},
    94  		{
    95  			desc:  "empty key value should be invalid",
    96  			valid: false,
    97  			kv:    attribute.Key("").Bool(true),
    98  		},
    99  		{
   100  			desc:  "INVALID value type should be invalid",
   101  			valid: false,
   102  			kv: attribute.KeyValue{
   103  				Key: attribute.Key("valid key"),
   104  				// Default type is INVALID.
   105  				Value: attribute.Value{},
   106  			},
   107  		},
   108  		{
   109  			desc:  "non-empty key with BOOL type Value should be valid",
   110  			valid: true,
   111  			kv:    attribute.Bool("bool", true),
   112  		},
   113  		{
   114  			desc:  "non-empty key with INT64 type Value should be valid",
   115  			valid: true,
   116  			kv:    attribute.Int64("int64", 0),
   117  		},
   118  		{
   119  			desc:  "non-empty key with FLOAT64 type Value should be valid",
   120  			valid: true,
   121  			kv:    attribute.Float64("float64", 0),
   122  		},
   123  		{
   124  			desc:  "non-empty key with STRING type Value should be valid",
   125  			valid: true,
   126  			kv:    attribute.String("string", ""),
   127  		},
   128  	}
   129  
   130  	for _, test := range tests {
   131  		if got, want := test.kv.Valid(), test.valid; got != want {
   132  			t.Error(test.desc)
   133  		}
   134  	}
   135  }
   136  
   137  func TestIncorrectCast(t *testing.T) {
   138  	testCases := []struct {
   139  		name string
   140  		val  attribute.Value
   141  	}{
   142  		{
   143  			name: "Float64",
   144  			val:  attribute.Float64Value(1.0),
   145  		},
   146  		{
   147  			name: "Int64",
   148  			val:  attribute.Int64Value(2),
   149  		},
   150  		{
   151  			name: "String",
   152  			val:  attribute.BoolValue(true),
   153  		},
   154  		{
   155  			name: "Float64Slice",
   156  			val:  attribute.Float64SliceValue([]float64{1.0}),
   157  		},
   158  		{
   159  			name: "Int64Slice",
   160  			val:  attribute.Int64SliceValue([]int64{2}),
   161  		},
   162  		{
   163  			name: "StringSlice",
   164  			val:  attribute.BoolSliceValue([]bool{true}),
   165  		},
   166  	}
   167  	for _, tt := range testCases {
   168  		t.Run(tt.name, func(t *testing.T) {
   169  			assert.NotPanics(t, func() {
   170  				tt.val.AsBool()
   171  				tt.val.AsBoolSlice()
   172  				tt.val.AsFloat64()
   173  				tt.val.AsFloat64Slice()
   174  				tt.val.AsInt64()
   175  				tt.val.AsInt64Slice()
   176  				tt.val.AsInterface()
   177  				tt.val.AsString()
   178  				tt.val.AsStringSlice()
   179  			})
   180  		})
   181  	}
   182  }
   183  

View as plain text