base_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Copyright 2019 Yunion
  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 options
  15. import (
  16. "reflect"
  17. "testing"
  18. "yunion.io/x/jsonutils"
  19. )
  20. type S struct {
  21. In interface{}
  22. Want string
  23. }
  24. func testS(t *testing.T, c *S) {
  25. jsonGot, err := StructToParams(c.In)
  26. if err != nil {
  27. t.Errorf("StructToParams failed: in: %#v: err: %s",
  28. c.In, err)
  29. }
  30. jsonWant, _ := jsonutils.ParseString(c.Want)
  31. if !reflect.DeepEqual(jsonGot, jsonWant) {
  32. t.Errorf("json not equal, want %s, got %s",
  33. jsonWant.String(), jsonGot.String())
  34. }
  35. }
  36. func testSs(t *testing.T, cs []*S) {
  37. for _, c := range cs {
  38. testS(t, c)
  39. }
  40. }
  41. func TestOptionsStructToParams(t *testing.T) {
  42. t.Run("int", func(t *testing.T) {
  43. type s struct {
  44. Int int
  45. }
  46. cases := []*S{
  47. {
  48. In: &s{100},
  49. Want: "{int: 100}",
  50. },
  51. {
  52. In: &s{0},
  53. Want: "{int: 0}",
  54. },
  55. }
  56. testSs(t, cases)
  57. })
  58. t.Run("int ptr", func(t *testing.T) {
  59. type s struct {
  60. IntP *int
  61. }
  62. cases := []*S{
  63. {
  64. In: &s{},
  65. Want: "{}",
  66. },
  67. {
  68. In: &s{Int(100)},
  69. Want: "{int_p: 100}",
  70. },
  71. {
  72. In: &s{Int(0)},
  73. Want: "{int_p: 0}",
  74. },
  75. }
  76. testSs(t, cases)
  77. })
  78. t.Run("bool", func(t *testing.T) {
  79. type s struct {
  80. Bool bool
  81. }
  82. cases := []*S{
  83. {
  84. In: &s{},
  85. Want: "{bool: false}",
  86. },
  87. {
  88. In: &s{true},
  89. Want: "{bool: true}",
  90. },
  91. {
  92. In: &s{false},
  93. Want: "{bool: false}",
  94. },
  95. }
  96. testSs(t, cases)
  97. })
  98. t.Run("bool ptr", func(t *testing.T) {
  99. type s struct {
  100. BoolP *bool
  101. }
  102. cases := []*S{
  103. {
  104. In: &s{},
  105. Want: "{}",
  106. },
  107. {
  108. In: &s{Bool(true)},
  109. Want: "{bool_p: true}",
  110. },
  111. {
  112. In: &s{Bool(false)},
  113. Want: "{bool_p: false}",
  114. },
  115. }
  116. testSs(t, cases)
  117. })
  118. t.Run("string", func(t *testing.T) {
  119. type s struct {
  120. String string
  121. }
  122. cases := []*S{
  123. {
  124. In: &s{},
  125. Want: `{}`,
  126. },
  127. {
  128. In: &s{""},
  129. Want: `{}`,
  130. },
  131. {
  132. In: &s{"holy"},
  133. Want: `{string: "holy"}`,
  134. },
  135. }
  136. testSs(t, cases)
  137. })
  138. t.Run("string ptr", func(t *testing.T) {
  139. type s struct {
  140. String *string `json:",allowempty"`
  141. }
  142. cases := []*S{
  143. {
  144. In: &s{},
  145. Want: `{}`,
  146. },
  147. {
  148. In: &s{String("")},
  149. Want: `{"string": ""}`,
  150. },
  151. {
  152. In: &s{String("holy")},
  153. Want: `{string: "holy"}`,
  154. },
  155. }
  156. testSs(t, cases)
  157. })
  158. t.Run("string slice", func(t *testing.T) {
  159. type s struct {
  160. StringSlice []string
  161. }
  162. cases := []*S{
  163. {
  164. In: &s{},
  165. Want: `{}`,
  166. },
  167. {
  168. In: &s{[]string{}},
  169. Want: `{}`,
  170. },
  171. {
  172. In: &s{[]string{"holy"}},
  173. Want: `{"string_slice.0": "holy"}`,
  174. },
  175. {
  176. In: &s{[]string{"holy", "goblet"}},
  177. Want: `{"string_slice.0": "holy", "string_slice.1": "goblet"}`,
  178. },
  179. }
  180. testSs(t, cases)
  181. })
  182. t.Run("json tag", func(t *testing.T) {
  183. type s struct {
  184. StringSlice []string `json:"string"`
  185. }
  186. cases := []*S{
  187. {
  188. In: &s{},
  189. Want: `{}`,
  190. },
  191. {
  192. In: &s{[]string{}},
  193. Want: `{}`,
  194. },
  195. {
  196. In: &s{[]string{"holy"}},
  197. Want: `{"string.0": "holy"}`,
  198. },
  199. {
  200. In: &s{[]string{"holy", "goblet"}},
  201. Want: `{"string.0": "holy", "string.1": "goblet"}`,
  202. },
  203. }
  204. testSs(t, cases)
  205. })
  206. t.Run("json tag ignore", func(t *testing.T) {
  207. type s struct {
  208. StringSliceIgnored []string `json:"-"`
  209. }
  210. cases := []*S{
  211. {
  212. In: &s{},
  213. Want: `{}`,
  214. },
  215. {
  216. In: &s{[]string{}},
  217. Want: `{}`,
  218. },
  219. {
  220. In: &s{[]string{"holy"}},
  221. Want: `{}`,
  222. },
  223. {
  224. In: &s{[]string{"holy", "goblet"}},
  225. Want: `{}`,
  226. },
  227. }
  228. testSs(t, cases)
  229. })
  230. }
  231. func TestBaseListOptions(t *testing.T) {
  232. t.Run("pending-delete-all", func(t *testing.T) {
  233. opts := &BaseListOptions{
  234. PendingDeleteAll: Bool(true),
  235. }
  236. params, err := opts.Params()
  237. if err != nil {
  238. t.Fatalf("unexpected error: %s", err)
  239. }
  240. for _, f := range []string{"details"} {
  241. got, err := params.Bool(f)
  242. if got {
  243. t.Fatalf("pending_delete=all should not imply details=true: %v", err)
  244. }
  245. }
  246. })
  247. }