better end points and better tests

This commit is contained in:
samantha42
2026-03-21 15:47:40 +01:00
parent 3f203178b2
commit 3490dd13d4
10 changed files with 482 additions and 34 deletions

View File

@@ -131,3 +131,38 @@ func DecodeJSON(t *testing.T, w *httptest.ResponseRecorder, dst any) {
t.Fatalf("DecodeJSON: %v\n\tbody: %s", err, w.Body.String())
}
}
// SeedFixtures inserts one department and one GL account into db and returns
// their IDs. Call this at the top of any test that needs FK-valid actuals or
// budget rows.
//
// Usage:
//
// deptID, glID := testutil.SeedFixtures(t, db)
func SeedFixtures(t *testing.T, db *sql.DB) (deptID int, glAccountID int) {
t.Helper()
res, err := db.Exec(`
INSERT INTO departments (code, name, cost_center, active)
VALUES ('TEST', 'Test Department', 'CC-TEST', 1)
ON CONFLICT(code) DO UPDATE SET name = excluded.name
`)
if err != nil {
t.Fatalf("SeedFixtures: insert department: %v", err)
}
id, _ := res.LastInsertId()
deptID = int(id)
res, err = db.Exec(`
INSERT INTO gl_accounts (code, description, type, favour_high, active)
VALUES ('TEST', 'Test Revenue', 'revenue', 1, 1)
ON CONFLICT(code) DO UPDATE SET description = excluded.description
`)
if err != nil {
t.Fatalf("SeedFixtures: insert gl_account: %v", err)
}
id, _ = res.LastInsertId()
glAccountID = int(id)
return deptID, glAccountID
}