file_sink_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 file
  15. import (
  16. "github.com/lf-edge/ekuiper/internal/conf"
  17. "github.com/lf-edge/ekuiper/internal/topo/context"
  18. "github.com/lf-edge/ekuiper/internal/topo/transform"
  19. "github.com/lf-edge/ekuiper/pkg/message"
  20. "os"
  21. "reflect"
  22. "testing"
  23. )
  24. // Unit test for Configure function
  25. func TestConfigure(t *testing.T) {
  26. props := map[string]interface{}{
  27. "interval": 500,
  28. "path": "test",
  29. }
  30. m := &fileSink{}
  31. err := m.Configure(props)
  32. if err != nil {
  33. t.Errorf("Configure() error = %v, wantErr nil", err)
  34. }
  35. if m.c.Interval != 500 {
  36. t.Errorf("Configure() Interval = %v, want 500", m.c.Interval)
  37. }
  38. if m.c.Path != "test" {
  39. t.Errorf("Configure() Path = %v, want test", m.c.Path)
  40. }
  41. err = m.Configure(map[string]interface{}{"interval": -1, "path": "test"})
  42. if err == nil {
  43. t.Errorf("Configure() error = %v, wantErr not nil", err)
  44. }
  45. err = m.Configure(map[string]interface{}{"interval": 500, "path": ""})
  46. if err == nil {
  47. t.Errorf("Configure() error = %v, wantErr not nil", err)
  48. }
  49. err = m.Configure(map[string]interface{}{"fileType": "csv2"})
  50. if err == nil {
  51. t.Errorf("Configure() error = %v, wantErr not nil", err)
  52. }
  53. err = m.Configure(map[string]interface{}{"interval": 500,
  54. "path": "test",
  55. "fileType": "csv"})
  56. if err == nil {
  57. t.Errorf("Configure() error = %v, wantErr not nil", err)
  58. }
  59. }
  60. func TestFileSink_Configure(t *testing.T) {
  61. tests := []struct {
  62. name string
  63. c *sinkConf
  64. p map[string]interface{}
  65. }{
  66. {
  67. name: "default configurations",
  68. c: &sinkConf{
  69. Interval: 1000,
  70. Path: "cache",
  71. FileType: LINES_TYPE,
  72. },
  73. p: map[string]interface{}{},
  74. },
  75. {
  76. name: "previous setting",
  77. c: &sinkConf{
  78. Interval: 500,
  79. Path: "test",
  80. FileType: LINES_TYPE,
  81. },
  82. p: map[string]interface{}{
  83. "interval": 500,
  84. "path": "test",
  85. },
  86. },
  87. {
  88. name: "new props",
  89. c: &sinkConf{
  90. Interval: 500,
  91. Path: "test",
  92. FileType: CSV_TYPE,
  93. Format: message.FormatDelimited,
  94. Delimiter: ",",
  95. },
  96. p: map[string]interface{}{
  97. "interval": 500,
  98. "path": "test",
  99. "fileType": "csv",
  100. "format": message.FormatDelimited,
  101. },
  102. },
  103. }
  104. for _, tt := range tests {
  105. t.Run(tt.name, func(t *testing.T) {
  106. m := &fileSink{}
  107. if err := m.Configure(tt.p); err != nil {
  108. t.Errorf("fileSink.Configure() error = %v", err)
  109. return
  110. }
  111. if !reflect.DeepEqual(m.c, tt.c) {
  112. t.Errorf("fileSink.Configure() = %v, want %v", m.c, tt.c)
  113. }
  114. })
  115. }
  116. }
  117. func TestFileSink_Collect(t *testing.T) {
  118. tests := []struct {
  119. name string
  120. ft FileType
  121. fname string
  122. content []byte
  123. }{
  124. {
  125. name: "lines",
  126. ft: LINES_TYPE,
  127. fname: "test_lines",
  128. content: []byte("{\"key\":\"value1\"}\n{\"key\":\"value2\"}"),
  129. }, {
  130. name: "json",
  131. ft: JSON_TYPE,
  132. fname: "test_json",
  133. content: []byte(`[{"key":"value1"}{"key":"value2"}]`),
  134. }, {
  135. name: "csv",
  136. ft: CSV_TYPE,
  137. fname: "test_csv",
  138. content: []byte("key\n{\"key\":\"value1\"}\n{\"key\":\"value2\"}"),
  139. },
  140. }
  141. // Create a stream context for testing
  142. contextLogger := conf.Log.WithField("rule", "test2")
  143. ctx := context.WithValue(context.Background(), context.LoggerKey, contextLogger)
  144. tf, _ := transform.GenTransform("", "json", "", "")
  145. vCtx := context.WithValue(ctx, context.TransKey, tf)
  146. for _, tt := range tests {
  147. t.Run(tt.name, func(t *testing.T) {
  148. // Create a temporary file for testing
  149. tmpfile, err := os.CreateTemp("", tt.fname)
  150. if err != nil {
  151. t.Fatal(err)
  152. }
  153. defer os.Remove(tmpfile.Name())
  154. // Create a file sink with the temporary file path
  155. sink := &fileSink{c: &sinkConf{Path: tmpfile.Name(), FileType: tt.ft, HasHeader: true}}
  156. sink.Open(ctx)
  157. // Test collecting a map item
  158. m := map[string]interface{}{"key": "value1"}
  159. if err := sink.Collect(vCtx, m); err != nil {
  160. t.Errorf("unexpected error: %s", err)
  161. }
  162. // Test collecting another map item
  163. m = map[string]interface{}{"key": "value2"}
  164. if err := sink.Collect(ctx, m); err != nil {
  165. t.Errorf("unexpected error: %s", err)
  166. }
  167. if err = sink.Close(ctx); err != nil {
  168. t.Errorf("unexpected close error: %s", err)
  169. }
  170. // Read the contents of the temporary file and check if they match the collected items
  171. contents, err := os.ReadFile(tmpfile.Name())
  172. if err != nil {
  173. t.Fatal(err)
  174. }
  175. if !reflect.DeepEqual(contents, tt.content) {
  176. t.Errorf("expected %q but got %q", tt.content, string(contents))
  177. }
  178. })
  179. }
  180. }