cast_test.go 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // Copyright 2021-2022 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 cast
  15. import (
  16. "encoding/hex"
  17. "fmt"
  18. "reflect"
  19. "testing"
  20. )
  21. func TestMapConvert_Funcs(t *testing.T) {
  22. source := map[interface{}]interface{}{
  23. "QUERY_TABLE": "VBAP",
  24. "ROWCOUNT": 10,
  25. "FIELDS": []interface{}{
  26. map[interface{}]interface{}{"FIELDNAME": "MANDT"},
  27. map[interface{}]interface{}{"FIELDNAME": "VBELN"},
  28. map[interface{}]interface{}{"FIELDNAME": "POSNR"},
  29. },
  30. }
  31. exp := map[string]interface{}{
  32. "QUERY_TABLE": "VBAP",
  33. "ROWCOUNT": 10,
  34. "FIELDS": []interface{}{
  35. map[string]interface{}{"FIELDNAME": "MANDT"},
  36. map[string]interface{}{"FIELDNAME": "VBELN"},
  37. map[string]interface{}{"FIELDNAME": "POSNR"},
  38. },
  39. }
  40. got := ConvertMap(source)
  41. if !reflect.DeepEqual(exp, got) {
  42. t.Errorf("result mismatch:\n\nexp=%s\n\ngot=%s\n\n", exp, got)
  43. }
  44. }
  45. func TestToTypedSlice(t *testing.T) {
  46. var tests = []struct {
  47. s interface{}
  48. r interface{}
  49. e string
  50. }{
  51. {
  52. s: []interface{}{"abc", 123},
  53. r: []string{"abc", "123"},
  54. },
  55. {
  56. s: []interface{}{"addd", "bbb"},
  57. r: []string{"addd", "bbb"},
  58. },
  59. {
  60. s: []interface{}{nil, "bbb", "ddd"},
  61. e: "cannot convert []interface {}([<nil> bbb ddd]) to string slice for the 0 element: <nil>",
  62. },
  63. }
  64. t.Logf("The test bucket size is %d.", len(tests))
  65. for i, tt := range tests {
  66. result, err := ToTypedSlice(tt.s, func(input interface{}, ssn Strictness) (interface{}, error) {
  67. if input == nil {
  68. return nil, nil
  69. } else {
  70. return fmt.Sprintf("%v", input), nil
  71. }
  72. }, "string", CONVERT_SAMEKIND)
  73. if !reflect.DeepEqual(tt.e, errstring(err)) {
  74. t.Errorf("%d: error mismatch:\n exp=%s\n got=%s\n\n", i, tt.e, err)
  75. } else if tt.e == "" && !reflect.DeepEqual(tt.r, result) {
  76. t.Errorf("%d\n\nstmt mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.r, result)
  77. }
  78. }
  79. }
  80. func errstring(err error) string {
  81. if err != nil {
  82. return err.Error()
  83. }
  84. return ""
  85. }
  86. func TestMapToStructStrict(t *testing.T) {
  87. type args struct {
  88. input interface{}
  89. output interface{}
  90. expect interface{}
  91. }
  92. type Result struct {
  93. Foo string `json:"foo"`
  94. Bar string `json:"bar"`
  95. }
  96. tests := []struct {
  97. name string
  98. args args
  99. wantErr bool
  100. }{
  101. {
  102. name: "normal parse",
  103. args: args{
  104. input: map[string]interface{}{
  105. "foo": "foo",
  106. "bar": "bar",
  107. },
  108. output: &Result{},
  109. expect: &Result{
  110. Foo: "foo",
  111. Bar: "bar",
  112. },
  113. },
  114. wantErr: false,
  115. },
  116. {
  117. name: "input have more than keys",
  118. args: args{
  119. input: map[string]interface{}{
  120. "foo": "foo",
  121. "bar": "bar",
  122. "foobar": "foobar",
  123. },
  124. output: &Result{},
  125. },
  126. wantErr: true,
  127. },
  128. {
  129. name: "input have less keys",
  130. args: args{
  131. input: map[string]interface{}{
  132. "foo": "foo",
  133. },
  134. output: &Result{},
  135. expect: &Result{
  136. Foo: "foo",
  137. },
  138. },
  139. wantErr: false,
  140. },
  141. {
  142. name: "input have unused keys",
  143. args: args{
  144. input: map[string]interface{}{
  145. "foo": "foo",
  146. "foobar": "foobar",
  147. },
  148. output: &Result{},
  149. },
  150. wantErr: true,
  151. },
  152. }
  153. for _, tt := range tests {
  154. t.Run(tt.name, func(t *testing.T) {
  155. err := MapToStructStrict(tt.args.input, tt.args.output)
  156. if (err != nil) != tt.wantErr {
  157. t.Errorf("MapToStructure() error = %v, wantErr %v", err, tt.wantErr)
  158. }
  159. if tt.wantErr == false && !reflect.DeepEqual(tt.args.output, tt.args.expect) {
  160. t.Errorf(" got = %v, want %v", tt.args.output, tt.args.expect)
  161. }
  162. })
  163. }
  164. }
  165. func TestMapToStruct(t *testing.T) {
  166. type args struct {
  167. input interface{}
  168. output interface{}
  169. expect interface{}
  170. }
  171. type Result struct {
  172. Foo string `json:"foo"`
  173. Bar string `json:"bar"`
  174. }
  175. tests := []struct {
  176. name string
  177. args args
  178. wantErr bool
  179. }{
  180. {
  181. name: "normal parse",
  182. args: args{
  183. input: map[string]interface{}{
  184. "foo": "foo",
  185. "bar": "bar",
  186. },
  187. output: &Result{},
  188. expect: &Result{
  189. Foo: "foo",
  190. Bar: "bar",
  191. },
  192. },
  193. wantErr: false,
  194. },
  195. {
  196. name: "input have more than keys",
  197. args: args{
  198. input: map[string]interface{}{
  199. "foo": "foo",
  200. "bar": "bar",
  201. "foobar": "foobar",
  202. },
  203. output: &Result{},
  204. expect: &Result{
  205. Foo: "foo",
  206. Bar: "bar",
  207. },
  208. },
  209. wantErr: false,
  210. },
  211. {
  212. name: "input have less keys",
  213. args: args{
  214. input: map[string]interface{}{
  215. "foo": "foo",
  216. },
  217. output: &Result{},
  218. expect: &Result{
  219. Foo: "foo",
  220. },
  221. },
  222. wantErr: false,
  223. },
  224. {
  225. name: "input have unused keys",
  226. args: args{
  227. input: map[string]interface{}{
  228. "foo": "foo",
  229. "foobar": "foobar",
  230. },
  231. output: &Result{},
  232. expect: &Result{
  233. Foo: "foo",
  234. },
  235. },
  236. wantErr: false,
  237. },
  238. }
  239. for _, tt := range tests {
  240. t.Run(tt.name, func(t *testing.T) {
  241. if err := MapToStruct(tt.args.input, tt.args.output); (err != nil) != tt.wantErr {
  242. t.Errorf("MapToStructure() error = %v, wantErr %v", err, tt.wantErr)
  243. }
  244. })
  245. }
  246. }
  247. func TestMapToStructNotCaseSensitive(t *testing.T) {
  248. type args struct {
  249. input interface{}
  250. output interface{}
  251. expect interface{}
  252. }
  253. type Result struct {
  254. Foo string `json:"foo"`
  255. Bar string
  256. }
  257. tests := []struct {
  258. name string
  259. args args
  260. wantErr bool
  261. }{
  262. {
  263. name: "normal parse",
  264. args: args{
  265. input: map[string]interface{}{
  266. "foo": "foo",
  267. "bar": "bar",
  268. },
  269. output: &Result{},
  270. expect: &Result{
  271. Foo: "foo",
  272. Bar: "bar",
  273. },
  274. },
  275. wantErr: false,
  276. },
  277. {
  278. name: "not case sensitive",
  279. args: args{
  280. input: map[string]interface{}{
  281. "FOO": "foo",
  282. "BAR": "bar",
  283. },
  284. output: &Result{},
  285. expect: &Result{
  286. Foo: "foo",
  287. Bar: "bar",
  288. },
  289. },
  290. wantErr: false,
  291. },
  292. {
  293. name: "keys must match",
  294. args: args{
  295. input: map[string]interface{}{
  296. "foo": "foo",
  297. "BARS": "bars",
  298. },
  299. output: &Result{},
  300. expect: &Result{
  301. Foo: "foo",
  302. },
  303. },
  304. wantErr: false,
  305. },
  306. }
  307. for _, tt := range tests {
  308. t.Run(tt.name, func(t *testing.T) {
  309. if err := MapToStruct(tt.args.input, tt.args.output); (err != nil) != tt.wantErr {
  310. t.Errorf("MapToStructure() error = %v, wantErr %v", err, tt.wantErr)
  311. }
  312. })
  313. }
  314. }
  315. func TestMapToStructTag(t *testing.T) {
  316. type args struct {
  317. input interface{}
  318. output interface{}
  319. expect interface{}
  320. }
  321. type Result struct {
  322. Foo string `json:"fo"`
  323. Bar string
  324. }
  325. tests := []struct {
  326. name string
  327. args args
  328. wantErr bool
  329. }{
  330. {
  331. name: "normal parse",
  332. args: args{
  333. input: map[string]interface{}{
  334. "fo": "foo",
  335. "bar": "bar",
  336. },
  337. output: &Result{},
  338. expect: &Result{
  339. Foo: "foo",
  340. Bar: "bar",
  341. },
  342. },
  343. wantErr: false,
  344. },
  345. {
  346. name: "key tag not match",
  347. args: args{
  348. input: map[string]interface{}{
  349. "FOO": "foo",
  350. "BAR": "bar",
  351. },
  352. output: &Result{},
  353. expect: &Result{
  354. Bar: "bar",
  355. },
  356. },
  357. wantErr: false,
  358. },
  359. {
  360. name: "key tag not match",
  361. args: args{
  362. input: map[string]interface{}{
  363. "foo": "foo",
  364. "BARS": "bars",
  365. },
  366. output: &Result{},
  367. expect: &Result{},
  368. },
  369. wantErr: false,
  370. },
  371. }
  372. for _, tt := range tests {
  373. t.Run(tt.name, func(t *testing.T) {
  374. if err := MapToStruct(tt.args.input, tt.args.output); (err != nil) != tt.wantErr {
  375. t.Errorf("MapToStructure() error = %v, wantErr %v", err, tt.wantErr)
  376. }
  377. })
  378. }
  379. }
  380. func TestToByteA(t *testing.T) {
  381. bytea, _ := hex.DecodeString("736f6d6520646174612077697468200020616e6420efbbbf")
  382. tests := []struct {
  383. input interface{}
  384. output []byte
  385. err string
  386. }{
  387. {
  388. input: "foo",
  389. err: "illegal string foo, must be base64 encoded string",
  390. }, {
  391. input: []byte("foo"),
  392. output: []byte("foo"),
  393. err: "",
  394. }, {
  395. input: 1,
  396. output: nil,
  397. err: "cannot convert int(1) to bytes",
  398. }, {
  399. input: "c29tZSBkYXRhIHdpdGggACBhbmQg77u/",
  400. output: bytea,
  401. },
  402. }
  403. for i, tt := range tests {
  404. r, err := ToByteA(tt.input, CONVERT_SAMEKIND)
  405. if err != nil {
  406. if err.Error() != tt.err {
  407. t.Errorf("%d, ToByteA() error = %v, wantErr %v", i, err, tt.err)
  408. continue
  409. }
  410. } else {
  411. if !reflect.DeepEqual(r, tt.output) {
  412. t.Errorf("%d: ToByteA() = %x, want %x", i, r, tt.output)
  413. }
  414. }
  415. }
  416. }