-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathes_test.go
92 lines (77 loc) · 1.53 KB
/
es_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"os"
"strconv"
"strings"
"testing"
"unicode"
)
type Products struct {
ProductID int `json:"ProductID"`
ProductName string `json:"ProductName"`
ProductType string `json:"ProductType"`
StockStatus string `json:"StockStatus"`
}
func Test_Products(t *testing.T) {
csvFile, err := os.Open(".../scripts/Products.csv")
if err != nil {
log.Fatal(err)
}
defer csvFile.Close()
jsonFile, err := os.Create(".../scripts/Products.json")
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
reader := csv.NewReader(csvFile)
reader.Comma = ','
header, err := reader.Read()
if err != nil {
log.Fatal(err)
}
indexName := "products"
for {
record, err := reader.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
products := Products{}
for i, value := range record {
switch header[i] {
case "ProductID":
products.ProductID, _ = strconv.Atoi(value)
case "ProductName":
products.ProductName = value
case "ProductType":
products.ProductType = value
case "StockStatus":
products.StockStatus = value
}
}
action := map[string]interface{}{
"index": map[string]interface{}{
"_index": indexName,
},
}
jsonData, err := json.Marshal(action)
if err != nil {
log.Fatal(err)
}
jsonFile.Write(jsonData)
jsonFile.WriteString("\n")
jsonData, err = json.Marshal(products)
if err != nil {
log.Fatal(err)
}
jsonFile.Write(jsonData)
jsonFile.WriteString("\n")
}
}