file_source_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 source
  15. import (
  16. "fmt"
  17. "github.com/lf-edge/ekuiper/internal/topo/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. files, err := os.ReadDir(moveToFolder)
  76. if err != nil {
  77. t.Error(err)
  78. }
  79. if len(files) != 3 {
  80. t.Errorf("expect 3 files in moveTo folder, but got %d", len(files))
  81. }
  82. for _, f := range files {
  83. os.Rename(filepath.Join(moveToFolder, f.Name()), filepath.Join(path, "test", "json", f.Name()))
  84. }
  85. }
  86. func TestCSVFolder(t *testing.T) {
  87. // Move test files to temp folder
  88. path, err := os.Getwd()
  89. if err != nil {
  90. t.Fatal(err)
  91. }
  92. testFolder := filepath.Join(path, "test", "csvTemp")
  93. err = os.MkdirAll(testFolder, 0755)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. files, err := os.ReadDir(filepath.Join(path, "test", "csv"))
  98. if err != nil {
  99. t.Fatal(err)
  100. }
  101. for _, f := range files {
  102. err = copy(filepath.Join(path, "test", "csv", f.Name()), filepath.Join(testFolder, f.Name()))
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. }
  107. // Start testing
  108. exp := []api.SourceTuple{
  109. api.NewDefaultSourceTuple(map[string]interface{}{"@": "#", "id": "1", "ts": "1670170500", "value": "161.927872"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "a.csv")}),
  110. api.NewDefaultSourceTuple(map[string]interface{}{"@": "#", "id": "2", "ts": "1670170900", "value": "176"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "a.csv")}),
  111. api.NewDefaultSourceTuple(map[string]interface{}{"id": "33", "ts": "1670270500", "humidity": "89"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "b.csv")}),
  112. api.NewDefaultSourceTuple(map[string]interface{}{"id": "44", "ts": "1670270900", "humidity": "76"}, map[string]interface{}{"file": filepath.Join(path, "test", "csvTemp", "b.csv")}),
  113. }
  114. p := map[string]interface{}{
  115. "fileType": "csv",
  116. "path": filepath.Join(path, "test"),
  117. "actionAfterRead": 1,
  118. "hasHeader": true,
  119. "delimiter": "\t",
  120. "ignoreStartLines": 3,
  121. "ignoreEndLines": 1,
  122. }
  123. r := &FileSource{}
  124. err = r.Configure("csvTemp", p)
  125. if err != nil {
  126. t.Errorf(err.Error())
  127. return
  128. }
  129. mock.TestSourceOpen(r, exp, t)
  130. // wait for file deleted takes effect
  131. time.Sleep(100 * time.Millisecond)
  132. files, err = os.ReadDir(testFolder)
  133. if err != nil {
  134. t.Error(err)
  135. }
  136. if len(files) != 0 {
  137. t.Errorf("expect 0 files in csvTemp folder, but got %d", len(files))
  138. }
  139. }
  140. func copy(src, dst string) error {
  141. sourceFileStat, err := os.Stat(src)
  142. if err != nil {
  143. return err
  144. }
  145. if !sourceFileStat.Mode().IsRegular() {
  146. return fmt.Errorf("%s is not a regular file", src)
  147. }
  148. source, err := os.Open(src)
  149. if err != nil {
  150. return err
  151. }
  152. defer source.Close()
  153. destination, err := os.Create(dst)
  154. if err != nil {
  155. return err
  156. }
  157. defer destination.Close()
  158. _, err = io.Copy(destination, source)
  159. return err
  160. }
  161. func TestCSVFile(t *testing.T) {
  162. path, err := os.Getwd()
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. exp := []api.SourceTuple{
  167. api.NewDefaultSourceTuple(map[string]interface{}{"ns": "@", "id": "id", "ts": "ts", "number": "value"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}),
  168. api.NewDefaultSourceTuple(map[string]interface{}{"ns": "#", "id": "1", "ts": "1670170500", "number": "161.927872"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}),
  169. api.NewDefaultSourceTuple(map[string]interface{}{"ns": "#", "id": "2", "ts": "1670170900", "number": "176"}, map[string]interface{}{"file": filepath.Join(path, "test", "csv", "a.csv")}),
  170. }
  171. p := map[string]interface{}{
  172. "fileType": "csv",
  173. "path": filepath.Join(path, "test", "csv"),
  174. "delimiter": "\t",
  175. "ignoreStartLines": 3,
  176. "ignoreEndLines": 1,
  177. "columns": []string{"ns", "id", "ts", "number"},
  178. }
  179. r := &FileSource{}
  180. err = r.Configure("a.csv", p)
  181. if err != nil {
  182. t.Errorf(err.Error())
  183. return
  184. }
  185. mock.TestSourceOpen(r, exp, t)
  186. }