You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils_test.go 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package actions
  4. import (
  5. "math"
  6. "testing"
  7. "time"
  8. "code.gitea.io/gitea/modules/timeutil"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. )
  12. func TestLogIndexes_ToDB(t *testing.T) {
  13. tests := []struct {
  14. indexes LogIndexes
  15. }{
  16. {
  17. indexes: []int64{1, 2, 0, -1, -2, math.MaxInt64, math.MinInt64},
  18. },
  19. }
  20. for _, tt := range tests {
  21. t.Run("", func(t *testing.T) {
  22. got, err := tt.indexes.ToDB()
  23. require.NoError(t, err)
  24. indexes := LogIndexes{}
  25. require.NoError(t, indexes.FromDB(got))
  26. assert.Equal(t, tt.indexes, indexes)
  27. })
  28. }
  29. }
  30. func Test_calculateDuration(t *testing.T) {
  31. oldTimeSince := timeSince
  32. defer func() {
  33. timeSince = oldTimeSince
  34. }()
  35. timeSince = func(t time.Time) time.Duration {
  36. return timeutil.TimeStamp(1000).AsTime().Sub(t)
  37. }
  38. type args struct {
  39. started timeutil.TimeStamp
  40. stopped timeutil.TimeStamp
  41. status Status
  42. }
  43. tests := []struct {
  44. name string
  45. args args
  46. want time.Duration
  47. }{
  48. {
  49. name: "unknown",
  50. args: args{
  51. started: 0,
  52. stopped: 0,
  53. status: StatusUnknown,
  54. },
  55. want: 0,
  56. },
  57. {
  58. name: "running",
  59. args: args{
  60. started: 500,
  61. stopped: 0,
  62. status: StatusRunning,
  63. },
  64. want: 500 * time.Second,
  65. },
  66. {
  67. name: "done",
  68. args: args{
  69. started: 500,
  70. stopped: 600,
  71. status: StatusSuccess,
  72. },
  73. want: 100 * time.Second,
  74. },
  75. }
  76. for _, tt := range tests {
  77. t.Run(tt.name, func(t *testing.T) {
  78. assert.Equalf(t, tt.want, calculateDuration(tt.args.started, tt.args.stopped, tt.args.status), "calculateDuration(%v, %v, %v)", tt.args.started, tt.args.stopped, tt.args.status)
  79. })
  80. }
  81. }