-
Notifications
You must be signed in to change notification settings - Fork 4
/
example_test.go
75 lines (60 loc) · 1.26 KB
/
example_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
package exceltesting_test
import (
"database/sql"
"encoding/csv"
"os"
"path/filepath"
"testing"
"github.com/future-architect/go-exceltesting"
_ "github.com/jackc/pgx/v4/stdlib"
)
var conn *sql.DB
func TestMain(m *testing.M) {
uri := "postgres://excellocal:password@localhost:15432/excellocal"
var err error
conn, err = sql.Open("pgx", uri)
if err != nil {
os.Exit(1)
}
defer conn.Close()
m.Run()
}
func TestExample_Load(t *testing.T) {
if _, err := conn.Exec("TRUNCATE company;"); err != nil {
t.Fatal(err)
}
e := exceltesting.New(conn)
e.Load(t, exceltesting.LoadRequest{
TargetBookPath: filepath.Join("testdata", "load_example.xlsx"),
SheetPrefix: "",
IgnoreSheet: nil,
})
}
func TestExample_LoadRawFromCSV(t *testing.T) {
if _, err := conn.Exec("TRUNCATE company;"); err != nil {
t.Fatal(err)
}
f, err := os.Open(filepath.Join("testdata", "sample.csv"))
if err != nil {
t.Fatal(err)
}
defer f.Close()
cr := csv.NewReader(f)
rs, err := cr.ReadAll()
if err != nil {
t.Fatal(err)
}
tx, err := conn.Begin()
if err != nil {
t.Fatal(err)
}
err = exceltesting.LoadRaw(tx, exceltesting.LoadRawRequest{
TableName: "company",
Columns: rs[0],
Values: rs[1:],
})
if err != nil {
t.Fatal(err)
}
tx.Rollback()
}