Skip to content

Commit 3bfdbbb

Browse files
committed
test: improve tests for YNAB 4 import
1 parent a9ab49c commit 3bfdbbb

16 files changed

+7850
-1584
lines changed

pkg/controllers/import_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ func (suite *TestSuiteStandard) TestImportFails() {
5959
suite.Assert().Contains(test.DecodeError(suite.T(), recorder.Body.Bytes()), "You must send a file to this endpoint")
6060

6161
// Wrong file name
62-
body, headers := suite.loadTestFile("wrong-name.json")
62+
body, headers := suite.loadTestFile("importer/wrong-name.json")
6363
recorder = test.Request(suite.controller, suite.T(), http.MethodPost, "http://example.com/v1/import?budgetName=same", body, headers)
6464
suite.Assert().Equal(http.StatusBadRequest, recorder.Code, "Request ID %s, response %s", recorder.Header().Get("x-request-id"), recorder.Body.String())
6565
suite.Assert().Contains(test.DecodeError(suite.T(), recorder.Body.Bytes()), "If you tried to upload a YNAB 4 budget, make sure its file name ends with .yfull")
6666

6767
// Empty file
68-
body, headers = suite.loadTestFile("EmptyFile.yfull")
68+
body, headers = suite.loadTestFile("importer/EmptyFile.yfull")
6969
recorder = test.Request(suite.controller, suite.T(), http.MethodPost, "http://example.com/v1/import?budgetName=same", body, headers)
7070
suite.Assert().Equal(http.StatusBadRequest, recorder.Code, "Request ID %s, response %s", recorder.Header().Get("x-request-id"), recorder.Body.String())
7171
suite.Assert().Contains(test.DecodeError(suite.T(), recorder.Body.Bytes()), "not a valid YNAB4 Budget.yfull file: unexpected end of JSON input")
@@ -85,7 +85,7 @@ func (suite *TestSuiteStandard) TestImportFails() {
8585

8686
func (suite *TestSuiteStandard) TestImport() {
8787
// Import one
88-
body, headers := suite.loadTestFile("Budget.yfull")
88+
body, headers := suite.loadTestFile("importer/Budget.yfull")
8989
recorder := test.Request(suite.controller, suite.T(), http.MethodPost, "http://example.com/v1/import?budgetName=Test Budget", body, headers)
9090
suite.Assert().Equal(http.StatusCreated, recorder.Code, "Request ID %s, response %s", recorder.Header().Get("x-request-id"), recorder.Body.String())
9191
}

pkg/importer/parser/ynab4/parse.go

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package ynab4
22

33
import (
44
"encoding/json"
5-
"errors"
65
"fmt"
76
"io"
87
"regexp"
@@ -41,16 +40,9 @@ func Parse(f io.Reader) (types.ParsedResources, error) {
4140
},
4241
}
4342

44-
// Add all accounts
45-
accountIDNames, err := parseAccounts(&resources, budget.Accounts)
46-
if err != nil {
47-
return types.ParsedResources{}, fmt.Errorf("error parsing accounts: %w", err)
48-
}
49-
50-
payeeIDNames, err := parsePayees(&resources, budget.Payees)
51-
if err != nil {
52-
return types.ParsedResources{}, fmt.Errorf("error parsing payees: %w", err)
53-
}
43+
// Parse accounts and payees
44+
accountIDNames := parseAccounts(&resources, budget.Accounts)
45+
payeeIDNames := parsePayees(&resources, budget.Payees)
5446

5547
// Copy all payee mappings to the account mappings as for Envelope Zero, both are accounts
5648
maps.Copy(accountIDNames, payeeIDNames)
@@ -91,7 +83,7 @@ func parseHiddenCategoryName(f string) (category, envelope string, err error) {
9183
return
9284
}
9385

94-
func parseAccounts(resources *types.ParsedResources, accounts []Account) (IDToName, error) {
86+
func parseAccounts(resources *types.ParsedResources, accounts []Account) IDToName {
9587
idToNames := make(IDToName)
9688

9789
resources.Accounts = make(map[string]types.Account)
@@ -109,10 +101,10 @@ func parseAccounts(resources *types.ParsedResources, accounts []Account) (IDToNa
109101
}
110102
}
111103

112-
return idToNames, nil
104+
return idToNames
113105
}
114106

115-
func parsePayees(resources *types.ParsedResources, payees []Payee) (IDToName, error) {
107+
func parsePayees(resources *types.ParsedResources, payees []Payee) IDToName {
116108
idToNames := make(IDToName)
117109

118110
// Payees in YNAB 4 map to External Accounts in Envelope Zero
@@ -139,7 +131,7 @@ func parsePayees(resources *types.ParsedResources, payees []Payee) (IDToName, er
139131
}
140132
}
141133

142-
return idToNames, nil
134+
return idToNames
143135
}
144136

145137
func parseCategories(resources *types.ParsedResources, categories []Category) (IDToEnvelopes, error) {
@@ -196,7 +188,7 @@ func parseCategories(resources *types.ParsedResources, categories []Category) (I
196188
var err error
197189
mapping.Category, mapping.Envelope, err = parseHiddenCategoryName(mapping.Envelope)
198190
if err != nil {
199-
return IDToEnvelopes{}, fmt.Errorf("hidden category could not be parsed: %w", err)
191+
return IDToEnvelopes{}, fmt.Errorf("hidden category could not be parsed, your Budget.yfull file seems to be corrupted: %w", err)
200192
}
201193

202194
hidden = true
@@ -224,15 +216,10 @@ func parseCategories(resources *types.ParsedResources, categories []Category) (I
224216

225217
// Add all envelopes, adding categories as needed
226218
for _, envelope := range tEnvelopes {
227-
category, ok := tCategories[envelope.Category]
228-
if !ok {
229-
return IDToEnvelopes{}, errors.New("an envelope referenced a non-existing category. Your Budget.yfull file seems to be inconsistent")
230-
}
231-
232219
// Check if the category already exists in the resources. If not, create it
233-
_, ok = resources.Categories[envelope.Category]
220+
_, ok := resources.Categories[envelope.Category]
234221
if !ok {
235-
resources.Categories[envelope.Category] = category
222+
resources.Categories[envelope.Category] = tCategories[envelope.Category]
236223
}
237224

238225
resources.Categories[envelope.Category].Envelopes[envelope.Envelope.Model.Name] = envelope.Envelope
@@ -389,7 +376,7 @@ func parseMonthlyBudgets(resources *types.ParsedResources, monthlyBudgets []Mont
389376
for _, monthBudget := range monthlyBudgets {
390377
month, err := internal_types.ParseMonth(monthBudget.Month)
391378
if err != nil {
392-
return fmt.Errorf("could not parse date: %w", err)
379+
return fmt.Errorf("could not parse date, the Budget.yfull file seems to be corrupt: %w", err)
393380
}
394381

395382
for _, subCategoryBudget := range monthBudget.MonthlySubCategoryBudgets {

0 commit comments

Comments
 (0)