rule_minmax_test.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. package valid
  2. import (
  3. "testing"
  4. "time"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestMin(t *testing.T) {
  8. date0 := time.Time{}
  9. date20000101 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
  10. date20001201 := time.Date(2000, 12, 1, 0, 0, 0, 0, time.UTC)
  11. date20000601 := time.Date(2000, 6, 1, 0, 0, 0, 0, time.UTC)
  12. tests := []struct {
  13. tag string
  14. threshold interface{}
  15. exclusive bool
  16. value interface{}
  17. err string
  18. }{
  19. // int cases
  20. {"t1.1", 1, false, 1, ""},
  21. {"t1.2", 1, false, 2, ""},
  22. {"t1.3", 1, false, -1, "must be no less than 1"},
  23. {"t1.4", 1, false, 0, ""},
  24. {"t1.5", 1, true, 1, "must be greater than 1"},
  25. {"t1.6", 1, false, "1", "cannot convert string to int64"},
  26. {"t1.7", "1", false, 1, "type not supported: string"},
  27. // uint cases
  28. {"t2.1", uint(2), false, uint(2), ""},
  29. {"t2.2", uint(2), false, uint(3), ""},
  30. {"t2.3", uint(2), false, uint(1), "must be no less than 2"},
  31. {"t2.4", uint(2), false, uint(0), ""},
  32. {"t2.5", uint(2), true, uint(2), "must be greater than 2"},
  33. {"t2.6", uint(2), false, "1", "cannot convert string to uint64"},
  34. // float cases
  35. {"t3.1", float64(2), false, float64(2), ""},
  36. {"t3.2", float64(2), false, float64(3), ""},
  37. {"t3.3", float64(2), false, float64(1), "must be no less than 2"},
  38. {"t3.4", float64(2), false, float64(0), ""},
  39. {"t3.5", float64(2), true, float64(2), "must be greater than 2"},
  40. {"t3.6", float64(2), false, "1", "cannot convert string to float64"},
  41. // Time cases
  42. {"t4.1", date20000601, false, date20000601, ""},
  43. {"t4.2", date20000601, false, date20001201, ""},
  44. {"t4.3", date20000601, false, date20000101, "must be no less than 2000-06-01 00:00:00 +0000 UTC"},
  45. {"t4.4", date20000601, false, date0, ""},
  46. {"t4.5", date20000601, true, date20000601, "must be greater than 2000-06-01 00:00:00 +0000 UTC"},
  47. {"t4.6", date20000601, true, 1, "cannot convert int to time.Time"},
  48. {"t4.7", struct{}{}, false, 1, "type not supported: struct {}"},
  49. {"t4.8", date0, false, date20000601, ""},
  50. }
  51. for _, test := range tests {
  52. r := Min(test.threshold)
  53. if test.exclusive {
  54. r = r.Exclusive()
  55. }
  56. err := r.Validate(test.value)
  57. assertError(t, test.err, err, test.tag)
  58. }
  59. }
  60. func TestMinError(t *testing.T) {
  61. r := Min(10)
  62. assert.Equal(t, "must be no less than 10", r.Validate(9).Error())
  63. r = r.Error("123")
  64. assert.Equal(t, "123", r.err.Message())
  65. }
  66. func TestMax(t *testing.T) {
  67. date0 := time.Time{}
  68. date20000101 := time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC)
  69. date20001201 := time.Date(2000, 12, 1, 0, 0, 0, 0, time.UTC)
  70. date20000601 := time.Date(2000, 6, 1, 0, 0, 0, 0, time.UTC)
  71. tests := []struct {
  72. tag string
  73. threshold interface{}
  74. exclusive bool
  75. value interface{}
  76. err string
  77. }{
  78. // int cases
  79. {"t1.1", 2, false, 2, ""},
  80. {"t1.2", 2, false, 1, ""},
  81. {"t1.3", 2, false, 3, "must be no greater than 2"},
  82. {"t1.4", 2, false, 0, ""},
  83. {"t1.5", 2, true, 2, "must be less than 2"},
  84. {"t1.6", 2, false, "1", "cannot convert string to int64"},
  85. {"t1.7", "1", false, 1, "type not supported: string"},
  86. // uint cases
  87. {"t2.1", uint(2), false, uint(2), ""},
  88. {"t2.2", uint(2), false, uint(1), ""},
  89. {"t2.3", uint(2), false, uint(3), "must be no greater than 2"},
  90. {"t2.4", uint(2), false, uint(0), ""},
  91. {"t2.5", uint(2), true, uint(2), "must be less than 2"},
  92. {"t2.6", uint(2), false, "1", "cannot convert string to uint64"},
  93. // float cases
  94. {"t3.1", float64(2), false, float64(2), ""},
  95. {"t3.2", float64(2), false, float64(1), ""},
  96. {"t3.3", float64(2), false, float64(3), "must be no greater than 2"},
  97. {"t3.4", float64(2), false, float64(0), ""},
  98. {"t3.5", float64(2), true, float64(2), "must be less than 2"},
  99. {"t3.6", float64(2), false, "1", "cannot convert string to float64"},
  100. // Time cases
  101. {"t4.1", date20000601, false, date20000601, ""},
  102. {"t4.2", date20000601, false, date20000101, ""},
  103. {"t4.3", date20000601, false, date20001201, "must be no greater than 2000-06-01 00:00:00 +0000 UTC"},
  104. {"t4.4", date20000601, false, date0, ""},
  105. {"t4.5", date20000601, true, date20000601, "must be less than 2000-06-01 00:00:00 +0000 UTC"},
  106. {"t4.6", date20000601, true, 1, "cannot convert int to time.Time"},
  107. }
  108. for _, test := range tests {
  109. r := Max(test.threshold)
  110. if test.exclusive {
  111. r = r.Exclusive()
  112. }
  113. err := r.Validate(test.value)
  114. assertError(t, test.err, err, test.tag)
  115. }
  116. }
  117. func TestMaxError(t *testing.T) {
  118. r := Max(10)
  119. assert.Equal(t, "must be no greater than 10", r.Validate(11).Error())
  120. r = r.Error("123")
  121. assert.Equal(t, "123", r.err.Message())
  122. }
  123. func TestThresholdRule_ErrorObject(t *testing.T) {
  124. r := Max(10)
  125. err := NewError("code", "abc")
  126. r = r.ErrorObject(err)
  127. assert.Equal(t, err, r.err)
  128. assert.Equal(t, err.Code(), r.err.Code())
  129. assert.Equal(t, err.Message(), r.err.Message())
  130. }