validate_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package util
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. // TC-0269: page=2, pageSize=10
  7. func TestNormalizePage(t *testing.T) {
  8. tests := []struct {
  9. name string
  10. page, pageSize int64
  11. wantP, wantSize int64
  12. }{
  13. {"normal values", 2, 10, 2, 10},
  14. {"page=0", 0, 10, 1, 10},
  15. {"page=-1", -1, 10, 1, 10},
  16. {"pageSize=0", 1, 0, 1, 20},
  17. {"pageSize=-5", 1, -5, 1, 20},
  18. {"pageSize=500", 1, 500, 1, 500},
  19. {"pageSize=100 boundary", 1, 100, 1, 100},
  20. {"pageSize=101 boundary", 1, 101, 1, 101},
  21. {"pageSize=5000 boundary", 1, 5000, 1, 5000},
  22. {"pageSize=10000 boundary", 1, 10000, 1, 10000},
  23. {"pageSize=10001 over limit", 1, 10001, 1, 10000},
  24. {"both zero", 0, 0, 1, 20},
  25. {"both negative", -3, -10, 1, 20},
  26. }
  27. for _, tt := range tests {
  28. t.Run(tt.name, func(t *testing.T) {
  29. p, s := NormalizePage(tt.page, tt.pageSize)
  30. assert.Equal(t, tt.wantP, p)
  31. assert.Equal(t, tt.wantSize, s)
  32. })
  33. }
  34. }
  35. // TC-0277: `[email protected]`
  36. func TestIsValidEmail(t *testing.T) {
  37. tests := []struct {
  38. name string
  39. email string
  40. want bool
  41. }{
  42. {"standard email", "[email protected]", true},
  43. {"with dots", "[email protected]", true},
  44. {"with plus", "[email protected]", true},
  45. {"with hyphen domain", "[email protected]", true},
  46. {"missing @", "userexample.com", false},
  47. {"missing domain", "user@", false},
  48. {"missing TLD", "user@example", false},
  49. {"empty string", "", false},
  50. {"double @", "user@@example.com", false},
  51. {"space in email", "user @example.com", false},
  52. {"short TLD", "[email protected]", false},
  53. {"subdomain", "[email protected]", true},
  54. }
  55. for _, tt := range tests {
  56. t.Run(tt.name, func(t *testing.T) {
  57. assert.Equal(t, tt.want, IsValidEmail(tt.email))
  58. })
  59. }
  60. }
  61. // TC-0284: `13800138000`
  62. func TestIsValidPhone(t *testing.T) {
  63. tests := []struct {
  64. name string
  65. phone string
  66. want bool
  67. }{
  68. {"chinese mobile", "13800138000", true},
  69. {"with country code", "+8613800138000", true},
  70. {"too short 6 digits", "123456", false},
  71. {"min 7 digits", "1234567", true},
  72. {"max 15 digits", "+123456789012345", true},
  73. {"over 16 digits", "1234567890123456", false},
  74. {"contains letters", "1380013abc", false},
  75. {"empty string", "", false},
  76. {"only plus", "+", false},
  77. {"plus with short", "+123456", false},
  78. {"leading zero not first", "01234567890", false},
  79. }
  80. for _, tt := range tests {
  81. t.Run(tt.name, func(t *testing.T) {
  82. assert.Equal(t, tt.want, IsValidPhone(tt.phone))
  83. })
  84. }
  85. }