Skip to content

Commit c04a7f5

Browse files
committed
1
1 parent 82c0511 commit c04a7f5

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

datetime/interval_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,90 @@ func TestIntervalStringIntegration(t *testing.T) {
284284
})
285285
}
286286

287+
func TestIntervalStringFromTarantool(t *testing.T) {
288+
skipIfDatetimeUnsupported(t)
289+
290+
conn := test_helpers.ConnectWithValidation(t, dialer, opts)
291+
defer conn.Close()
292+
293+
testCases := []struct {
294+
luaExpr string
295+
expected string
296+
}{
297+
{
298+
"return require('datetime').interval.new({})",
299+
"0 seconds",
300+
},
301+
{
302+
"return require('datetime').interval.new({year = 1})",
303+
"+1 year",
304+
},
305+
{
306+
"return require('datetime').interval.new({year = -1})",
307+
"-1 year",
308+
},
309+
{
310+
"return require('datetime').interval.new({year = 5})",
311+
"+5 years",
312+
},
313+
{
314+
"return require('datetime').interval.new({year = 1, month = 2, day = 3})",
315+
"+1 year, 2 months, 3 days",
316+
},
317+
{
318+
"return require('datetime').interval.new({hour = 1, min = 30, sec = 45})",
319+
"+1 hour, 30 minutes, 45 seconds",
320+
},
321+
{
322+
"return require('datetime').interval.new({year = -1, month = 2, week = -3, day = 4, hour = -5, min = 6, sec = -7, nsec = 9})",
323+
"-1 year, 2 months, -3 weeks, 4 days, -5 hours, 6 minutes, -7 seconds, 9 nanoseconds",
324+
},
325+
{
326+
"return require('datetime').interval.new({sec = 5, nsec = 123456789})",
327+
"+5 seconds, 123456789 nanoseconds",
328+
},
329+
{
330+
"return require('datetime').interval.new({sec = -5, nsec = -123456789})",
331+
"-5 seconds, -123456789 nanoseconds",
332+
},
333+
{
334+
"return require('datetime').interval.new({nsec = 500000000})",
335+
"+500000000 nanoseconds",
336+
},
337+
{
338+
"return require('datetime').interval.new({nsec = -500000000})",
339+
"-500000000 nanoseconds",
340+
},
341+
{
342+
"return require('datetime').interval.new({year = 1, month = 6, week = 2, day = 3, hour = 12, min = 30, sec = 45, nsec = 123456789})",
343+
"+1 year, 6 months, 2 weeks, 3 days, 12 hours, 30 minutes, 45 seconds, 123456789 nanoseconds",
344+
},
345+
{
346+
"return require('datetime').interval.new({year = -1, day = -2, hour = -3})",
347+
"-1 year, -2 days, -3 hours",
348+
},
349+
}
350+
351+
for _, tc := range testCases {
352+
t.Run(tc.expected, func(t *testing.T) {
353+
data, err := conn.Do(tarantool.NewEvalRequest(tc.luaExpr)).Get()
354+
if err != nil {
355+
t.Fatalf("Eval failed: %s", err)
356+
}
357+
if len(data) != 1 {
358+
t.Fatalf("Expected 1 result, got %d", len(data))
359+
}
360+
ival, ok := data[0].(Interval)
361+
if !ok {
362+
t.Fatalf("Result is not Interval: %T", data[0])
363+
}
364+
if got := ival.String(); got != tc.expected {
365+
t.Errorf("String() = %q, want %q", got, tc.expected)
366+
}
367+
})
368+
}
369+
}
370+
287371
func TestIntervalStringEdgeCases(t *testing.T) {
288372
tests := []struct {
289373
name string

0 commit comments

Comments
 (0)