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,179 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
database "household-book/db"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
func TestGroupedBalanceAccountsAndBoundAsset(t *testing.T) {
|
||||
db := setupGroupedBalanceTestDB(t)
|
||||
ctx := context.Background()
|
||||
|
||||
accounts, err := NewAccountService(db).List(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("list accounts: %v", err)
|
||||
}
|
||||
balances := make(map[string]int64, len(accounts))
|
||||
for _, account := range accounts {
|
||||
balances[account.Name] = account.CurrentBalance
|
||||
}
|
||||
if got := balances["现金"]; got != 11800 {
|
||||
t.Fatalf("cash current balance=%d, want 11800", got)
|
||||
}
|
||||
if got := balances["信用卡"]; got != 1200 {
|
||||
t.Fatalf("credit current balance=%d, want 1200", got)
|
||||
}
|
||||
if got := balances["无流水账户"]; got != 300 {
|
||||
t.Fatalf("no-transaction current balance=%d, want 300", got)
|
||||
}
|
||||
|
||||
assets, err := NewAssetService(db).ListAssets(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("list assets: %v", err)
|
||||
}
|
||||
if len(assets) != 1 {
|
||||
t.Fatalf("assets count=%d, want 1", len(assets))
|
||||
}
|
||||
if !assets[0].IsDerived || assets[0].Value != 11800 {
|
||||
t.Fatalf("bound asset=%+v, want derived value 11800", assets[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestGroupedBalanceNetWorth(t *testing.T) {
|
||||
db := setupGroupedBalanceTestDB(t)
|
||||
for _, stmt := range []string{
|
||||
`INSERT INTO assets (id, name, type, value, account_id)
|
||||
VALUES (2, '手工资产', 'other', 2500, NULL)`,
|
||||
`INSERT INTO liabilities (id, principal) VALUES (1, 1700)`,
|
||||
} {
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
t.Fatalf("exec net worth fixture: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
summary, err := NewAssetService(db).NetWorth(context.Background())
|
||||
if err != nil {
|
||||
t.Fatalf("net worth: %v", err)
|
||||
}
|
||||
if summary.TotalAssets != 14300 {
|
||||
t.Fatalf("total assets=%d, want 14300", summary.TotalAssets)
|
||||
}
|
||||
if summary.TotalDebt != 1700 {
|
||||
t.Fatalf("total debt=%d, want 1700", summary.TotalDebt)
|
||||
}
|
||||
if summary.LiquidAssets != 300 {
|
||||
t.Fatalf("liquid assets=%d, want 300", summary.LiquidAssets)
|
||||
}
|
||||
if summary.LiquidDebt != 900 {
|
||||
t.Fatalf("liquid debt=%d, want 900", summary.LiquidDebt)
|
||||
}
|
||||
if summary.NetWorth != 12000 {
|
||||
t.Fatalf("net worth=%d, want 12000", summary.NetWorth)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBalanceQueriesExecuteAgainstMigratedSchema(t *testing.T) {
|
||||
store, err := database.Open(filepath.Join(t.TempDir(), "book.db"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = store.Close() })
|
||||
if err := database.RunMigrations(store); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := database.Seed(store); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for name, query := range map[string]string{
|
||||
"accounts": accountSelectSQL(),
|
||||
"assets": assetSelectSQL(),
|
||||
} {
|
||||
rows, err := store.Query(query)
|
||||
if err != nil {
|
||||
t.Fatalf("%s query: %v", name, err)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
t.Fatalf("close %s rows: %v", name, err)
|
||||
}
|
||||
}
|
||||
var totalAssets, totalDebt, liquidAssets, liquidDebt int64
|
||||
if err := store.QueryRow(netWorthSQL()).Scan(
|
||||
&totalAssets, &totalDebt, &liquidAssets, &liquidDebt,
|
||||
); err != nil {
|
||||
t.Fatalf("net worth query: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func setupGroupedBalanceTestDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
db, err := sql.Open("sqlite", ":memory:")
|
||||
if err != nil {
|
||||
t.Fatalf("open sqlite: %v", err)
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
t.Cleanup(func() { _ = db.Close() })
|
||||
|
||||
for _, stmt := range []string{
|
||||
`CREATE TABLE accounts (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
balance INTEGER NOT NULL,
|
||||
bank_name TEXT NOT NULL DEFAULT '',
|
||||
card_number TEXT NOT NULL DEFAULT '',
|
||||
branch_name TEXT NOT NULL DEFAULT '',
|
||||
note TEXT NOT NULL DEFAULT '',
|
||||
is_active INTEGER NOT NULL DEFAULT 1,
|
||||
created_at TEXT NOT NULL DEFAULT '',
|
||||
updated_at TEXT NOT NULL DEFAULT ''
|
||||
)`,
|
||||
`CREATE TABLE transactions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
type TEXT NOT NULL,
|
||||
amount INTEGER NOT NULL,
|
||||
account_id INTEGER,
|
||||
to_account_id INTEGER
|
||||
)`,
|
||||
`CREATE TABLE assets (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
value INTEGER NOT NULL,
|
||||
account_id INTEGER,
|
||||
note TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL DEFAULT '',
|
||||
updated_at TEXT NOT NULL DEFAULT ''
|
||||
)`,
|
||||
`CREATE TABLE liabilities (
|
||||
id INTEGER PRIMARY KEY,
|
||||
principal INTEGER NOT NULL
|
||||
)`,
|
||||
`INSERT INTO accounts (id, name, type, balance) VALUES
|
||||
(1, '现金', 'cash', 10000),
|
||||
(2, '信用卡', 'credit_card', 0),
|
||||
(3, '负余额信用卡', 'credit_card', 0),
|
||||
(4, '无流水账户', 'cash', 300),
|
||||
(5, '停用账户', 'cash', 400)`,
|
||||
`UPDATE accounts SET is_active = 0 WHERE id = 5`,
|
||||
`INSERT INTO transactions (id, type, amount, account_id, to_account_id) VALUES
|
||||
(1, 'income', 5000, 1, NULL),
|
||||
(2, 'expense', 1200, 1, NULL),
|
||||
(3, 'transfer', 2000, 1, 2),
|
||||
(4, 'expense', 800, 2, NULL),
|
||||
(5, 'expense', 900, 3, NULL)`,
|
||||
`INSERT INTO assets (id, name, type, value, account_id)
|
||||
VALUES (1, '现金资产', 'cash', 0, 1)`,
|
||||
} {
|
||||
if _, err := db.Exec(stmt); err != nil {
|
||||
t.Fatalf("exec fixture: %v", err)
|
||||
}
|
||||
}
|
||||
return db
|
||||
}
|
||||
Reference in new issue
Block a user