Browse Source

Add unit tests for models

Frédéric Guillot 8 years ago
parent
commit
51f7775466
7 changed files with 224 additions and 1 deletions
  1. 5 1
      model/category.go
  2. 61 0
      model/category_test.go
  3. 57 0
      model/entry_test.go
  4. 2 0
      model/session.go
  5. 19 0
      model/theme_test.go
  6. 4 0
      model/user.go
  7. 76 0
      model/user_test.go

+ 5 - 1
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

+ 61 - 0
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`)
+	}
+}

+ 57 - 0
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"`)
+	}
+}

+ 2 - 0
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

+ 19 - 0
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`)
+	}
+}

+ 4 - 0
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")
 	}

+ 76 - 0
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`)
+	}
+}