github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/tests/integration/table_interval_from_duration_test.go (about) 1 //go:build integration 2 // +build integration 3 4 package integration 5 6 import ( 7 "context" 8 "fmt" 9 "os" 10 "testing" 11 "time" 12 13 "github.com/stretchr/testify/require" 14 15 "github.com/ydb-platform/ydb-go-sdk/v3" 16 "github.com/ydb-platform/ydb-go-sdk/v3/table" 17 "github.com/ydb-platform/ydb-go-sdk/v3/table/types" 18 ) 19 20 // https://github.com/ydb-platform/ydb-go-sdk/issues/259 21 func TestIssue259IntervalFromDuration(t *testing.T) { 22 ctx, cancel := context.WithCancel(context.Background()) 23 defer cancel() 24 25 db, err := ydb.Open(ctx, 26 os.Getenv("YDB_CONNECTION_STRING"), 27 ydb.WithAccessTokenCredentials(os.Getenv("YDB_ACCESS_TOKEN_CREDENTIALS")), 28 ) 29 require.NoError(t, err) 30 31 t.Run("Check about interval work with microseconds", func(t *testing.T) { 32 err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error { 33 // 34 res, err := tx.Execute(ctx, `DECLARE $ts as Interval; 35 $ten_micro = CAST(10 as Interval); 36 SELECT $ts == $ten_micro, $ten_micro;`, table.NewQueryParameters( 37 table.ValueParam(`$ts`, types.IntervalValueFromDuration(10*time.Microsecond)), 38 )) 39 if err != nil { 40 return err 41 } 42 if err = res.NextResultSetErr(ctx); err != nil { 43 return err 44 } 45 if !res.NextRow() { 46 return fmt.Errorf("unexpected no rows in result set (err = %w)", res.Err()) 47 } 48 var ( 49 valuesEqual bool 50 tenMicro time.Duration 51 ) 52 if err = res.Scan(&valuesEqual, &tenMicro); err != nil { 53 return err 54 } 55 if !valuesEqual { 56 return fmt.Errorf("unexpected values equal (err = %w)", res.Err()) 57 } 58 if tenMicro != 10*time.Microsecond { 59 return fmt.Errorf("unexpected ten micro equal: %v (err = %w)", tenMicro, res.Err()) 60 } 61 return res.Err() 62 }, table.WithIdempotent()) 63 require.NoError(t, err) 64 }) 65 66 t.Run("Check about parse interval represent date interval", func(t *testing.T) { 67 err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error { 68 // 69 query := ` 70 SELECT 71 DateTime::MakeTimestamp(DateTime::ParseIso8601("2009-02-14T02:31:30+0000")) - 72 DateTime::MakeTimestamp(DateTime::ParseIso8601("2009-02-14T01:31:30+0000")) 73 ` 74 res, err := tx.Execute(ctx, query, nil) 75 if err != nil { 76 return err 77 } 78 if err = res.NextResultSetErr(ctx); err != nil { 79 return err 80 } 81 if !res.NextRow() { 82 return fmt.Errorf("unexpected no rows in result set (err = %w)", res.Err()) 83 } 84 var delta time.Duration 85 if err = res.ScanWithDefaults(&delta); err != nil { 86 return err 87 } 88 if delta != time.Hour { 89 return fmt.Errorf("unexpected ten micro equal: %v (err = %w)", delta, res.Err()) 90 } 91 return res.Err() 92 }, table.WithIdempotent()) 93 require.NoError(t, err) 94 }) 95 96 t.Run("check about send interval work find with dates", func(t *testing.T) { 97 err := db.Table().DoTx(ctx, func(ctx context.Context, tx table.TransactionActor) error { 98 // 99 query := ` 100 DECLARE $delta AS Interval; 101 102 SELECT 103 DateTime::MakeTimestamp(DateTime::ParseIso8601("2009-02-14T01:31:30+0000")) + $delta == 104 DateTime::MakeTimestamp(DateTime::ParseIso8601("2009-02-14T02:31:30+0000")) 105 ` 106 res, err := tx.Execute(ctx, query, table.NewQueryParameters( 107 table.ValueParam("$delta", types.IntervalValueFromDuration(time.Hour))), 108 ) 109 if err != nil { 110 return err 111 } 112 if err = res.NextResultSetErr(ctx); err != nil { 113 return err 114 } 115 if !res.NextRow() { 116 return fmt.Errorf("unexpected no rows in result set (err = %w)", res.Err()) 117 } 118 var valuesEqual bool 119 if err = res.ScanWithDefaults(&valuesEqual); err != nil { 120 return err 121 } 122 if !valuesEqual { 123 return fmt.Errorf("unexpected values equal (err = %w)", res.Err()) 124 } 125 return res.Err() 126 }, table.WithIdempotent()) 127 require.NoError(t, err) 128 }) 129 }