rule_date_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package valid
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestDate(t *testing.T) {
  8. tests := []struct {
  9. tag string
  10. layout string
  11. value interface{}
  12. err string
  13. }{
  14. {"t1", time.ANSIC, "", ""},
  15. {"t2", time.ANSIC, "Wed Feb 4 21:00:57 2009", ""},
  16. {"t3", time.ANSIC, "Wed Feb 29 21:00:57 2009", "must be a valid date"},
  17. {"t4", "2006-01-02", "2009-11-12", ""},
  18. {"t5", "2006-01-02", "2009-11-12 21:00:57", "must be a valid date"},
  19. {"t6", "2006-01-02", "2009-1-12", "must be a valid date"},
  20. {"t7", "2006-01-02", "2009-01-12", ""},
  21. {"t8", "2006-01-02", "2009-01-32", "must be a valid date"},
  22. {"t9", "2006-01-02", 1, "must be either a string or byte slice"},
  23. }
  24. for _, test := range tests {
  25. r := Date(test.layout)
  26. err := r.Validate(test.value)
  27. assertError(t, test.err, err, test.tag)
  28. }
  29. }
  30. func TestDateRule_Error(t *testing.T) {
  31. r := Date(time.RFC3339)
  32. assert.Equal(t, "must be a valid date", r.Validate("0001-01-02T15:04:05Z07:00").Error())
  33. r2 := r.Min(time.Date(2000, 1, 1, 1, 1, 1, 0, time.UTC))
  34. assert.Equal(t, "the date is out of range", r2.Validate("1999-01-02T15:04:05Z").Error())
  35. r = r.Error("123")
  36. r = r.RangeError("456")
  37. assert.Equal(t, "123", r.err.Message())
  38. assert.Equal(t, "456", r.rangeErr.Message())
  39. }
  40. func TestDateRule_ErrorObject(t *testing.T) {
  41. r := Date(time.RFC3339)
  42. assert.Equal(t, "must be a valid date", r.Validate("0001-01-02T15:04:05Z07:00").Error())
  43. r = r.ErrorObject(NewError("code", "abc"))
  44. assert.Equal(t, "code", r.err.Code())
  45. assert.Equal(t, "abc", r.Validate("0001-01-02T15:04:05Z07:00").Error())
  46. r2 := r.Min(time.Date(2000, 1, 1, 1, 1, 1, 0, time.UTC))
  47. assert.Equal(t, "the date is out of range", r2.Validate("1999-01-02T15:04:05Z").Error())
  48. r = r.ErrorObject(NewError("C", "def"))
  49. r = r.RangeErrorObject(NewError("D", "123"))
  50. assert.Equal(t, "C", r.err.Code())
  51. assert.Equal(t, "def", r.err.Message())
  52. assert.Equal(t, "D", r.rangeErr.Code())
  53. assert.Equal(t, "123", r.rangeErr.Message())
  54. }
  55. func TestDateRule_MinMax(t *testing.T) {
  56. r := Date(time.ANSIC)
  57. assert.True(t, r.min.IsZero())
  58. assert.True(t, r.max.IsZero())
  59. r = r.Min(time.Now())
  60. assert.False(t, r.min.IsZero())
  61. assert.True(t, r.max.IsZero())
  62. r = r.Max(time.Now())
  63. assert.False(t, r.max.IsZero())
  64. r2 := Date("2006-01-02").Min(time.Date(2000, 12, 1, 0, 0, 0, 0, time.UTC)).Max(time.Date(2020, 2, 1, 0, 0, 0, 0, time.UTC))
  65. assert.Nil(t, r2.Validate("2010-01-02"))
  66. err := r2.Validate("1999-01-02")
  67. if assert.NotNil(t, err) {
  68. assert.Equal(t, "the date is out of range", err.Error())
  69. }
  70. err = r2.Validate("2021-01-02")
  71. if assert.NotNil(t, err) {
  72. assert.Equal(t, "the date is out of range", err.Error())
  73. }
  74. }