file_source_test.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2022-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. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/io/mock"
  18. "github.com/lf-edge/ekuiper/pkg/api"
  19. "io"
  20. "os"
  21. "path/filepath"
  22. "testing"
  23. "time"
  24. )
  25. func TestJsonFile(t *testing.T) {
  26. path, err := os.Getwd()
  27. if err != nil {
  28. t.Fatal(err)
  29. }
  30. meta := map[string]interface{}{
  31. "file": filepath.Join(path, "test", "test.json"),
  32. }
  33. exp := []api.SourceTuple{
  34. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(1), "name": "John Doe"}, meta),
  35. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(2), "name": "Jane Doe"}, meta),
  36. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(3), "name": "John Smith"}, meta),
  37. }
  38. p := map[string]interface{}{
  39. "path": filepath.Join(path, "test"),
  40. }
  41. r := &FileSource{}
  42. err = r.Configure("test.json", p)
  43. if err != nil {
  44. t.Errorf(err.Error())
  45. return
  46. }
  47. mock.TestSourceOpen(r, exp, t)
  48. }
  49. func TestJsonFolder(t *testing.T) {
  50. path, err := os.Getwd()
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. moveToFolder := filepath.Join(path, "test", "moveTo")
  55. exp := []api.SourceTuple{
  56. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(1), "name": "John Doe", "height": 1.82}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f1.json")}),
  57. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(2), "name": "Jane Doe", "height": 1.65}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f1.json")}),
  58. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(3), "name": "Will Doe", "height": 1.76}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f2.json")}),
  59. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(4), "name": "Dude Doe", "height": 1.92}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f3.json")}),
  60. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(5), "name": "Jane Doe", "height": 1.72}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f3.json")}),
  61. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(6), "name": "John Smith", "height": 2.22}, map[string]interface{}{"file": filepath.Join(path, "test", "json", "f3.json")}),
  62. }
  63. p := map[string]interface{}{
  64. "path": filepath.Join(path, "test"),
  65. "actionAfterRead": 2,
  66. "moveTo": moveToFolder,
  67. }
  68. r := &FileSource{}
  69. err = r.Configure("json", p)
  70. if err != nil {
  71. t.Errorf(err.Error())
  72. return
  73. }
  74. mock.TestSourceOpen(r, exp, t)
  75. // wait for the move to finish
  76. time.Sleep(100 * time.Millisecond)
  77. files, err := os.ReadDir(moveToFolder)
  78. if err != nil {
  79. t.Error(err)
  80. }
  81. if len(files) != 3 {
  82. t.Errorf("expect 3 files in moveTo folder, but got %d", len(files))
  83. }
  84. for _, f := range files {
  85. os.Rename(filepath.Join(moveToFolder, f.Name()), filepath.Join(path, "test", "json", f.Name()))
  86. }
  87. }
  88. func TestCSVFolder(t *testing.T) {
  89. // Move test files to temp folder
  90. path, err := os.Getwd()
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. testFolder := filepath.Join(path, "test", "csvTemp")
  95. err = os.MkdirAll(testFolder, 0755)
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. files, err := os.ReadDir(filepath.Join(path, "test", "csv"))
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. for _, f := range files {
  104. err = copy(filepath.Join(path, "test", "csv", f.Name()), filepath.Join(testFolder, f.Name()))
  105. if err != nil {
  106. t.Fatal(err)
  107. }
  108. }
  109. // Start testing
  110. exp := []api.SourceTuple{
  111. api.NewDefaultSourceTuple(map[string]interface{}{"@": "#", "id": "1", "ts": "1670170500", "value": "161.927872"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "a.csv")}),
  112. api.NewDefaultSourceTuple(map[string]interface{}{"@": "#", "id": "2", "ts": "1670170900", "value": "176"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "a.csv")}),
  113. api.NewDefaultSourceTuple(map[string]interface{}{"id": "33", "ts": "1670270500", "humidity": "89"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "b.csv")}),
  114. api.NewDefaultSourceTuple(map[string]interface{}{"id": "44", "ts": "1670270900", "humidity": "76"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "b.csv")}),
  115. }
  116. p := map[string]interface{}{
  117. "fileType": "csv",
  118. "path": filepath.Join(path, "test"),
  119. "actionAfterRead": 1,
  120. "hasHeader": true,
  121. "delimiter": "\t",
  122. "ignoreStartLines": 3,
  123. "ignoreEndLines": 1,
  124. }
  125. r := &FileSource{}
  126. err = r.Configure("csvTemp", p)
  127. if err != nil {
  128. t.Errorf(err.Error())
  129. return
  130. }
  131. mock.TestSourceOpen(r, exp, t)
  132. // wait for file deleted takes effect
  133. time.Sleep(100 * time.Millisecond)
  134. files, err = os.ReadDir(testFolder)
  135. if err != nil {
  136. t.Error(err)
  137. }
  138. if len(files) != 0 {
  139. t.Errorf("expect 0 files in csvTemp folder, but got %d", len(files))
  140. }
  141. }
  142. func copy(src, dst string) error {
  143. sourceFileStat, err := os.Stat(src)
  144. if err != nil {
  145. return err
  146. }
  147. if !sourceFileStat.Mode().IsRegular() {
  148. return fmt.Errorf("%s is not a regular file", src)
  149. }
  150. source, err := os.Open(src)
  151. if err != nil {
  152. return err
  153. }
  154. defer source.Close()
  155. destination, err := os.Create(dst)
  156. if err != nil {
  157. return err
  158. }
  159. defer destination.Close()
  160. _, err = io.Copy(destination, source)
  161. return err
  162. }
  163. func TestCSVFile(t *testing.T) {
  164. path, err := os.Getwd()
  165. if err != nil {
  166. t.Fatal(err)
  167. }
  168. exp := []api.SourceTuple{
  169. api.NewDefaultSourceTuple(map[string]interface{}{"ns": "@", "id": "id", "ts": "ts", "number": "value"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}),
  170. api.NewDefaultSourceTuple(map[string]interface{}{"ns": "#", "id": "1", "ts": "1670170500", "number": "161.927872"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}),
  171. api.NewDefaultSourceTuple(map[string]interface{}{"ns": "#", "id": "2", "ts": "1670170900", "number": "176"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}),
  172. }
  173. p := map[string]interface{}{
  174. "fileType": "csv",
  175. "path": filepath.Join(path, "test", "csv"),
  176. "delimiter": "\t",
  177. "ignoreStartLines": 3,
  178. "ignoreEndLines": 1,
  179. "columns": []string{"ns", "id", "ts", "number"},
  180. }
  181. r := &FileSource{}
  182. err = r.Configure("a.csv", p)
  183. if err != nil {
  184. t.Errorf(err.Error())
  185. return
  186. }
  187. mock.TestSourceOpen(r, exp, t)
  188. }
  189. func TestJsonLines(t *testing.T) {
  190. path, err := os.Getwd()
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. meta := map[string]interface{}{
  195. "file": filepath.Join(path, "test", "test.lines"),
  196. }
  197. exp := []api.SourceTuple{
  198. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(1), "name": "John Doe"}, meta),
  199. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(2), "name": "Jane Doe"}, meta),
  200. api.NewDefaultSourceTuple(map[string]interface{}{"id": float64(3), "name": "John Smith"}, meta),
  201. }
  202. p := map[string]interface{}{
  203. "path": filepath.Join(path, "test"),
  204. "fileType": "lines",
  205. }
  206. r := &FileSource{}
  207. err = r.Configure("test.lines", p)
  208. if err != nil {
  209. t.Errorf(err.Error())
  210. return
  211. }
  212. mock.TestSourceOpen(r, exp, t)
  213. }