1
0

file_name_util_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. package util
  2. import (
  3. "strconv"
  4. "testing"
  5. "time"
  6. )
  7. // TestFileNameUtil 测试正常情况
  8. func TestFileNameUtil(t *testing.T) {
  9. const days = 10
  10. beforeTomorrow, _ := time.ParseDuration("-" + strconv.Itoa((days+1)*24) + "h")
  11. fileNames := []string{
  12. "a2020-10-10-11-12b.sql",
  13. "测试2021-10-10-11-12测试.sql",
  14. time.Now().Add(beforeTomorrow).Format(FileNameFormatStr) + ".sql",
  15. time.Now().Format(FileNameFormatStr) + ".sql",
  16. }
  17. deleteFiles := FileNameBeforeDays(days, fileNames, "test")
  18. // 只有一个不会被删除,需要+1
  19. if len(fileNames) != len(deleteFiles)+1 {
  20. t.Error("TestFileNameUtil Test failed!")
  21. }
  22. }
  23. // TestFileNameUtilAll 测试全部删除
  24. func TestFileNameUtilAll(t *testing.T) {
  25. const days = 10
  26. beforeTomorrow, _ := time.ParseDuration("-" + strconv.Itoa((days+1)*24) + "h")
  27. fileNames := []string{
  28. "a2020-10-10-11-12b.sql",
  29. "测试2021-10-10-11-12测试.sql",
  30. time.Now().Add(beforeTomorrow).Format(FileNameFormatStr) + ".sql",
  31. }
  32. deleteFiles := FileNameBeforeDays(days, fileNames, "test")
  33. if len(deleteFiles) != 0 {
  34. t.Error("TestFileNameUtilAll Test failed!")
  35. }
  36. }