Files
carvalho-finances/apps/api/internal/service/account_test.go
T
Mlcavalho1andClaude Sonnet 4.6 e3b3433703 feat(#37): módulo de cartão de crédito com faturas mensais
- Migration 011: closing_day/due_day em accounts + tabela credit_bills
- CreditBillService: cria/atualiza fatura corrente on-demand no GET /accounts
- Widget FATURA ATUAL no dashboard com total e botão PAGAR
- AccountsView: campos closing_day/due_day para contas credit + painel fatura
- Dashboard: current_bills lista faturas não pagas de todos os cartões

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-27 15:12:53 -03:00

90 lines
2.9 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 (m *mockAccountRepo) UpdateLastYieldDate(_ context.Context, _ int, _ string) error { return nil }
type mockCreditBillRepo struct{}
func (m *mockCreditBillRepo) ListByAccount(_ context.Context, _ int) ([]model.CreditBill, error) {
return nil, nil
}
func (m *mockCreditBillRepo) GetCurrent(_ context.Context, _ int) (*model.CreditBill, error) {
return nil, nil
}
func (m *mockCreditBillRepo) Upsert(_ context.Context, _ model.CreditBillInput) (*model.CreditBill, error) {
return &model.CreditBill{}, nil
}
func (m *mockCreditBillRepo) MarkPaid(_ context.Context, _ int, _ *int) error { return nil }
func newAccountSvc() *service.AccountService {
repo := &mockAccountRepo{}
cdiSvc := service.NewCDIYieldService(repo, &mockTxRepo{})
billSvc := service.NewCreditBillService(&mockCreditBillRepo{}, repo)
return service.NewAccountService(repo, cdiSvc, billSvc)
}
func TestCreateAccount_EmptyName(t *testing.T) {
svc := newAccountSvc()
_, 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 := newAccountSvc()
_, 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 := newAccountSvc()
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)
}
}