window_op_test.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 node
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/xsql"
  18. "reflect"
  19. "testing"
  20. )
  21. var fivet = []*xsql.Tuple{
  22. {
  23. Message: map[string]interface{}{
  24. "f1": "v1",
  25. },
  26. },
  27. {
  28. Message: map[string]interface{}{
  29. "f2": "v2",
  30. },
  31. },
  32. {
  33. Message: map[string]interface{}{
  34. "f3": "v3",
  35. },
  36. },
  37. {
  38. Message: map[string]interface{}{
  39. "f4": "v4",
  40. },
  41. },
  42. {
  43. Message: map[string]interface{}{
  44. "f5": "v5",
  45. },
  46. },
  47. }
  48. func TestNewTupleList(t *testing.T) {
  49. _, e := NewTupleList(nil, 0)
  50. es1 := "Window size should not be less than zero."
  51. if !reflect.DeepEqual(es1, e.Error()) {
  52. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", es1, e)
  53. }
  54. _, e = NewTupleList(nil, 2)
  55. es1 = "The tuples should not be nil or empty."
  56. if !reflect.DeepEqual(es1, e.Error()) {
  57. t.Errorf("error mismatch:\n exp=%s\n got=%s\n\n", es1, e)
  58. }
  59. }
  60. func TestCountWindow(t *testing.T) {
  61. var tests = []struct {
  62. tuplelist TupleList
  63. expWinCount int
  64. winTupleSets []xsql.WindowTuples
  65. expRestTuples []*xsql.Tuple
  66. }{
  67. {
  68. tuplelist: TupleList{
  69. tuples: fivet,
  70. size: 5,
  71. },
  72. expWinCount: 1,
  73. winTupleSets: []xsql.WindowTuples{
  74. {
  75. Content: []xsql.TupleRow{
  76. &xsql.Tuple{
  77. Message: map[string]interface{}{
  78. "f1": "v1",
  79. },
  80. },
  81. &xsql.Tuple{
  82. Message: map[string]interface{}{
  83. "f2": "v2",
  84. },
  85. },
  86. &xsql.Tuple{
  87. Message: map[string]interface{}{
  88. "f3": "v3",
  89. },
  90. },
  91. &xsql.Tuple{
  92. Message: map[string]interface{}{
  93. "f4": "v4",
  94. },
  95. },
  96. &xsql.Tuple{
  97. Message: map[string]interface{}{
  98. "f5": "v5",
  99. },
  100. },
  101. },
  102. },
  103. },
  104. expRestTuples: []*xsql.Tuple{
  105. {
  106. Message: map[string]interface{}{
  107. "f2": "v2",
  108. },
  109. },
  110. {
  111. Message: map[string]interface{}{
  112. "f3": "v3",
  113. },
  114. },
  115. {
  116. Message: map[string]interface{}{
  117. "f4": "v4",
  118. },
  119. },
  120. {
  121. Message: map[string]interface{}{
  122. "f5": "v5",
  123. },
  124. },
  125. },
  126. },
  127. {
  128. tuplelist: TupleList{
  129. tuples: fivet,
  130. size: 3,
  131. },
  132. expWinCount: 1,
  133. winTupleSets: []xsql.WindowTuples{
  134. {
  135. Content: []xsql.TupleRow{
  136. &xsql.Tuple{
  137. Message: map[string]interface{}{
  138. "f3": "v3",
  139. },
  140. },
  141. &xsql.Tuple{
  142. Message: map[string]interface{}{
  143. "f4": "v4",
  144. },
  145. },
  146. &xsql.Tuple{
  147. Message: map[string]interface{}{
  148. "f5": "v5",
  149. },
  150. },
  151. },
  152. },
  153. },
  154. expRestTuples: []*xsql.Tuple{
  155. {
  156. Message: map[string]interface{}{
  157. "f4": "v4",
  158. },
  159. },
  160. {
  161. Message: map[string]interface{}{
  162. "f5": "v5",
  163. },
  164. },
  165. },
  166. },
  167. {
  168. tuplelist: TupleList{
  169. tuples: fivet,
  170. size: 2,
  171. },
  172. expWinCount: 1,
  173. winTupleSets: []xsql.WindowTuples{
  174. {
  175. Content: []xsql.TupleRow{
  176. &xsql.Tuple{
  177. Message: map[string]interface{}{
  178. "f4": "v4",
  179. },
  180. },
  181. &xsql.Tuple{
  182. Message: map[string]interface{}{
  183. "f5": "v5",
  184. },
  185. },
  186. },
  187. },
  188. },
  189. expRestTuples: []*xsql.Tuple{
  190. {
  191. Message: map[string]interface{}{
  192. "f5": "v5",
  193. },
  194. },
  195. },
  196. },
  197. {
  198. tuplelist: TupleList{
  199. tuples: fivet,
  200. size: 6,
  201. },
  202. expWinCount: 0,
  203. winTupleSets: nil,
  204. expRestTuples: []*xsql.Tuple{
  205. {
  206. Message: map[string]interface{}{
  207. "f1": "v1",
  208. },
  209. },
  210. {
  211. Message: map[string]interface{}{
  212. "f2": "v2",
  213. },
  214. },
  215. {
  216. Message: map[string]interface{}{
  217. "f3": "v3",
  218. },
  219. },
  220. {
  221. Message: map[string]interface{}{
  222. "f4": "v4",
  223. },
  224. },
  225. {
  226. Message: map[string]interface{}{
  227. "f5": "v5",
  228. },
  229. },
  230. },
  231. },
  232. }
  233. fmt.Printf("The test bucket size is %d.\n\n", len(tests))
  234. for i, tt := range tests {
  235. if tt.expWinCount == 0 {
  236. if tt.tuplelist.hasMoreCountWindow() {
  237. t.Errorf("%d \n Should not have more count window.", i)
  238. }
  239. } else {
  240. for j := 0; j < tt.expWinCount; j++ {
  241. if !tt.tuplelist.hasMoreCountWindow() {
  242. t.Errorf("%d \n Expect more element, but cannot find more element.", i)
  243. }
  244. cw := tt.tuplelist.nextCountWindow()
  245. if !reflect.DeepEqual(tt.winTupleSets[j].Content, cw.Content) {
  246. t.Errorf("%d. \nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.winTupleSets[j], cw)
  247. }
  248. }
  249. rest := tt.tuplelist.getRestTuples()
  250. if !reflect.DeepEqual(tt.expRestTuples, rest) {
  251. t.Errorf("%d. \nresult mismatch:\n\nexp=%#v\n\ngot=%#v\n\n", i, tt.expRestTuples, rest)
  252. }
  253. }
  254. }
  255. }