template_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Copyright 2023 EMQ Technologies Co., Ltd.
  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 transform
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. )
  20. func Test_SelectMap(t *testing.T) {
  21. type args struct {
  22. input interface{}
  23. fields []string
  24. }
  25. tests := []struct {
  26. name string
  27. args args
  28. want interface{}
  29. }{
  30. {
  31. name: "test1",
  32. args: args{
  33. input: map[string]interface{}{
  34. "a": 1,
  35. "b": 2,
  36. "c": 3,
  37. },
  38. fields: []string{"a", "b"},
  39. },
  40. want: map[string]interface{}{
  41. "a": 1,
  42. "b": 2,
  43. },
  44. },
  45. {
  46. name: "test2",
  47. args: args{
  48. input: []map[string]interface{}{
  49. {
  50. "a": 1,
  51. "b": 2,
  52. "c": 3,
  53. },
  54. },
  55. fields: []string{"a", "b"},
  56. },
  57. want: []map[string]interface{}{
  58. {
  59. "a": 1,
  60. "b": 2,
  61. },
  62. },
  63. },
  64. {
  65. name: "test3",
  66. args: args{
  67. input: []interface{}{
  68. map[string]interface{}{
  69. "a": 1,
  70. "b": 2,
  71. "c": 3,
  72. },
  73. },
  74. fields: []string{"a", "b"},
  75. },
  76. want: []map[string]interface{}{
  77. {
  78. "a": 1,
  79. "b": 2,
  80. },
  81. },
  82. },
  83. {
  84. name: "test4",
  85. args: args{
  86. input: []map[string]interface{}{
  87. {
  88. "a": 1,
  89. "b": 2,
  90. "c": 3,
  91. },
  92. },
  93. fields: nil,
  94. },
  95. want: []map[string]interface{}{
  96. {
  97. "a": 1,
  98. "b": 2,
  99. "c": 3,
  100. },
  101. },
  102. },
  103. {
  104. name: "test5",
  105. args: args{
  106. input: []byte(`{"a": 1, "b": 2, "c": 3}`),
  107. fields: nil,
  108. },
  109. want: []byte(`{"a": 1, "b": 2, "c": 3}`),
  110. },
  111. {
  112. name: "test6",
  113. args: args{
  114. input: []byte(`{"a": 1, "b": 2, "c": 3}`),
  115. fields: []string{"d"},
  116. },
  117. want: map[string]interface{}{
  118. "d": nil,
  119. },
  120. },
  121. }
  122. for _, tt := range tests {
  123. t.Run(tt.name, func(t *testing.T) {
  124. if got, _ := SelectMap(tt.args.input, tt.args.fields); !reflect.DeepEqual(got, tt.want) {
  125. t.Errorf("SelectMap() = %v, want %v", got, tt.want)
  126. fmt.Println(reflect.TypeOf(got), reflect.TypeOf(tt.want))
  127. }
  128. })
  129. }
  130. }