provider_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. */
  12. package util
  13. import (
  14. "encoding/json"
  15. "testing"
  16. "github.com/stretchr/testify/assert"
  17. )
  18. func TestParameterTagsToJSONString(t *testing.T) {
  19. tests := []struct {
  20. name string
  21. tags map[string]string
  22. expected string
  23. wantErr bool
  24. }{
  25. {
  26. name: "Valid tags",
  27. tags: map[string]string{
  28. "key1": "value1",
  29. "key2": "value2",
  30. },
  31. expected: `{"key1":"value1","key2":"value2"}`,
  32. wantErr: false,
  33. },
  34. {
  35. name: "Empty tags",
  36. tags: map[string]string{},
  37. expected: `{}`,
  38. wantErr: false,
  39. },
  40. {
  41. name: "Nil tags",
  42. tags: nil,
  43. wantErr: false,
  44. expected: "null",
  45. },
  46. }
  47. for _, tt := range tests {
  48. t.Run(tt.name, func(t *testing.T) {
  49. result, err := ParameterTagsToJSONString(tt.tags)
  50. if tt.wantErr {
  51. assert.Error(t, err)
  52. } else {
  53. assert.NoError(t, err)
  54. var resultMap map[string]string
  55. err := json.Unmarshal([]byte(result), &resultMap)
  56. assert.NoError(t, err)
  57. assert.Equal(t, tt.expected, result)
  58. }
  59. })
  60. }
  61. }
  62. func TestFindTagKeysToRemove(t *testing.T) {
  63. tests := []struct {
  64. name string
  65. tags map[string]string
  66. metaTags map[string]string
  67. expected []string
  68. }{
  69. {
  70. name: "No tags to remove",
  71. tags: map[string]string{
  72. "key1": "value1",
  73. "key2": "value2",
  74. },
  75. metaTags: map[string]string{
  76. "key1": "value1",
  77. "key2": "value2",
  78. },
  79. expected: []string{},
  80. },
  81. {
  82. name: "Some tags to remove",
  83. tags: map[string]string{
  84. "key1": "value1",
  85. "key2": "value2",
  86. "key3": "value3",
  87. },
  88. metaTags: map[string]string{
  89. "key1": "value1",
  90. "key2": "value2",
  91. },
  92. expected: []string{"key3"},
  93. },
  94. {
  95. name: "All tags to remove",
  96. tags: map[string]string{
  97. "key1": "value1",
  98. "key2": "value2",
  99. },
  100. metaTags: map[string]string{},
  101. expected: []string{"key1", "key2"},
  102. },
  103. {
  104. name: "Empty tags and metaTags",
  105. tags: map[string]string{},
  106. metaTags: map[string]string{},
  107. expected: []string{},
  108. },
  109. {
  110. name: "Empty metaTags with non-empty tags",
  111. tags: map[string]string{
  112. "key1": "value1",
  113. },
  114. metaTags: map[string]string{},
  115. expected: []string{"key1"},
  116. },
  117. }
  118. for _, tt := range tests {
  119. t.Run(tt.name, func(t *testing.T) {
  120. result := FindTagKeysToRemove(tt.tags, tt.metaTags)
  121. assert.ElementsMatch(t, tt.expected, result)
  122. })
  123. }
  124. }