Files
carvalho-finances/apps/api/internal/service/account_test.go
T
Mlcavalho1andClaude Sonnet 4.6 9a964423fd feat(#21): contas bancárias e patrimônio consolidado
CRUD de contas (corrente/poupança/investimento/cartão) com saldo calculado
automaticamente. account_id nullable em transactions. Widget patrimônio no
dashboard. Tela /contas. Seletor de conta nas transações manuais.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-26 21:34:56 -03:00

69 lines
2.1 KiB
Go

package service_test
import (
"context"
"testing"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/service"
)
type mockAccountRepo struct {
items []model.Account
}
func (m *mockAccountRepo) List(_ context.Context) ([]model.Account, error) { return m.items, nil }
func (m *mockAccountRepo) GetByID(_ context.Context, id int) (*model.Account, error) {
for _, a := range m.items {
if a.ID == id {
cp := a
return &cp, nil
}
}
return nil, nil
}
func (m *mockAccountRepo) Create(_ context.Context, in model.AccountInput) (*model.Account, error) {
a := model.Account{ID: len(m.items) + 1, Name: in.Name, Type: in.Type, InitialBalance: in.InitialBalance, Balance: in.InitialBalance}
m.items = append(m.items, a)
return &a, nil
}
func (m *mockAccountRepo) Update(_ context.Context, id int, in model.AccountInput) (*model.Account, error) {
for i, a := range m.items {
if a.ID == id {
m.items[i].Name = in.Name
cp := m.items[i]
return &cp, nil
}
}
return nil, nil
}
func (m *mockAccountRepo) Delete(_ context.Context, _ int) error { return nil }
func (m *mockAccountRepo) TotalPatrimony(_ context.Context) (float64, error) { return 0, nil }
func TestCreateAccount_EmptyName(t *testing.T) {
svc := service.NewAccountService(&mockAccountRepo{})
_, err := svc.Create(context.Background(), model.AccountInput{Name: "", Type: "checking", InitialBalance: 0})
if err != service.ErrAccountEmptyName {
t.Fatalf("expected ErrAccountEmptyName, got %v", err)
}
}
func TestCreateAccount_InvalidType(t *testing.T) {
svc := service.NewAccountService(&mockAccountRepo{})
_, err := svc.Create(context.Background(), model.AccountInput{Name: "Nubank", Type: "bitcoin"})
if err != service.ErrAccountInvalidType {
t.Fatalf("expected ErrAccountInvalidType, got %v", err)
}
}
func TestCreateAccount_OK(t *testing.T) {
svc := service.NewAccountService(&mockAccountRepo{})
a, err := svc.Create(context.Background(), model.AccountInput{Name: "Nubank", Type: "checking", InitialBalance: 1000})
if err != nil {
t.Fatal(err)
}
if a.Name != "Nubank" || a.Balance != 1000 {
t.Errorf("unexpected account: %+v", a)
}
}