aboutsummaryrefslogtreecommitdiffhomepage
path: root/model
diff options
context:
space:
mode:
authorGravatar Frédéric Guillot <fred@miniflux.net>2017-11-25 19:06:02 -0800
committerGravatar Frédéric Guillot <fred@miniflux.net>2017-11-25 19:06:02 -0800
commit51f77754660ddcd29f61be293a8e405d8cd3ba18 (patch)
treec421b1ab2a470e23e3ce6dbf2d09878087ebd614 /model
parent4560af165e7fa25dddd46f49ad4a0d9f3149336c (diff)
Add unit tests for models
Diffstat (limited to 'model')
-rw-r--r--model/category.go6
-rw-r--r--model/category_test.go61
-rw-r--r--model/entry_test.go57
-rw-r--r--model/session.go2
-rw-r--r--model/theme_test.go19
-rw-r--r--model/user.go4
-rw-r--r--model/user_test.go76
7 files changed, 224 insertions, 1 deletions
diff --git a/model/category.go b/model/category.go
index 76f254f..1c72851 100644
--- a/model/category.go
+++ b/model/category.go
@@ -9,6 +9,7 @@ import (
"fmt"
)
+// Category represents a category in the system.
type Category struct {
ID int64 `json:"id,omitempty"`
Title string `json:"title,omitempty"`
@@ -20,6 +21,7 @@ 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")
@@ -32,6 +34,7 @@ func (c Category) ValidateCategoryCreation() error {
return nil
}
+// ValidateCategoryModification validates a category during the modification.
func (c Category) ValidateCategoryModification() error {
if c.Title == "" {
return errors.New("The title is mandatory")
@@ -41,11 +44,12 @@ func (c Category) ValidateCategoryModification() error {
return errors.New("The userID is mandatory")
}
- if c.ID == 0 {
+ if c.ID <= 0 {
return errors.New("The ID is mandatory")
}
return nil
}
+// Categories represents a list of categories.
type Categories []*Category
diff --git a/model/category_test.go b/model/category_test.go
new file mode 100644
index 0000000..9f8a054
--- /dev/null
+++ b/model/category_test.go
@@ -0,0 +1,61 @@
+// 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 "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`)
+ }
+}
diff --git a/model/entry_test.go b/model/entry_test.go
new file mode 100644
index 0000000..3f2e196
--- /dev/null
+++ b/model/entry_test.go
@@ -0,0 +1,57 @@
+// 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 "testing"
+
+func TestValidateEntryStatus(t *testing.T) {
+ for _, status := range []string{EntryStatusRead, EntryStatusUnread, EntryStatusRemoved} {
+ if err := ValidateEntryStatus(status); err != nil {
+ t.Error(`A valid status should not generate any error`)
+ }
+ }
+
+ if err := ValidateEntryStatus("invalid"); err == nil {
+ t.Error(`An invalid status should generate a error`)
+ }
+}
+
+func TestValidateEntryOrder(t *testing.T) {
+ for _, status := range []string{"id", "status", "published_at", "category_title", "category_id"} {
+ if err := ValidateEntryOrder(status); err != nil {
+ t.Error(`A valid order should not generate any error`)
+ }
+ }
+
+ if err := ValidateEntryOrder("invalid"); err == nil {
+ t.Error(`An invalid order should generate a error`)
+ }
+}
+
+func TestValidateEntryDirection(t *testing.T) {
+ for _, status := range []string{"asc", "desc"} {
+ if err := ValidateDirection(status); err != nil {
+ t.Error(`A valid direction should not generate any error`)
+ }
+ }
+
+ if err := ValidateDirection("invalid"); err == nil {
+ t.Error(`An invalid direction should generate a error`)
+ }
+}
+
+func TestGetOppositeDirection(t *testing.T) {
+ if GetOppositeDirection("asc") != "desc" {
+ t.Errorf(`The opposite direction of "asc" should be "desc"`)
+ }
+
+ if GetOppositeDirection("desc") != "asc" {
+ t.Errorf(`The opposite direction of "desc" should be "asc"`)
+ }
+
+ if GetOppositeDirection("invalid") != "asc" {
+ t.Errorf(`An invalid direction should return "asc"`)
+ }
+}
diff --git a/model/session.go b/model/session.go
index ba62d8a..96e445d 100644
--- a/model/session.go
+++ b/model/session.go
@@ -7,6 +7,7 @@ package model
import "time"
import "fmt"
+// Session represents a user session in the system.
type Session struct {
ID int64
UserID int64
@@ -20,4 +21,5 @@ func (s *Session) String() string {
return fmt.Sprintf("ID=%d, UserID=%d, IP=%s", s.ID, s.UserID, s.IP)
}
+// Sessions represents a list of sessions.
type Sessions []*Session
diff --git a/model/theme_test.go b/model/theme_test.go
new file mode 100644
index 0000000..b2a8cdb
--- /dev/null
+++ b/model/theme_test.go
@@ -0,0 +1,19 @@
+// 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 "testing"
+
+func TestValidateTheme(t *testing.T) {
+ for _, status := range []string{"default", "black"} {
+ if err := ValidateTheme(status); err != nil {
+ t.Error(`A valid theme should not generate any error`)
+ }
+ }
+
+ if err := ValidateTheme("invalid"); err == nil {
+ t.Error(`An invalid theme should generate a error`)
+ }
+}
diff --git a/model/user.go b/model/user.go
index 0fd5044..b756f1b 100644
--- a/model/user.go
+++ b/model/user.go
@@ -42,6 +42,10 @@ func (u User) ValidateUserCreation() error {
// ValidateUserModification validates user for modification.
func (u User) ValidateUserModification() error {
+ if u.ID <= 0 {
+ return errors.New("The ID is mandatory")
+ }
+
if u.Username == "" {
return errors.New("The username is mandatory")
}
diff --git a/model/user_test.go b/model/user_test.go
new file mode 100644
index 0000000..769696f
--- /dev/null
+++ b/model/user_test.go
@@ -0,0 +1,76 @@
+// 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 "testing"
+
+func TestValidateUserCreation(t *testing.T) {
+ user := &User{}
+ if err := user.ValidateUserCreation(); err == nil {
+ t.Error(`An empty user should generate an error`)
+ }
+
+ user = &User{Username: "test", Password: ""}
+ if err := user.ValidateUserCreation(); err == nil {
+ t.Error(`User without password should generate an error`)
+ }
+
+ user = &User{Username: "test", Password: "a"}
+ if err := user.ValidateUserCreation(); err == nil {
+ t.Error(`Passwords shorter than 6 characters should generate an error`)
+ }
+
+ user = &User{Username: "", Password: "secret"}
+ if err := user.ValidateUserCreation(); err == nil {
+ t.Error(`An empty username should generate an error`)
+ }
+
+ user = &User{Username: "test", Password: "secret"}
+ if err := user.ValidateUserCreation(); err != nil {
+ t.Error(`A valid user should not generate any error`)
+ }
+}
+
+func TestValidateUserModification(t *testing.T) {
+ user := &User{}
+ if err := user.ValidateUserModification(); err == nil {
+ t.Error(`An empty user should generate an error`)
+ }
+
+ user = &User{ID: 42, Username: "test", Password: "", Theme: "default"}
+ if err := user.ValidateUserModification(); err != nil {
+ t.Error(`User without password should not generate an error`)
+ }
+
+ user = &User{ID: 42, Username: "test", Password: "a", Theme: "default"}
+ if err := user.ValidateUserModification(); err == nil {
+ t.Error(`Passwords shorter than 6 characters should generate an error`)
+ }
+
+ user = &User{ID: 42, Username: "", Password: "secret", Theme: "default"}
+ if err := user.ValidateUserModification(); err == nil {
+ t.Error(`An empty username should generate an error`)
+ }
+
+ user = &User{ID: -1, Username: "test", Password: "secret", Theme: "default"}
+ if err := user.ValidateUserModification(); err == nil {
+ t.Error(`An invalid userID should generate an error`)
+ }
+
+ user = &User{ID: 0, Username: "test", Password: "secret", Theme: "default"}
+ if err := user.ValidateUserModification(); err == nil {
+ t.Error(`An invalid userID should generate an error`)
+ }
+
+ user = &User{ID: 42, Username: "test", Password: "secret", Theme: "invalid"}
+ if err := user.ValidateUserModification(); err == nil {
+ t.Error(`An invalid theme should generate an error`)
+ }
+
+ user = &User{ID: 42, Username: "test", Password: "secret", Theme: "default"}
+ if err := user.ValidateUserModification(); err != nil {
+ t.Error(`A valid user should not generate any error`)
+ }
+}