github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/internal/service/schedule/execute_at_test.go (about) 1 package schedule 2 3 import ( 4 "testing" 5 "time" 6 7 _time "github.com/sharovik/devbot/internal/service/time" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func init() { 13 _time.InitNOW(time.UTC) 14 } 15 16 func TestExecuteAt_IsEmpty(t *testing.T) { 17 cases := []ExecuteAt{ 18 { 19 Minutes: 1, 20 }, 21 { 22 Hours: 1, 23 }, 24 { 25 ExactDatetime: _time.Service.Now(), 26 }, 27 } 28 29 for _, actual := range cases { 30 assert.False(t, actual.IsEmpty()) 31 } 32 33 assert.True(t, new(ExecuteAt).IsEmpty()) 34 } 35 36 func TestExecuteAt_parseDays(t *testing.T) { 37 var ( 38 cases = map[string]int64{ 39 "in 2 days": int64(2), 40 "after 1 day": int64(1), 41 "every day": int64(0), 42 } 43 ) 44 45 for text, expected := range cases { 46 e := ExecuteAt{} 47 err := e.parseDays(text) 48 assert.NoError(t, err) 49 assert.Equal(t, expected, e.Days) 50 } 51 } 52 53 func TestExecuteAt_parse(t *testing.T) { 54 var ( 55 cases = map[string]interface{}{ 56 "in 2 hours": 2, 57 "after 1 hour": 1, 58 "every hour": nil, 59 } 60 ) 61 62 for text, expected := range cases { 63 e := ExecuteAt{} 64 hours, err := e.parse(text, HourRegexp) 65 assert.NoError(t, err) 66 assert.Equal(t, expected, hours) 67 } 68 69 cases = map[string]interface{}{ 70 "in 20 minutes": 20, 71 "every minute": nil, 72 } 73 74 for text, expected := range cases { 75 e := ExecuteAt{} 76 minutes, err := e.parse(text, MinuteRegexp) 77 assert.NoError(t, err) 78 assert.Equal(t, expected, minutes) 79 } 80 } 81 82 func TestExecuteAt_isRepeatable(t *testing.T) { 83 var ( 84 cases = map[string]bool{ 85 "repeat daily": true, 86 "every hour": true, 87 "repeat 1 days and 9 hours and 30 minutes": true, 88 "in 1 hour": false, 89 } 90 ) 91 92 for text, expected := range cases { 93 e := ExecuteAt{} 94 assert.Equal(t, expected, e.isRepeatable(text), text) 95 } 96 } 97 98 func TestExecuteAt_isDelayed(t *testing.T) { 99 var ( 100 cases = map[string]bool{ 101 "in few hours": true, 102 "every hour": false, 103 "after 1 hour": true, 104 } 105 ) 106 107 for text, expected := range cases { 108 e := ExecuteAt{} 109 assert.Equal(t, expected, e.isDelayed(text), text) 110 } 111 } 112 113 func TestExecuteAt_getDatetime(t *testing.T) { 114 var ( 115 actual ExecuteAt 116 expectedDate time.Time 117 ct = _time.Service.Now() 118 err error 119 ) 120 121 actual, err = new(ExecuteAt).FromString("schedule event examplescenario every 1 minute") 122 assert.NoError(t, err) 123 expectedDate = time.Date(ct.Year(), ct.Month(), ct.Day(), ct.Hour(), ct.Minute()+1, 0, 0, ct.Location()) 124 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 125 126 actual, err = new(ExecuteAt).FromString("in 1 hour and 2 minutes") 127 assert.NoError(t, err) 128 expectedDate = time.Date(ct.Year(), ct.Month(), ct.Day(), ct.Hour()+1, ct.Minute()+2, 0, 0, ct.Location()) 129 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 130 131 actual, err = new(ExecuteAt).FromString("1 hour") 132 assert.NoError(t, err) 133 expectedDate = time.Date(ct.Year(), ct.Month(), ct.Day(), ct.Hour(), ct.Minute()+60, 0, 0, ct.Location()) 134 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 135 136 actual, err = new(ExecuteAt).FromString("23 minutes") 137 assert.NoError(t, err) 138 expectedDate = time.Date(ct.Year(), ct.Month(), ct.Day(), ct.Hour(), ct.Minute()+23, 0, 0, ct.Location()) 139 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 140 141 actual, err = new(ExecuteAt).FromString("2022-12-18 11:22") 142 assert.NoError(t, err) 143 expectedDate, err = time.Parse(timeFormat, "2022-12-18 11:22") 144 assert.NoError(t, err) 145 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 146 147 actual, err = new(ExecuteAt).FromString("in 20 minutes") 148 assert.NoError(t, err) 149 expectedDate = time.Date(ct.Year(), ct.Month(), ct.Day(), ct.Hour(), ct.Minute()+20, 0, 0, ct.Location()) 150 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 151 152 actual, err = new(ExecuteAt).FromString("in 1 day") 153 assert.NoError(t, err) 154 expectedDate = time.Date(ct.Year(), ct.Month(), ct.Day(), ct.Hour()+24, ct.Minute(), 0, 0, ct.Location()) 155 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 156 157 actual, err = new(ExecuteAt).FromString("2 days") 158 assert.NoError(t, err) 159 expectedDate = time.Date(ct.Year(), ct.Month(), ct.Day()+2, ct.Hour(), ct.Minute(), 0, 0, ct.Location()) 160 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 161 162 actual, err = new(ExecuteAt).FromString("repeat 1 days and at 9:30") 163 assert.NoError(t, err) 164 expectedDate = time.Date(ct.Year(), ct.Month(), ct.Day()+1, 9, 30, 0, 0, ct.Location()) 165 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 166 assert.Equal(t, "repeat 1 days and at 9:30", actual.toString()) 167 } 168 169 func TestExecuteAt_ParseDays(t *testing.T) { 170 var ( 171 actual ExecuteAt 172 expectedDate time.Time 173 ct = _time.Service.Now() 174 err error 175 ) 176 177 days := int((7 + (time.Sunday - ct.Weekday())) % 7) 178 now := _time.Service.Now() 179 _, _, d := now.AddDate(0, 0, days).Date() 180 181 actual, err = new(ExecuteAt).FromString("Sunday at 10:00") 182 assert.NoError(t, err) 183 expectedDate = time.Date(ct.Year(), ct.Month(), d, 10, 00, 0, 0, ct.Location()) 184 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 185 assert.Equal(t, "Sunday and at 10:0", actual.toString()) 186 187 actual, err = new(ExecuteAt).FromString("every monday at 9:10") 188 assert.NoError(t, err) 189 190 days = int((7 + (time.Monday - ct.Weekday())) % 7) 191 now = _time.Service.Now() 192 _, _, d = now.AddDate(0, 0, days).Date() 193 expectedDate = time.Date(ct.Year(), ct.Month(), d, 9, 10, 0, 0, ct.Location()) 194 assert.Equal(t, expectedDate.Format(timeFormat), actual.getDatetime().Format(timeFormat)) 195 assert.Equal(t, "repeat Monday and at 9:10", actual.toString()) 196 }