utils_test.go 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package utils_test
  2. import (
  3. "wechat-conf/utils"
  4. "strings"
  5. "testing"
  6. "time"
  7. )
  8. func TestStrSliceIndexOf(t *testing.T) {
  9. cases := []map[string]interface{}{
  10. map[string]interface{}{
  11. "case": []string{"a", "b", "c"},
  12. "expected": int64(1),
  13. },
  14. map[string]interface{}{
  15. "case": []string{"aa", "bb", "cc"},
  16. "expected": int64(-1),
  17. },
  18. }
  19. for _, cs := range cases {
  20. td := cs["case"].([]string)
  21. ex := cs["expected"].(int64)
  22. res := utils.StrSliceIndexOf(td, "b")
  23. if ex != res {
  24. t.Fatalf("TestStrSliceIndexOf case fail : %v", res)
  25. }
  26. }
  27. }
  28. func TestGUIID2IntString(t *testing.T) {
  29. cases := []map[string]string{
  30. map[string]string{
  31. "case": "3b2da085-8881-4029-9a3d-6d33caf870d0",
  32. "expected": "553-217-207-305-861",
  33. },
  34. }
  35. for _, cs := range cases {
  36. res := utils.GUIID2IntString(cs["case"])
  37. if strings.Join(res, "-") != cs["expected"] {
  38. t.Fatalf("TestGUIID2IntString fail")
  39. }
  40. }
  41. }
  42. func TestEncodeBase64TrimTail(t *testing.T) {
  43. cases := []map[string]string{
  44. map[string]string{
  45. "case": "1",
  46. "expected": "MQ",
  47. },
  48. map[string]string{
  49. "case": "111",
  50. "expected": "MTEx",
  51. },
  52. }
  53. for _, cs := range cases {
  54. res := utils.EncodeBase64TrimTail(cs["case"])
  55. if res != cs["expected"] {
  56. t.Fatalf("TestEncodeBase64TrimTail fail")
  57. }
  58. }
  59. }
  60. func TestDecodeBase64NoTail(t *testing.T) {
  61. cases := []map[string]string{
  62. map[string]string{
  63. "case": "MQ",
  64. "expected": "1",
  65. },
  66. map[string]string{
  67. "case": "MTEx",
  68. "expected": "111",
  69. },
  70. }
  71. for _, cs := range cases {
  72. res := utils.DecodeBase64NoTail(cs["case"])
  73. if res != cs["expected"] {
  74. t.Fatalf("TestDecodeBase64NoTail fail : %s", cs["case"])
  75. }
  76. }
  77. }
  78. func TestGetFiveSeconds(t *testing.T) {
  79. cases := []map[string]string{
  80. map[string]string{
  81. "case": "2018-10-07 13:21:40",
  82. "expected": "20181007132140",
  83. },
  84. map[string]string{
  85. "case": "2018-10-07 13:21:42",
  86. "expected": "20181007132140",
  87. },
  88. map[string]string{
  89. "case": "2018-10-07 13:21:45",
  90. "expected": "20181007132145",
  91. },
  92. map[string]string{
  93. "case": "2018-10-07 13:21:48",
  94. "expected": "20181007132145",
  95. },
  96. }
  97. for _, cs := range cases {
  98. dt, _ := time.Parse("2006-01-02 15:04:05", cs["case"])
  99. res := utils.GetFiveSeconds(dt)
  100. if res != cs["expected"] {
  101. t.Fatalf("TestGetFiveSeconds fail : %s", cs["case"])
  102. }
  103. }
  104. }