Add test for account balances
This commit is contained in:
parent
a46ddded67
commit
880ed731f8
@ -88,7 +88,73 @@ func TestMain(t *testing.T) {
|
|||||||
|
|
||||||
AssertCategoriesAndAvailableEqual(ctx, t, loc, handler, budget)
|
AssertCategoriesAndAvailableEqual(ctx, t, loc, handler, budget)
|
||||||
|
|
||||||
// check accounts
|
AssertAccountsEqual(ctx, t, handler, budget)
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssertAccountsEqual(ctx context.Context, t *testing.T, handler *Handler, budget *postgres.Budget) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
t.Run("account balances", func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
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]
|
||||||
|
if name != "accounts" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
testCaseFile := filepath.Join(resultDir, file.Name())
|
||||||
|
handler.CheckAccountBalance(ctx, t, testCaseFile, budget)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h Handler) CheckAccountBalance(ctx context.Context, t *testing.T, testCaseFile string, budget *postgres.Budget) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
accounts, err := h.Service.GetAccountsWithBalance(ctx, budget.ID)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("get accounts: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
testDataFile, err := os.Open(testCaseFile)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not load category test data: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var testData map[string]float64
|
||||||
|
dec := json.NewDecoder(testDataFile)
|
||||||
|
err = dec.Decode(&testData)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("could not decode category test data: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
for accountName, accountBalance := range testData {
|
||||||
|
found := false
|
||||||
|
for _, account := range accounts {
|
||||||
|
if account.Name == accountName {
|
||||||
|
assertEqual(t, accountBalance, account.WorkingBalance.GetFloat64(), "balance for "+accountName)
|
||||||
|
found = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !found {
|
||||||
|
t.Errorf("account " + accountName + " was not found in result")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func AssertCategoriesAndAvailableEqual(ctx context.Context, t *testing.T, loc *time.Location, handler *Handler, budget *postgres.Budget) {
|
func AssertCategoriesAndAvailableEqual(ctx context.Context, t *testing.T, loc *time.Location, handler *Handler, budget *postgres.Budget) {
|
||||||
@ -110,6 +176,9 @@ func AssertCategoriesAndAvailableEqual(ctx context.Context, t *testing.T, loc *t
|
|||||||
|
|
||||||
name := file.Name()[0 : len(file.Name())-5]
|
name := file.Name()[0 : len(file.Name())-5]
|
||||||
parts := strings.Split(name, "-")
|
parts := strings.Split(name, "-")
|
||||||
|
if len(parts) != 2 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
year, _ := strconv.Atoi(parts[0])
|
year, _ := strconv.Atoi(parts[0])
|
||||||
month, _ := strconv.Atoi(parts[1])
|
month, _ := strconv.Atoi(parts[1])
|
||||||
first := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc)
|
first := time.Date(year, time.Month(month), 1, 0, 0, 0, 0, loc)
|
||||||
|
2
testdata
2
testdata
@ -1 +1 @@
|
|||||||
Subproject commit b6eda7681f92c2c635555c392660cb69bec2a1ed
|
Subproject commit 6ca3adcee2713e8205133bec6c24b45aa8d730d9
|
Loading…
x
Reference in New Issue
Block a user