amseltools/internal/dirtoexcel/dirtoexcel_test.go
Marvin Preuss 4e80f25d32
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
ci/woodpecker/tag/woodpecker Pipeline was successful
feat: adds charts for categories
2023-04-12 08:31:26 +00:00

122 lines
2.0 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
sheets map[string][]want
}{
{
"00",
[]string{
"amazon_foo_12.34_bar.jpg",
"bma-zon_12.34_bar.jpg",
"aldi_10.98.jpg",
"aldi_10.00.jpg",
"edeka_10.11.jpg",
},
map[string][]want{
"Food": {
{
"ALDI",
"20.98",
},
{
"EDEKA",
"10.11",
},
},
"NonFood": {
{
"AMAZON",
"12.34",
},
},
"Other": {
{
"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)
// 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)
// Read the excel.
f, err := excelize.OpenFile(outfile)
is.NoErr(err)
defer func() {
err := f.Close()
is.NoErr(err)
}()
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)
is.Equal(j.Company, company)
value, err := f.GetCellValue(name, fmt.Sprintf("B%d", i+1))
is.NoErr(err)
is.Equal(j.Value, value)
}
}
})
}
}
func TestCategoriesInsert(t *testing.T) {
is := is.New(t)
m := dirtoexcel.Categories{}
err := m.Insert("amazon", "11.11")
is.NoErr(err)
is.Equal(dirtoexcel.Categories{"NonFood": []dirtoexcel.Value{{"AMAZON", 11.11}}}, m)
}