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,106 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
|
||||
database "household-book/db"
|
||||
)
|
||||
|
||||
func TestDeleteAccountRejectsReconciliationReference(t *testing.T) {
|
||||
store := setupAccountReferenceTestDB(t)
|
||||
if _, err := store.Exec(`
|
||||
INSERT INTO accounts (id, name, type) VALUES (1, '现金', 'cash');
|
||||
INSERT INTO account_reconciliations
|
||||
(account_id, system_balance, actual_balance, difference)
|
||||
VALUES (1, 100, 100, 0);
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err := NewAccountService(store).Delete(context.Background(), 1)
|
||||
if !errors.Is(err, ErrHasTransactions) {
|
||||
t.Fatalf("delete error=%v, want ErrHasTransactions", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteAccountRejectsTransferTargetReference(t *testing.T) {
|
||||
store := setupAccountReferenceTestDB(t)
|
||||
if _, err := store.Exec(`
|
||||
INSERT INTO accounts (id, name, type) VALUES
|
||||
(1, '转入账户', 'cash'),
|
||||
(2, '转出账户', 'cash');
|
||||
INSERT INTO transactions (type, amount, account_id, to_account_id, date)
|
||||
VALUES ('transfer', 100, 2, 1, '2026-08-09');
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
err := NewAccountService(store).Delete(context.Background(), 1)
|
||||
if !errors.Is(err, ErrHasTransactions) {
|
||||
t.Fatalf("delete error=%v, want ErrHasTransactions", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteAccountWithoutReferencesSucceeds(t *testing.T) {
|
||||
store := setupAccountReferenceTestDB(t)
|
||||
if _, err := store.Exec("INSERT INTO accounts (id, name, type) VALUES (1, '现金', 'cash')"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := NewAccountService(store).Delete(context.Background(), 1); err != nil {
|
||||
t.Fatalf("delete unreferenced account: %v", err)
|
||||
}
|
||||
var count int
|
||||
if err := store.QueryRow("SELECT COUNT(*) FROM accounts WHERE id = 1").Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Fatalf("account count=%d, want 0", count)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCleanupAutoImportAccountKeepsReferencedAccount(t *testing.T) {
|
||||
store := setupAccountReferenceTestDB(t)
|
||||
if _, err := store.Exec(`
|
||||
INSERT INTO accounts (id, name, type, note)
|
||||
VALUES (1, '导入账户', 'debit_card', '由收支导入自动创建');
|
||||
INSERT INTO account_reconciliations
|
||||
(account_id, system_balance, actual_balance, difference)
|
||||
VALUES (1, 100, 100, 0);
|
||||
`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
tx, err := store.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback()
|
||||
if err := NewTransactionService(store).cleanupAutoImportAccount(context.Background(), tx, 1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var count int
|
||||
if err := tx.QueryRow("SELECT COUNT(*) FROM accounts WHERE id = 1").Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("account count=%d, want 1", count)
|
||||
}
|
||||
}
|
||||
|
||||
func setupAccountReferenceTestDB(t *testing.T) *sql.DB {
|
||||
t.Helper()
|
||||
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)
|
||||
}
|
||||
return store
|
||||
}
|
||||
Reference in new issue
Block a user