funcs_trans_test.go 4.9 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 function
  15. import (
  16. "fmt"
  17. "reflect"
  18. "testing"
  19. "github.com/lf-edge/ekuiper/internal/conf"
  20. kctx "github.com/lf-edge/ekuiper/internal/topo/context"
  21. "github.com/lf-edge/ekuiper/internal/topo/state"
  22. "github.com/lf-edge/ekuiper/pkg/api"
  23. )
  24. func TestCompressExec(t *testing.T) {
  25. ff, ok := builtinStatfulFuncs["compress"]
  26. if !ok {
  27. t.Fatal("builtin not found")
  28. }
  29. f := ff()
  30. contextLogger := conf.Log.WithField("rule", "testCompressExec")
  31. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  32. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  33. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  34. tests := []struct {
  35. args []interface{}
  36. result interface{}
  37. }{
  38. { // 0
  39. args: []interface{}{
  40. "foo",
  41. "bar",
  42. },
  43. result: fmt.Errorf("unsupported compressor: bar"),
  44. }, { // 1
  45. args: []interface{}{
  46. "hello world",
  47. "zlib",
  48. },
  49. result: []byte{120, 156, 0, 11, 0, 244, 255, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 3, 0, 26, 11, 4, 93},
  50. }, { // 2
  51. args: []interface{}{
  52. `{"name":"John Doe","age":30,"email":"john.doe@example.com"}`,
  53. "zlib",
  54. },
  55. result: []byte{120, 156, 0, 59, 0, 196, 255, 123, 34, 110, 97, 109, 101, 34, 58, 34, 74, 111, 104, 110, 32, 68, 111, 101, 34, 44, 34, 97, 103, 101, 34, 58, 51, 48, 44, 34, 101, 109, 97, 105, 108, 34, 58, 34, 106, 111, 104, 110, 46, 100, 111, 101, 64, 101, 120, 97, 109, 112, 108, 101, 46, 99, 111, 109, 34, 125, 3, 0, 32, 223, 19, 1},
  56. }, { // 3
  57. args: []interface{}{
  58. `{"name":"John Doe","age":30,"email":"john.doe@example.com","address":{"street":"123 Main St","city":"Anytown","state":"CA","zip":"12345"},"phoneNumbers":[{"type":"home","number":"555-555-1234"},{"type":"work","number":"555-555-5678"}],"isActive":true}`,
  59. "gzip",
  60. },
  61. result: fmt.Errorf("compress type must be consistent, previous zlib, now gzip"),
  62. }, { // 4
  63. args: []interface{}{
  64. `hello world`,
  65. "zlib",
  66. },
  67. result: []byte{120, 156, 0, 11, 0, 244, 255, 104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100, 3, 0, 26, 11, 4, 93},
  68. },
  69. }
  70. for i, tt := range tests {
  71. result, _ := f.Exec(tt.args, fctx)
  72. if !reflect.DeepEqual(result, tt.result) {
  73. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  74. }
  75. }
  76. }
  77. func TestDecompressExec(t *testing.T) {
  78. ff, ok := builtinStatfulFuncs["decompress"]
  79. if !ok {
  80. t.Fatal("builtin not found")
  81. }
  82. f := ff()
  83. contextLogger := conf.Log.WithField("rule", "testDecompressExec")
  84. ctx := kctx.WithValue(kctx.Background(), kctx.LoggerKey, contextLogger)
  85. tempStore, _ := state.CreateStore("mockRule0", api.AtMostOnce)
  86. fctx := kctx.NewDefaultFuncContext(ctx.WithMeta("mockRule0", "test", tempStore), 2)
  87. tests := []struct {
  88. args []interface{}
  89. result interface{}
  90. }{
  91. { // 0
  92. args: []interface{}{
  93. "foo",
  94. "bar",
  95. },
  96. result: fmt.Errorf("unsupported decompressor: bar"),
  97. }, { // 1
  98. args: []interface{}{
  99. []byte{120, 156, 202, 72, 205, 201, 201, 87, 40, 207, 47, 202, 73, 1, 4, 0, 0, 255, 255, 26, 11, 4, 93},
  100. "zlib",
  101. },
  102. result: []byte("hello world"),
  103. }, { // 2
  104. args: []interface{}{
  105. []byte{120, 156, 170, 86, 202, 75, 204, 77, 85, 178, 82, 242, 202, 207, 200, 83, 112, 201, 79, 85, 210, 81, 74, 76, 79, 85, 178, 50, 54, 208, 81, 74, 205, 77, 204, 204, 81, 178, 82, 202, 202, 207, 200, 211, 75, 201, 79, 117, 72, 173, 72, 204, 45, 200, 73, 213, 75, 206, 207, 85, 170, 5, 4, 0, 0, 255, 255, 32, 223, 19, 1},
  106. "zlib",
  107. },
  108. result: []byte(`{"name":"John Doe","age":30,"email":"john.doe@example.com"}`),
  109. }, { // 3
  110. args: []interface{}{
  111. `{"name":"John Doe","age":30,"email":"john.doe@example.com","address":{"street":"123 Main St","city":"Anytown","state":"CA","zip":"12345"},"phoneNumbers":[{"type":"home","number":"555-555-1234"},{"type":"work","number":"555-555-5678"}],"isActive":true}`,
  112. "gzip",
  113. },
  114. result: fmt.Errorf("decompress type must be consistent, previous zlib, now gzip"),
  115. }, { // 4
  116. args: []interface{}{
  117. []byte{120, 156, 202, 72, 205, 201, 201, 87, 40, 207, 47, 202, 73, 1, 4, 0, 0, 255, 255, 26, 11, 4, 93},
  118. "zlib",
  119. },
  120. result: []byte("hello world"),
  121. },
  122. }
  123. for i, tt := range tests {
  124. result, _ := f.Exec(tt.args, fctx)
  125. if !reflect.DeepEqual(result, tt.result) {
  126. t.Errorf("%d result mismatch,\ngot:\t%v \nwant:\t%v", i, result, tt.result)
  127. }
  128. }
  129. }