mirror of
https://github.com/miniflux/v2.git
synced 2025-08-01 17:38:37 +00:00
Refactor category validation
This commit is contained in:
parent
e45cc2d2aa
commit
4468ef1410
13 changed files with 122 additions and 212 deletions
|
@ -4,51 +4,28 @@
|
|||
|
||||
package model // import "miniflux.app/model"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
import "fmt"
|
||||
|
||||
// Category represents a category in the system.
|
||||
// Category represents a feed category.
|
||||
type Category struct {
|
||||
ID int64 `json:"id,omitempty"`
|
||||
Title string `json:"title,omitempty"`
|
||||
UserID int64 `json:"user_id,omitempty"`
|
||||
FeedCount int `json:"nb_feeds,omitempty"`
|
||||
ID int64 `json:"id"`
|
||||
Title string `json:"title"`
|
||||
UserID int64 `json:"user_id"`
|
||||
FeedCount int `json:"-"`
|
||||
}
|
||||
|
||||
func (c *Category) String() string {
|
||||
return fmt.Sprintf("ID=%d, UserID=%d, Title=%s", c.ID, c.UserID, c.Title)
|
||||
}
|
||||
|
||||
// ValidateCategoryCreation validates a category during the creation.
|
||||
func (c Category) ValidateCategoryCreation() error {
|
||||
if c.Title == "" {
|
||||
return errors.New("The title is mandatory")
|
||||
}
|
||||
|
||||
if c.UserID == 0 {
|
||||
return errors.New("The userID is mandatory")
|
||||
}
|
||||
|
||||
return nil
|
||||
// CategoryRequest represents the request to create or update a category.
|
||||
type CategoryRequest struct {
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
// ValidateCategoryModification validates a category during the modification.
|
||||
func (c Category) ValidateCategoryModification() error {
|
||||
if c.Title == "" {
|
||||
return errors.New("The title is mandatory")
|
||||
}
|
||||
|
||||
if c.UserID == 0 {
|
||||
return errors.New("The userID is mandatory")
|
||||
}
|
||||
|
||||
if c.ID <= 0 {
|
||||
return errors.New("The ID is mandatory")
|
||||
}
|
||||
|
||||
return nil
|
||||
// Patch updates category fields.
|
||||
func (cr *CategoryRequest) Patch(category *Category) {
|
||||
category.Title = cr.Title
|
||||
}
|
||||
|
||||
// Categories represents a list of categories.
|
||||
|
|
|
@ -1,61 +0,0 @@
|
|||
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
||||
// Use of this source code is governed by the Apache 2.0
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package model // import "miniflux.app/model"
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestValidateCategoryCreation(t *testing.T) {
|
||||
category := &Category{}
|
||||
if err := category.ValidateCategoryCreation(); err == nil {
|
||||
t.Error(`An empty category should generate an error`)
|
||||
}
|
||||
|
||||
category = &Category{Title: "Test"}
|
||||
if err := category.ValidateCategoryCreation(); err == nil {
|
||||
t.Error(`A category without userID should generate an error`)
|
||||
}
|
||||
|
||||
category = &Category{UserID: 42}
|
||||
if err := category.ValidateCategoryCreation(); err == nil {
|
||||
t.Error(`A category without title should generate an error`)
|
||||
}
|
||||
|
||||
category = &Category{Title: "Test", UserID: 42}
|
||||
if err := category.ValidateCategoryCreation(); err != nil {
|
||||
t.Error(`All required fields are filled, it should not generate any error`)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateCategoryModification(t *testing.T) {
|
||||
category := &Category{}
|
||||
if err := category.ValidateCategoryModification(); err == nil {
|
||||
t.Error(`An empty category should generate an error`)
|
||||
}
|
||||
|
||||
category = &Category{Title: "Test"}
|
||||
if err := category.ValidateCategoryModification(); err == nil {
|
||||
t.Error(`A category without userID should generate an error`)
|
||||
}
|
||||
|
||||
category = &Category{UserID: 42}
|
||||
if err := category.ValidateCategoryModification(); err == nil {
|
||||
t.Error(`A category without title should generate an error`)
|
||||
}
|
||||
|
||||
category = &Category{ID: -1, Title: "Test", UserID: 42}
|
||||
if err := category.ValidateCategoryModification(); err == nil {
|
||||
t.Error(`An invalid categoryID should generate an error`)
|
||||
}
|
||||
|
||||
category = &Category{ID: 0, Title: "Test", UserID: 42}
|
||||
if err := category.ValidateCategoryModification(); err == nil {
|
||||
t.Error(`An invalid categoryID should generate an error`)
|
||||
}
|
||||
|
||||
category = &Category{ID: 1, Title: "Test", UserID: 42}
|
||||
if err := category.ValidateCategoryModification(); err != nil {
|
||||
t.Error(`All required fields are filled, it should not generate any error`)
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue