85 lines
1.3 KiB
Go
85 lines
1.3 KiB
Go
|
package dirtoexcel_test
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"path"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/matryer/is"
|
||
|
"github.com/xuri/excelize/v2"
|
||
|
"go.xsfx.dev/amseltools/internal/dirtoexcel"
|
||
|
)
|
||
|
|
||
|
func TestCreate(t *testing.T) {
|
||
|
type want struct {
|
||
|
Company string
|
||
|
Value string
|
||
|
}
|
||
|
|
||
|
tables := []struct {
|
||
|
name string
|
||
|
files []string
|
||
|
want []want
|
||
|
}{
|
||
|
{
|
||
|
"00",
|
||
|
[]string{
|
||
|
"amazon_foo_12.34_bar.jpg",
|
||
|
"bma-zon_12.34_bar.jpg",
|
||
|
},
|
||
|
[]want{
|
||
|
{
|
||
|
"amazon",
|
||
|
"12.34",
|
||
|
},
|
||
|
{
|
||
|
"bma-zon",
|
||
|
"12.34",
|
||
|
},
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
is := is.New(t)
|
||
|
|
||
|
for _, tt := range tables {
|
||
|
tt := tt
|
||
|
|
||
|
t.Run(tt.name, func(t *testing.T) {
|
||
|
dir := t.TempDir()
|
||
|
for _, i := range tt.files {
|
||
|
_, err := os.Create(path.Join(dir, i))
|
||
|
is.NoErr(err)
|
||
|
}
|
||
|
|
||
|
outfile := path.Join(dir, "foo.xlsx")
|
||
|
|
||
|
// Create the excel.
|
||
|
err := dirtoexcel.Create(outfile, dir)
|
||
|
is.NoErr(err)
|
||
|
|
||
|
// Read the excel.
|
||
|
f, err := excelize.OpenFile(outfile)
|
||
|
is.NoErr(err)
|
||
|
|
||
|
defer func() {
|
||
|
err := f.Close()
|
||
|
is.NoErr(err)
|
||
|
}()
|
||
|
|
||
|
for i := range tt.files {
|
||
|
company, err := f.GetCellValue("sheet1", fmt.Sprintf("A%d", i+1))
|
||
|
is.NoErr(err)
|
||
|
|
||
|
is.Equal(tt.want[i].Company, company)
|
||
|
|
||
|
value, err := f.GetCellValue("sheet1", fmt.Sprintf("B%d", i+1))
|
||
|
is.NoErr(err)
|
||
|
|
||
|
is.Equal(tt.want[i].Value, value)
|
||
|
}
|
||
|
})
|
||
|
}
|
||
|
}
|