diff --git a/internal/handlers/reports.go b/internal/handlers/reports.go index 7ddbb4e..985ff07 100644 --- a/internal/handlers/reports.go +++ b/internal/handlers/reports.go @@ -58,6 +58,11 @@ func (rl *ReportList) Write(w http.ResponseWriter) error { return enc.Encode(rl) } +func (rl *ReportList) Read(json_str string) error { + dec := json.NewDecoder(strings.NewReader(json_str)) + return dec.Decode(rl) +} + type Series struct { Values []float64 Series map[string]*Series diff --git a/internal/handlers/reports_test.go b/internal/handlers/reports_test.go new file mode 100644 index 0000000..3b86930 --- /dev/null +++ b/internal/handlers/reports_test.go @@ -0,0 +1,68 @@ +package handlers_test + +import ( + "github.com/aclindsa/moneygo/internal/handlers" + "net/http" + "strconv" + "testing" +) + +func createReport(client *http.Client, report *handlers.Report) (*handlers.Report, error) { + var r handlers.Report + err := create(client, report, &r, "/report/", "report") + return &r, err +} + +func getReport(client *http.Client, reportid int64) (*handlers.Report, error) { + var r handlers.Report + err := read(client, &r, "/report/"+strconv.FormatInt(reportid, 10), "report") + if err != nil { + return nil, err + } + return &r, nil +} + +func getReports(client *http.Client) (*handlers.ReportList, error) { + var rl handlers.ReportList + err := read(client, &rl, "/report/", "reports") + if err != nil { + return nil, err + } + return &rl, nil +} + +func updateReport(client *http.Client, report *handlers.Report) (*handlers.Report, error) { + var r handlers.Report + err := update(client, report, &r, "/report/"+strconv.FormatInt(report.ReportId, 10), "report") + if err != nil { + return nil, err + } + return &r, nil +} + +func deleteReport(client *http.Client, r *handlers.Report) error { + err := remove(client, "/report/"+strconv.FormatInt(r.ReportId, 10), "report") + if err != nil { + return err + } + return nil +} + +func TestCreateReport(t *testing.T) { + RunWith(t, &data[0], func(t *testing.T, d *TestData) { + for i := 1; i < len(data[0].reports); i++ { + orig := data[0].reports[i] + r := d.reports[i] + + if r.ReportId == 0 { + t.Errorf("Unable to create report: %+v", r) + } + if r.Name != orig.Name { + t.Errorf("Name doesn't match") + } + if r.Lua != orig.Lua { + t.Errorf("Lua doesn't match") + } + } + }) +} diff --git a/internal/handlers/testdata_test.go b/internal/handlers/testdata_test.go index 62e0666..c505e59 100644 --- a/internal/handlers/testdata_test.go +++ b/internal/handlers/testdata_test.go @@ -117,6 +117,14 @@ func (t *TestData) Initialize() (*TestData, error) { t2.transactions = append(t2.transactions, *tt2) } + for _, report := range t.reports { + r2, err := createReport(t2.clients[report.UserId], &report) + if err != nil { + return nil, err + } + t2.reports = append(t2.reports, *r2) + } + t2.initialized = true return &t2, nil } @@ -310,5 +318,61 @@ var data = []TestData{ }, }, }, + reports: []handlers.Report{ + handlers.Report{ + UserId: 0, + Name: "", + Lua: ` +function account_series_map(accounts, tabulation) + map = {} + + for i=1,100 do -- we're not messing with accounts more than 100 levels deep + all_handled = true + for id, acct in pairs(accounts) do + if not map[id] then + all_handled = false + if not acct.parent then + map[id] = tabulation:series(acct.name) + elseif map[acct.parent.accountid] then + map[id] = map[acct.parent.accountid]:series(acct.name) + end + end + end + if all_handled then + return map + end + end + + error("Accounts nested (at least) 100 levels deep") +end + +function generate() + year = date.now().year + account_type = account.Expense + + accounts = get_accounts() + t = tabulation.new(12) + t:title(year .. " Monthly Expenses") + series_map = account_series_map(accounts, t) + + for month=1,12 do + begin_date = date.new(year, month, 1) + end_date = date.new(year, month+1, 1) + + t:label(month, tostring(begin_date)) + + for id, acct in pairs(accounts) do + series = series_map[id] + if acct.type == account_type then + balance = acct:balance(begin_date, end_date) + series:value(month, balance.amount) + end + end + end + + return t +end`, + }, + }, }, }