ruleset_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 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 processor
  15. import (
  16. "github.com/lf-edge/ekuiper/pkg/ast"
  17. "io"
  18. "reflect"
  19. "strings"
  20. "testing"
  21. )
  22. func TestIO(t *testing.T) {
  23. expected := `{"streams":{"demo":"CREATE STREAM demo () WITH (DATASOURCE=\"users\", FORMAT=\"JSON\")"},"tables":{},"rules":{"rule1":"{\"id\":\"rule1\",\"sql\": \"SELECT * FROM demo\",\"actions\": [{\"log\": {}}]}","rule2":"{\"id\": \"rule2\",\"sql\": \"SELECT * FROM demo\",\"actions\": [{ \"log\": {}}]}"}}`
  24. expectedStreams := []string{"demo"}
  25. expectedRules := []string{"rule1", "rule2"}
  26. sp := NewStreamProcessor()
  27. defer sp.db.Clean()
  28. rp := NewRuleProcessor()
  29. defer rp.db.Clean()
  30. rsp := NewRulesetProcessor(rp, sp)
  31. err := rsp.Import([]byte(expected), true)
  32. if err != nil {
  33. t.Errorf("fail to import ruleset: %v", err)
  34. return
  35. }
  36. streams, err := sp.execShow(ast.TypeStream)
  37. if err != nil {
  38. t.Errorf("fail to get all streams: %v", err)
  39. return
  40. }
  41. if !reflect.DeepEqual(streams, expectedStreams) {
  42. t.Errorf("After import, expect streams %v, but got %v", expectedStreams, streams)
  43. return
  44. }
  45. rules, err := rp.GetAllRules()
  46. if err != nil {
  47. t.Errorf("fail to get all rules: %v", err)
  48. return
  49. }
  50. if !reflect.DeepEqual(rules, expectedRules) {
  51. t.Errorf("After import, expect rules %v, but got %v", expectedRules, rules)
  52. return
  53. }
  54. exp, err := rsp.Export()
  55. if err != nil {
  56. t.Errorf("fail to export ruleset: %v", err)
  57. return
  58. }
  59. buf := new(strings.Builder)
  60. _, err = io.Copy(buf, exp)
  61. if err != nil {
  62. t.Errorf("fail to convert exported ruleset: %v", err)
  63. return
  64. }
  65. actual := buf.String()
  66. if actual != expected {
  67. t.Errorf("Expect\t\n %v but got\t\n %v", expected, actual)
  68. }
  69. }