amseltools/internal/dirtoexcel/dirtoexcel_test.go

122 lines
2.0 KiB
Go
Raw Normal View History

2023-03-29 15:26:21 +02:00
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 {
2023-04-11 19:15:16 +02:00
name string
files []string
sheets map[string][]want
2023-03-29 15:26:21 +02:00
}{
{
"00",
[]string{
"amazon_foo_12.34_bar.jpg",
"bma-zon_12.34_bar.jpg",
2023-04-11 19:15:16 +02:00
"aldi_10.98.jpg",
"aldi_10.00.jpg",
2023-04-12 10:31:26 +02:00
"edeka_10.11.jpg",
2023-03-29 15:26:21 +02:00
},
2023-04-11 19:15:16 +02:00
map[string][]want{
"Food": {
{
"ALDI",
"20.98",
},
2023-04-12 10:31:26 +02:00
{
"EDEKA",
"10.11",
},
2023-03-29 15:26:21 +02:00
},
2023-04-12 10:31:26 +02:00
"NonFood": {
2023-04-11 19:15:16 +02:00
{
"AMAZON",
"12.34",
},
},
"Other": {
{
"BMA-ZON",
"12.34",
},
2023-03-29 15:26:21 +02:00
},
},
},
}
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)
2023-04-11 19:15:16 +02:00
// NOTE: Uncomment this to view excel file.
//
// eFile, err := os.ReadFile(outfile)
// is.NoErr(err)
//
// err = os.WriteFile(path.Join("/home/marv/wip/amseltools", "foo.xlsx"), eFile, 0o644)
// is.NoErr(err)
2023-03-29 15:26:21 +02:00
// Read the excel.
f, err := excelize.OpenFile(outfile)
is.NoErr(err)
defer func() {
err := f.Close()
is.NoErr(err)
}()
2023-04-11 19:15:16 +02:00
for name, values := range tt.sheets {
for i, j := range values {
company, err := f.GetCellValue(name, fmt.Sprintf("A%d", i+1))
is.NoErr(err)
2023-03-29 15:26:21 +02:00
2023-04-11 19:15:16 +02:00
is.Equal(j.Company, company)
2023-03-29 15:26:21 +02:00
2023-04-11 19:15:16 +02:00
value, err := f.GetCellValue(name, fmt.Sprintf("B%d", i+1))
is.NoErr(err)
2023-03-29 15:26:21 +02:00
2023-04-11 19:15:16 +02:00
is.Equal(j.Value, value)
}
2023-03-29 15:26:21 +02:00
}
})
}
}
2023-04-11 19:15:16 +02:00
func TestCategoriesInsert(t *testing.T) {
is := is.New(t)
m := dirtoexcel.Categories{}
err := m.Insert("amazon", "11.11")
is.NoErr(err)
2023-04-12 10:31:26 +02:00
is.Equal(dirtoexcel.Categories{"NonFood": []dirtoexcel.Value{{"AMAZON", 11.11}}}, m)
2023-04-11 19:15:16 +02:00
}