Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#36 support sheet format version2 #43

Merged
merged 3 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 38 additions & 2 deletions exceltesting.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (e *exceltesing) LoadWithContext(ctx context.Context, r LoadRequest) error
return fmt.Errorf("exceltesing: excelize.OpenFile: %w", err)
}
defer f.Close()

for _, sheet := range f.GetSheetList() {
if slices.Contains(r.IgnoreSheet, sheet) {
continue
Expand Down Expand Up @@ -290,8 +291,15 @@ type DumpRequest struct {
}

func (e *exceltesing) loadExcelSheet(f *excelize.File, targetSheet string) (*table, error) {
const tableNmCell = "A2"
const columnDefineRowNum = 9
var (
tableNmCell = "A2"
columnDefineRowNum = 9
)

formatVersion := extractSheetFormatVersion(f, targetSheet)
if formatVersion == "2.0" {
columnDefineRowNum = 6
}

tableNm, err := f.GetCellValue(targetSheet, tableNmCell)
if err != nil {
Expand Down Expand Up @@ -520,3 +528,31 @@ func convert(vs [][]any, columns []string) [][]x {
}
return resp
}

// extractSheetFormatVersion is extracting exceltesting sheet format version.
// default 1.0
func extractSheetFormatVersion(f *excelize.File, sheet string) string {
index := f.GetSheetIndex(sheet)
if index == -1 {
return "1.0"
}

rows, err := f.GetRows(sheet)
if err != nil {
return "1.0"
}
if len(rows) < 3 {
return "1.0"
}

row := rows[2] // 3行目に記載があるとする
if len(row) < 2 {
return "1.0"
}

if strings.TrimSpace(strings.ToLower(row[0])) == "version" {
return strings.TrimSpace(row[1])
}

return "1.0"
}
55 changes: 55 additions & 0 deletions exceltesting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,44 @@ func Test_exceltesing_Load(t *testing.T) {
},
},
},
{
name: "inserted excel data using sheet format version2",
r: LoadRequest{
TargetBookPath: filepath.Join("testdata", "load_v2.xlsx"),
SheetPrefix: "normal-",
IgnoreSheet: nil,
},
want: []testX{
{
ID: "test1",
A: true,
B: []byte("bytea"),
C: "a",
D: time.Date(2022, 1, 1, 0, 0, 0, 0, time.UTC),
E: 0.1,
F: 0.01,
G: pgtype.JSON{Bytes: []uint8("{}"), Status: pgtype.Present},
H: pgtype.JSONB{Bytes: []uint8("{}"), Status: pgtype.Present},
I: pgtype.Inet{IPNet: &net.IPNet{IP: net.ParseIP("0.0.0.0"), Mask: net.IPv4Mask(255, 255, 255, 255)}, Status: pgtype.Present},
J: 32767,
K: 2147483647,
L: 9223372036854775807,
M: "00:00:01",
N: 11111,
O: 0,
P: "test",
Q: "01:02:03",
S: time.Date(2022, 1, 1, 1, 2, 3, 0, time.UTC),
T: time.Date(2022, 1, 1, 1, 2, 3, 0, jst),
U: "cee0db76-d69c-4ae3-ae33-5b5970adde48",
V: "abc",
W: 1,
X: 1,
Y: 1,
Z: 1,
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -160,6 +198,23 @@ func Test_exceltesing_Compare(t *testing.T) {
wantFile: filepath.Join("testdata", "compare.xlsx"),
equal: true,
},
{
name: "equal on exceltesing version 2.0 sheet",
input: func(t *testing.T) {
t.Helper()
tdb := testonly.OpenTestDB(t)
defer tdb.Close()
if _, err := tdb.Exec(`TRUNCATE company;`); err != nil {
t.Fatal(err)
}
if _, err := tdb.Exec(`INSERT INTO company (company_cd,company_name,founded_year,created_at,updated_at,revision)
VALUES ('00001','Future',1989,current_timestamp,current_timestamp,1),('00002','YDC',1972,current_timestamp,current_timestamp,1);`); err != nil {
t.Fatal(err)
}
},
wantFile: filepath.Join("testdata", "compare_v2.xlsx"),
equal: true,
},
{
name: "diff",
input: func(t *testing.T) {
Expand Down
Binary file added testdata/compare_v2.xlsx
Binary file not shown.
Binary file added testdata/load_v2.xlsx
Binary file not shown.