Actually use files

This commit is contained in:
2022-04-05 20:33:04 +00:00
parent 9471c5b63b
commit bd99f58ab4
3 changed files with 38 additions and 12 deletions

View File

@ -8,6 +8,9 @@ import (
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"testing"
"time"
@ -82,8 +85,33 @@ func TestMain(t *testing.T) {
handler.DoYNABImport(ctx, t, budget)
// check available balance for more dates
handler.CheckAvailableBalance(ctx, t, budget)
loc := time.Now().Location()
resultDir := "../testdata/production-export/results"
files, err := os.ReadDir(resultDir)
if err != nil {
t.Errorf("could not load results: %s", err)
return
}
for _, file := range files {
if file.IsDir() {
continue
}
name := file.Name()[0 : len(file.Name())-5]
parts := strings.Split(name, "-")
year, _ := strconv.Atoi(parts[1])
month, _ := strconv.Atoi(parts[2])
first := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc)
switch parts[0] {
case "categories":
t.Logf("Checking balances for %s", first.Format("2006-01"))
testCaseFile := filepath.Join(resultDir, file.Name())
handler.CheckAvailableBalance(ctx, t, testCaseFile, budget, first)
}
}
// check categories
@ -96,12 +124,10 @@ type CategoryTestData struct {
Assigned float64
}
func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget *postgres.Budget) {
func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, testCaseFile string, budget *postgres.Budget, first time.Time) {
t.Helper()
loc := time.Now().Location()
first := time.Date(2021, 12, 1, 0, 0, 0, 0, loc)
firstOfNextMonth := time.Date(2022, 1, 1, 0, 0, 0, 0, loc)
data, err := h.prepareBudgeting(ctx, *budget, firstOfNextMonth, first)
data, err := h.prepareBudgeting(ctx, *budget, first)
if err != nil {
t.Errorf("prepare budgeting: %s", err)
return
@ -110,7 +136,7 @@ func (h Handler) CheckAvailableBalance(ctx context.Context, t *testing.T, budget
// 2022-01 assert_equal(t, -115170.56, data.AvailableBalance.GetFloat64(), "available balance")
assertEqual(t, -110181.600, data.AvailableBalance.GetFloat64(), "available balance")
categoryTestDataFile, err := os.Open("../testdata/production-export/results/categories-2021-12.json")
categoryTestDataFile, err := os.Open(testCaseFile)
if err != nil {
t.Errorf("could not load category test data: %s", err)
return