github.com/MontFerret/ferret@v0.18.0/pkg/stdlib/datetime/add_subtract_test.go (about) 1 package datetime_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/MontFerret/ferret/pkg/runtime/core" 9 "github.com/MontFerret/ferret/pkg/runtime/values" 10 11 "github.com/MontFerret/ferret/pkg/stdlib/datetime" 12 ) 13 14 var ( 15 utcLoc, _ = time.LoadLocation("UTC") 16 ) 17 18 func TestDateAdd(t *testing.T) { 19 tcs := []*testCase{ 20 &testCase{ 21 Name: "When more than 3 arguments", 22 Expected: values.None, 23 Args: []core.Value{ 24 values.NewInt(0), 25 values.NewInt(0), 26 values.NewInt(0), 27 values.NewInt(0), 28 }, 29 ShouldErr: true, 30 }, 31 &testCase{ 32 Name: "When less than 3 arguments", 33 Expected: values.None, 34 Args: []core.Value{ 35 values.NewInt(0), 36 }, 37 ShouldErr: true, 38 }, 39 &testCase{ 40 Name: "When incorrect arguments", 41 Expected: values.None, 42 Args: []core.Value{ 43 values.NewString("bla-bla"), 44 values.NewInt(0), 45 values.NewString("be-be"), 46 }, 47 ShouldErr: true, 48 }, 49 &testCase{ 50 Name: "When wrong unit given", 51 Expected: values.None, 52 Args: []core.Value{ 53 mustLayoutDt("2006-01-02", "1999-02-07"), 54 values.NewInt(5), 55 values.NewString("not_exist"), 56 }, 57 ShouldErr: true, 58 }, 59 &testCase{ 60 Name: "When argument have correct types", 61 Expected: func() core.Value { 62 expected, _ := datetime.DateAdd( 63 context.Background(), 64 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 65 values.NewInt(1), 66 values.NewString("day"), 67 ) 68 return expected 69 }(), 70 Args: []core.Value{ 71 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 72 values.NewInt(1), 73 values.NewString("day"), 74 }, 75 }, 76 &testCase{ 77 Name: "-1 day", 78 Expected: mustDefaultLayoutDt("1999-02-06T15:04:05Z"), 79 Args: []core.Value{ 80 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 81 values.NewInt(-1), 82 values.NewString("day"), 83 }, 84 }, 85 &testCase{ 86 Name: "+3 months", 87 Expected: mustDefaultLayoutDt("1999-05-07T15:04:05Z"), 88 Args: []core.Value{ 89 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 90 values.NewInt(3), 91 values.NewString("months"), 92 }, 93 }, 94 &testCase{ 95 Name: "+5 years", 96 Expected: mustLayoutDt("2006-01-02", "2004-02-07"), 97 Args: []core.Value{ 98 mustLayoutDt("2006-01-02", "1999-02-07"), 99 values.NewInt(5), 100 values.NewString("y"), 101 }, 102 }, 103 &testCase{ 104 Name: "1999 minus 2000 years", 105 Expected: values.NewDateTime( 106 time.Date(-1, 2, 7, 0, 0, 0, 0, utcLoc), 107 ), 108 Args: []core.Value{ 109 mustLayoutDt("2006-01-02", "1999-02-07"), 110 values.NewInt(-2000), 111 values.NewString("year"), 112 }, 113 }, 114 &testCase{ 115 Name: "+2 hours", 116 Expected: mustDefaultLayoutDt("1999-02-07T17:04:05Z"), 117 Args: []core.Value{ 118 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 119 values.NewInt(2), 120 values.NewString("h"), 121 }, 122 }, 123 &testCase{ 124 Name: "+20 minutes", 125 Expected: mustDefaultLayoutDt("1999-02-07T15:24:05Z"), 126 Args: []core.Value{ 127 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 128 values.NewInt(20), 129 values.NewString("i"), 130 }, 131 }, 132 &testCase{ 133 Name: "+30 seconds", 134 Expected: mustDefaultLayoutDt("1999-02-07T15:04:35Z"), 135 Args: []core.Value{ 136 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 137 values.NewInt(30), 138 values.NewString("s"), 139 }, 140 }, 141 &testCase{ 142 Name: "+1000 milliseconds", 143 Expected: mustDefaultLayoutDt("1999-02-07T15:04:06Z"), 144 Args: []core.Value{ 145 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 146 values.NewInt(1000), 147 values.NewString("f"), 148 }, 149 }, 150 } 151 152 for _, tc := range tcs { 153 tc.Do(t, datetime.DateAdd) 154 } 155 } 156 157 func TestDateSubtract(t *testing.T) { 158 tcs := []*testCase{ 159 &testCase{ 160 Name: "When more than 3 arguments", 161 Expected: values.None, 162 Args: []core.Value{ 163 values.NewInt(0), 164 values.NewInt(0), 165 values.NewInt(0), 166 values.NewInt(0), 167 }, 168 ShouldErr: true, 169 }, 170 &testCase{ 171 Name: "When less than 3 arguments", 172 Expected: values.None, 173 Args: []core.Value{ 174 values.NewInt(0), 175 }, 176 ShouldErr: true, 177 }, 178 &testCase{ 179 Name: "When incorrect arguments", 180 Expected: values.None, 181 Args: []core.Value{ 182 values.NewString("bla-bla"), 183 values.NewInt(0), 184 values.NewString("be-be"), 185 }, 186 ShouldErr: true, 187 }, 188 &testCase{ 189 Name: "When wrong unit given", 190 Expected: values.None, 191 Args: []core.Value{ 192 mustLayoutDt("2006-01-02", "1999-02-07"), 193 values.NewInt(5), 194 values.NewString("not_exist"), 195 }, 196 ShouldErr: true, 197 }, 198 &testCase{ 199 Name: "When argument have correct types", 200 Expected: func() core.Value { 201 expected, _ := datetime.DateSubtract( 202 context.Background(), 203 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 204 values.NewInt(1), 205 values.NewString("day"), 206 ) 207 return expected 208 }(), 209 Args: []core.Value{ 210 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 211 values.NewInt(1), 212 values.NewString("day"), 213 }, 214 }, 215 &testCase{ 216 Name: "-1 day", 217 Expected: mustDefaultLayoutDt("1999-02-08T15:04:05Z"), 218 Args: []core.Value{ 219 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 220 values.NewInt(-1), 221 values.NewString("day"), 222 }, 223 }, 224 &testCase{ 225 Name: "+3 months", 226 Expected: mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 227 Args: []core.Value{ 228 mustDefaultLayoutDt("1999-05-07T15:04:05Z"), 229 values.NewInt(3), 230 values.NewString("months"), 231 }, 232 }, 233 &testCase{ 234 Name: "+5 years", 235 Expected: mustLayoutDt("2006-01-02", "1994-02-07"), 236 Args: []core.Value{ 237 mustLayoutDt("2006-01-02", "1999-02-07"), 238 values.NewInt(5), 239 values.NewString("y"), 240 }, 241 }, 242 &testCase{ 243 Name: "1999 minus 2000 years", 244 Expected: values.NewDateTime( 245 time.Date(-1, 2, 7, 0, 0, 0, 0, utcLoc), 246 ), 247 Args: []core.Value{ 248 mustLayoutDt("2006-01-02", "1999-02-07"), 249 values.NewInt(2000), 250 values.NewString("year"), 251 }, 252 }, 253 &testCase{ 254 Name: "-2 hours", 255 Expected: mustDefaultLayoutDt("1999-02-07T13:04:05Z"), 256 Args: []core.Value{ 257 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 258 values.NewInt(2), 259 values.NewString("h"), 260 }, 261 }, 262 &testCase{ 263 Name: "-20 minutes", 264 Expected: mustDefaultLayoutDt("1999-02-07T14:44:05Z"), 265 Args: []core.Value{ 266 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 267 values.NewInt(20), 268 values.NewString("i"), 269 }, 270 }, 271 &testCase{ 272 Name: "-30 seconds", 273 Expected: mustDefaultLayoutDt("1999-02-07T15:03:35Z"), 274 Args: []core.Value{ 275 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 276 values.NewInt(30), 277 values.NewString("s"), 278 }, 279 }, 280 &testCase{ 281 Name: "-1000 milliseconds", 282 Expected: mustDefaultLayoutDt("1999-02-07T15:04:04Z"), 283 Args: []core.Value{ 284 mustDefaultLayoutDt("1999-02-07T15:04:05Z"), 285 values.NewInt(1000), 286 values.NewString("f"), 287 }, 288 }, 289 } 290 291 for _, tc := range tcs { 292 tc.Do(t, datetime.DateSubtract) 293 } 294 }