chore: initial commit of HouseholdBudgetBook (backend, frontend, desktop)
This commit is contained in:
commit
4a5db3f1f9
167 files changed
+21732
No files matched your search
@@ -0,0 +1,116 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func TestCategoryRankingOnlyLeafCategories(t *testing.T) {
|
||||
db := setupReportTestDB(t)
|
||||
svc := NewReportService(db)
|
||||
ctx := context.Background()
|
||||
|
||||
insertCategory(t, db, 1, "支出", "expense", nil, 1)
|
||||
insertCategory(t, db, 2, "餐饮", "expense", int64Ptr(1), 1)
|
||||
insertTransaction(t, db, "expense", 1000, 1, "2026-05-10")
|
||||
insertTransaction(t, db, "expense", 2000, 2, "2026-05-11")
|
||||
|
||||
items, err := svc.CategoryRanking(ctx, 2026, 5, "expense", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("CategoryRanking: %v", err)
|
||||
}
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("expected only leaf category in ranking, got %d items", len(items))
|
||||
}
|
||||
if items[0].CategoryID != 2 || items[0].Amount != 2000 {
|
||||
t.Fatalf("unexpected ranking item: %+v", items[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestCategoryRankingIncludesInactiveFlag(t *testing.T) {
|
||||
db := setupReportTestDB(t)
|
||||
svc := NewReportService(db)
|
||||
ctx := context.Background()
|
||||
|
||||
insertCategory(t, db, 1, "支出", "expense", nil, 1)
|
||||
insertCategory(t, db, 2, "旧分类", "expense", int64Ptr(1), 0)
|
||||
insertTransaction(t, db, "expense", 500, 2, "2026-05-12")
|
||||
|
||||
items, err := svc.CategoryRanking(ctx, 2026, 5, "expense", 10)
|
||||
if err != nil {
|
||||
t.Fatalf("CategoryRanking: %v", err)
|
||||
}
|
||||
if len(items) != 1 {
|
||||
t.Fatalf("expected 1 item, got %d", len(items))
|
||||
}
|
||||
if items[0].CategoryIsActive {
|
||||
t.Fatalf("expected inactive category, got %+v", items[0])
|
||||
}
|
||||
}
|
||||
|
||||
func setupReportTestDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
db, err := sql.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
stmts := []string{
|
||||
`CREATE TABLE categories (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
parent_id INTEGER,
|
||||
is_active INTEGER NOT NULL DEFAULT 1
|
||||
)`,
|
||||
`CREATE TABLE transactions (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
type TEXT NOT NULL,
|
||||
amount INTEGER NOT NULL,
|
||||
category_id INTEGER,
|
||||
account_id INTEGER,
|
||||
to_account_id INTEGER,
|
||||
member_id INTEGER,
|
||||
date TEXT NOT NULL,
|
||||
description TEXT NOT NULL DEFAULT '',
|
||||
counterparty TEXT NOT NULL DEFAULT '',
|
||||
import_batch_id INTEGER,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now')),
|
||||
updated_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
)`,
|
||||
}
|
||||
for _, stmt := range stmts {
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
t.Fatalf("exec schema: %v", err)
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
func insertCategory(t *testing.T, db *sql.DB, id int64, name, typ string, parentID *int64, isActive int) {
|
||||
t.Helper()
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO categories (id, name, type, parent_id, is_active) VALUES (?, ?, ?, ?, ?)`,
|
||||
id, name, typ, parentID, isActive,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert category: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func insertTransaction(t *testing.T, db *sql.DB, typ string, amount, categoryID int64, date string) {
|
||||
t.Helper()
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO transactions (type, amount, category_id, date) VALUES (?, ?, ?, ?)`,
|
||||
typ, amount, categoryID, date,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("insert transaction: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func int64Ptr(v int64) *int64 { return &v }
|
||||
Reference in new issue
Block a user