-
Notifications
You must be signed in to change notification settings - Fork 4
/
exceltesting_raw_test.go
87 lines (78 loc) · 2.26 KB
/
exceltesting_raw_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
package exceltesting
import (
"github.com/future-architect/go-exceltesting/testonly"
"path/filepath"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestLoadRaw(t *testing.T) {
conn := testonly.OpenTestDB(t)
t.Cleanup(func() { conn.Close() })
testonly.ExecSQLFile(t, conn, filepath.Join("testdata", "schema", "ddl.sql"))
type company struct {
companyCD string
companyName string
foundedYear int
}
tests := []struct {
name string
r LoadRawRequest
want []company
wantErr bool
}{
{
name: "inserted data",
r: LoadRawRequest{
TableName: "company",
Columns: []string{"company_cd", "company_name", "founded_year", "created_at", "updated_at", "revision"},
Values: [][]string{
{"00001", "Future", "1989", "current_timestamp", "current_timestamp", "1"},
{"00002", "YDC", "1972", "current_timestamp", "current_timestamp", "1"},
},
},
want: []company{
{companyCD: "00001", companyName: "Future", foundedYear: 1989},
{companyCD: "00002", companyName: "YDC", foundedYear: 1972},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if _, err := conn.Exec("TRUNCATE company;"); err != nil {
t.Fatalf("truncate company: %v", err)
}
tx, err := conn.Begin()
if err != nil {
t.Fatalf("start transaction: %v", err)
}
if err := LoadRaw(tx, tt.r); (err != nil) != tt.wantErr {
t.Errorf("LoadRaw() error = %v, wantErr %v", err, tt.wantErr)
}
rows, err := tx.Query("SELECT company_cd, company_name, founded_year FROM company ORDER BY company_cd;")
if err != nil {
t.Errorf("failed to query: %v", err)
}
defer rows.Close()
var got []company
for rows.Next() {
var companyCD, companyName string
var foundedYear int
if err := rows.Scan(&companyCD, &companyName, &foundedYear); err != nil {
t.Errorf("failed to scan: %v", err)
}
got = append(got, company{
companyCD: companyCD,
companyName: companyName,
foundedYear: foundedYear,
})
}
if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(company{})); diff != "" {
t.Errorf("got columns for table(company) mismatch (-want +got):\n%s", diff)
}
if err := tx.Rollback(); err != nil {
t.Fatalf("rollback: %v", err)
}
})
}
}