entry_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package model
  5. import "testing"
  6. func TestValidateEntryStatus(t *testing.T) {
  7. for _, status := range []string{EntryStatusRead, EntryStatusUnread, EntryStatusRemoved} {
  8. if err := ValidateEntryStatus(status); err != nil {
  9. t.Error(`A valid status should not generate any error`)
  10. }
  11. }
  12. if err := ValidateEntryStatus("invalid"); err == nil {
  13. t.Error(`An invalid status should generate a error`)
  14. }
  15. }
  16. func TestValidateEntryOrder(t *testing.T) {
  17. for _, status := range []string{"id", "status", "published_at", "category_title", "category_id"} {
  18. if err := ValidateEntryOrder(status); err != nil {
  19. t.Error(`A valid order should not generate any error`)
  20. }
  21. }
  22. if err := ValidateEntryOrder("invalid"); err == nil {
  23. t.Error(`An invalid order should generate a error`)
  24. }
  25. }
  26. func TestValidateEntryDirection(t *testing.T) {
  27. for _, status := range []string{"asc", "desc"} {
  28. if err := ValidateDirection(status); err != nil {
  29. t.Error(`A valid direction should not generate any error`)
  30. }
  31. }
  32. if err := ValidateDirection("invalid"); err == nil {
  33. t.Error(`An invalid direction should generate a error`)
  34. }
  35. }
  36. func TestValidateRange(t *testing.T) {
  37. if err := ValidateRange(-1, 0); err == nil {
  38. t.Error(`An invalid offset should generate a error`)
  39. }
  40. if err := ValidateRange(0, -1); err == nil {
  41. t.Error(`An invalid limit should generate a error`)
  42. }
  43. if err := ValidateRange(42, 42); err != nil {
  44. t.Error(`A valid offset and limit should not generate any error`)
  45. }
  46. }
  47. func TestGetOppositeDirection(t *testing.T) {
  48. if OppositeDirection("asc") != "desc" {
  49. t.Errorf(`The opposite direction of "asc" should be "desc"`)
  50. }
  51. if OppositeDirection("desc") != "asc" {
  52. t.Errorf(`The opposite direction of "desc" should be "asc"`)
  53. }
  54. if OppositeDirection("invalid") != "asc" {
  55. t.Errorf(`An invalid direction should return "asc"`)
  56. }
  57. }