kv_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. package attribute_test
  15. import (
  16. "testing"
  17. "github.com/google/go-cmp/cmp"
  18. "github.com/stretchr/testify/assert"
  19. "go.opentelemetry.io/otel/attribute"
  20. )
  21. func TestKeyValueConstructors(t *testing.T) {
  22. tt := []struct {
  23. name string
  24. actual attribute.KeyValue
  25. expected attribute.KeyValue
  26. }{
  27. {
  28. name: "Bool",
  29. actual: attribute.Bool("k1", true),
  30. expected: attribute.KeyValue{
  31. Key: "k1",
  32. Value: attribute.BoolValue(true),
  33. },
  34. },
  35. {
  36. name: "Int64",
  37. actual: attribute.Int64("k1", 123),
  38. expected: attribute.KeyValue{
  39. Key: "k1",
  40. Value: attribute.Int64Value(123),
  41. },
  42. },
  43. {
  44. name: "Float64",
  45. actual: attribute.Float64("k1", 123.5),
  46. expected: attribute.KeyValue{
  47. Key: "k1",
  48. Value: attribute.Float64Value(123.5),
  49. },
  50. },
  51. {
  52. name: "String",
  53. actual: attribute.String("k1", "123.5"),
  54. expected: attribute.KeyValue{
  55. Key: "k1",
  56. Value: attribute.StringValue("123.5"),
  57. },
  58. },
  59. {
  60. name: "Int",
  61. actual: attribute.Int("k1", 123),
  62. expected: attribute.KeyValue{
  63. Key: "k1",
  64. Value: attribute.IntValue(123),
  65. },
  66. },
  67. }
  68. for _, test := range tt {
  69. t.Run(test.name, func(t *testing.T) {
  70. if diff := cmp.Diff(test.actual, test.expected, cmp.AllowUnexported(attribute.Value{})); diff != "" {
  71. t.Fatal(diff)
  72. }
  73. })
  74. }
  75. }
  76. func TestKeyValueValid(t *testing.T) {
  77. tests := []struct {
  78. desc string
  79. valid bool
  80. kv attribute.KeyValue
  81. }{
  82. {
  83. desc: "uninitialized KeyValue should be invalid",
  84. valid: false,
  85. kv: attribute.KeyValue{},
  86. },
  87. {
  88. desc: "empty key value should be invalid",
  89. valid: false,
  90. kv: attribute.Key("").Bool(true),
  91. },
  92. {
  93. desc: "INVALID value type should be invalid",
  94. valid: false,
  95. kv: attribute.KeyValue{
  96. Key: attribute.Key("valid key"),
  97. // Default type is INVALID.
  98. Value: attribute.Value{},
  99. },
  100. },
  101. {
  102. desc: "non-empty key with BOOL type Value should be valid",
  103. valid: true,
  104. kv: attribute.Bool("bool", true),
  105. },
  106. {
  107. desc: "non-empty key with INT64 type Value should be valid",
  108. valid: true,
  109. kv: attribute.Int64("int64", 0),
  110. },
  111. {
  112. desc: "non-empty key with FLOAT64 type Value should be valid",
  113. valid: true,
  114. kv: attribute.Float64("float64", 0),
  115. },
  116. {
  117. desc: "non-empty key with STRING type Value should be valid",
  118. valid: true,
  119. kv: attribute.String("string", ""),
  120. },
  121. }
  122. for _, test := range tests {
  123. if got, want := test.kv.Valid(), test.valid; got != want {
  124. t.Error(test.desc)
  125. }
  126. }
  127. }
  128. func TestIncorrectCast(t *testing.T) {
  129. testCases := []struct {
  130. name string
  131. val attribute.Value
  132. }{
  133. {
  134. name: "Float64",
  135. val: attribute.Float64Value(1.0),
  136. },
  137. {
  138. name: "Int64",
  139. val: attribute.Int64Value(2),
  140. },
  141. {
  142. name: "String",
  143. val: attribute.BoolValue(true),
  144. },
  145. {
  146. name: "Float64Slice",
  147. val: attribute.Float64SliceValue([]float64{1.0}),
  148. },
  149. {
  150. name: "Int64Slice",
  151. val: attribute.Int64SliceValue([]int64{2}),
  152. },
  153. {
  154. name: "StringSlice",
  155. val: attribute.BoolSliceValue([]bool{true}),
  156. },
  157. }
  158. for _, tt := range testCases {
  159. t.Run(tt.name, func(t *testing.T) {
  160. assert.NotPanics(t, func() {
  161. tt.val.AsBool()
  162. tt.val.AsBoolSlice()
  163. tt.val.AsFloat64()
  164. tt.val.AsFloat64Slice()
  165. tt.val.AsInt64()
  166. tt.val.AsInt64Slice()
  167. tt.val.AsInterface()
  168. tt.val.AsString()
  169. tt.val.AsStringSlice()
  170. })
  171. })
  172. }
  173. }