havingPlan_test.go 2.7 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 planner
  15. import (
  16. "testing"
  17. "github.com/stretchr/testify/assert"
  18. "github.com/lf-edge/ekuiper/pkg/ast"
  19. )
  20. func TestHavingPlan_ExtractStateFunc(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. condition ast.Expr
  24. newCondition ast.Expr
  25. stateFuncs []*ast.Call
  26. }{
  27. {
  28. name: "test extract one",
  29. condition: &ast.BinaryExpr{
  30. OP: ast.GT,
  31. LHS: &ast.Call{
  32. Name: "last_agg_hit_count",
  33. },
  34. RHS: &ast.IntegerLiteral{
  35. Val: 1,
  36. },
  37. },
  38. newCondition: &ast.BinaryExpr{
  39. OP: ast.GT,
  40. LHS: &ast.Call{
  41. Name: "last_agg_hit_count",
  42. Cached: true,
  43. },
  44. RHS: &ast.IntegerLiteral{
  45. Val: 1,
  46. },
  47. },
  48. stateFuncs: []*ast.Call{
  49. {
  50. Name: "last_agg_hit_count",
  51. },
  52. },
  53. },
  54. {
  55. name: "test extract multiple",
  56. condition: &ast.BinaryExpr{
  57. OP: ast.AND,
  58. LHS: &ast.BinaryExpr{
  59. OP: ast.GT,
  60. LHS: &ast.Call{
  61. Name: "last_agg_hit_time",
  62. },
  63. RHS: &ast.IntegerLiteral{
  64. Val: 1,
  65. },
  66. },
  67. RHS: &ast.Call{
  68. Name: "last_agg_hit_count",
  69. },
  70. },
  71. newCondition: &ast.BinaryExpr{
  72. OP: ast.AND,
  73. LHS: &ast.BinaryExpr{
  74. OP: ast.GT,
  75. LHS: &ast.Call{
  76. Name: "last_agg_hit_time",
  77. Cached: true,
  78. },
  79. RHS: &ast.IntegerLiteral{
  80. Val: 1,
  81. },
  82. },
  83. RHS: &ast.Call{
  84. Name: "last_agg_hit_count",
  85. Cached: true,
  86. },
  87. },
  88. stateFuncs: []*ast.Call{
  89. {
  90. Name: "last_agg_hit_time",
  91. }, {
  92. Name: "last_agg_hit_count",
  93. },
  94. },
  95. },
  96. {
  97. name: "test extract none",
  98. condition: &ast.BinaryExpr{
  99. OP: ast.GT,
  100. LHS: &ast.Call{
  101. Name: "event_time",
  102. },
  103. RHS: &ast.IntegerLiteral{
  104. Val: 1,
  105. },
  106. },
  107. newCondition: &ast.BinaryExpr{
  108. OP: ast.GT,
  109. LHS: &ast.Call{
  110. Name: "event_time",
  111. },
  112. RHS: &ast.IntegerLiteral{
  113. Val: 1,
  114. },
  115. },
  116. stateFuncs: nil,
  117. },
  118. }
  119. for _, tt := range tests {
  120. t.Run(tt.name, func(t *testing.T) {
  121. p := &HavingPlan{
  122. condition: tt.condition,
  123. }
  124. p.ExtractStateFunc()
  125. assert.Equal(t, tt.newCondition, p.condition)
  126. assert.Equal(t, tt.stateFuncs, p.stateFuncs)
  127. })
  128. }
  129. }