feat(#17): CRUD de categorias — API Go + tela Vue + testes unitários
API REST /api/categories (GET/POST/PUT/DELETE) com regras de negócio: categorias padrão não podem ser excluídas; categorias com transações vinculadas também bloqueadas. Tela Vue com formulário inline de criação/edição, indicador visual de categorias padrão e botão de exclusão condicional. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
package service_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"financeiro-carvalho/internal/model"
|
||||
"financeiro-carvalho/internal/repository"
|
||||
"financeiro-carvalho/internal/service"
|
||||
)
|
||||
|
||||
// mockCategoryRepo implements repository.CategoryRepository in memory.
|
||||
type mockCategoryRepo struct {
|
||||
categories []model.Category
|
||||
txCount map[int]int // category_id → transaction count
|
||||
}
|
||||
|
||||
func newMockRepo(cats []model.Category) *mockCategoryRepo {
|
||||
return &mockCategoryRepo{categories: cats, txCount: map[int]int{}}
|
||||
}
|
||||
|
||||
func (m *mockCategoryRepo) List(_ context.Context) ([]model.Category, error) {
|
||||
return m.categories, nil
|
||||
}
|
||||
|
||||
func (m *mockCategoryRepo) GetByID(_ context.Context, id int) (*model.Category, error) {
|
||||
for _, c := range m.categories {
|
||||
if c.ID == id {
|
||||
cp := c
|
||||
return &cp, nil
|
||||
}
|
||||
}
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
|
||||
func (m *mockCategoryRepo) Create(_ context.Context, name, color string) (*model.Category, error) {
|
||||
c := model.Category{ID: len(m.categories) + 1, Name: name, Color: color}
|
||||
m.categories = append(m.categories, c)
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (m *mockCategoryRepo) Update(_ context.Context, id int, name, color string) (*model.Category, error) {
|
||||
for i, c := range m.categories {
|
||||
if c.ID == id {
|
||||
m.categories[i].Name = name
|
||||
m.categories[i].Color = color
|
||||
cp := m.categories[i]
|
||||
return &cp, nil
|
||||
}
|
||||
}
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
|
||||
func (m *mockCategoryRepo) Delete(_ context.Context, id int) error {
|
||||
for i, c := range m.categories {
|
||||
if c.ID == id {
|
||||
m.categories = append(m.categories[:i], m.categories[i+1:]...)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return repository.ErrNotFound
|
||||
}
|
||||
|
||||
func (m *mockCategoryRepo) HasTransactions(_ context.Context, id int) (bool, error) {
|
||||
return m.txCount[id] > 0, nil
|
||||
}
|
||||
|
||||
// ── tests ──────────────────────────────────────────────────────────────────────
|
||||
|
||||
func TestDeleteDefaultCategory_Rejected(t *testing.T) {
|
||||
repo := newMockRepo([]model.Category{
|
||||
{ID: 1, Name: "Saúde", Color: "#EF4444", IsDefault: true},
|
||||
})
|
||||
svc := service.NewCategoryService(repo)
|
||||
|
||||
err := svc.Delete(context.Background(), 1)
|
||||
if err != service.ErrDefaultCategory {
|
||||
t.Fatalf("expected ErrDefaultCategory, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteCategoryWithTransactions_Rejected(t *testing.T) {
|
||||
repo := newMockRepo([]model.Category{
|
||||
{ID: 2, Name: "Lazer", Color: "#10B981", IsDefault: false},
|
||||
})
|
||||
repo.txCount[2] = 3
|
||||
svc := service.NewCategoryService(repo)
|
||||
|
||||
err := svc.Delete(context.Background(), 2)
|
||||
if err != service.ErrHasTransactions {
|
||||
t.Fatalf("expected ErrHasTransactions, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteUserCategory_OK(t *testing.T) {
|
||||
repo := newMockRepo([]model.Category{
|
||||
{ID: 3, Name: "Minha Cat", Color: "#123456", IsDefault: false},
|
||||
})
|
||||
svc := service.NewCategoryService(repo)
|
||||
|
||||
if err := svc.Delete(context.Background(), 3); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateCategory_EmptyName_Rejected(t *testing.T) {
|
||||
svc := service.NewCategoryService(newMockRepo(nil))
|
||||
_, err := svc.Create(context.Background(), model.CategoryInput{Name: " ", Color: "#123456"})
|
||||
if err != service.ErrEmptyName {
|
||||
t.Fatalf("expected ErrEmptyName, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateCategory_InvalidColor_Rejected(t *testing.T) {
|
||||
svc := service.NewCategoryService(newMockRepo(nil))
|
||||
_, err := svc.Create(context.Background(), model.CategoryInput{Name: "X", Color: "red"})
|
||||
if err != service.ErrInvalidColor {
|
||||
t.Fatalf("expected ErrInvalidColor, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateCategory_OK(t *testing.T) {
|
||||
svc := service.NewCategoryService(newMockRepo(nil))
|
||||
cat, err := svc.Create(context.Background(), model.CategoryInput{Name: "Nova", Color: "#AABBCC"})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if cat.Name != "Nova" || cat.Color != "#AABBCC" {
|
||||
t.Fatalf("unexpected result: %+v", cat)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user