@@ -3,13 +3,15 @@ package ydb_test
33import (
44 "fmt"
55 "testing"
6+ "time"
67
78 "github.com/google/uuid"
89 "github.com/stretchr/testify/require"
910 "github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
1011
1112 "github.com/ydb-platform/ydb-go-sdk/v3"
1213 "github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
14+ "github.com/ydb-platform/ydb-go-sdk/v3/internal/bind"
1315 "github.com/ydb-platform/ydb-go-sdk/v3/internal/params"
1416 "github.com/ydb-platform/ydb-go-sdk/v3/table"
1517 "github.com/ydb-platform/ydb-go-sdk/v3/table/types"
@@ -218,3 +220,89 @@ func BenchmarkParams(b *testing.B) {
218220 }
219221 })
220222}
223+
224+ func TestParamsFromMap (t * testing.T ) {
225+ t .Run ("DefaultTimeTypes" , func (t * testing.T ) {
226+ params := ydb .ParamsFromMap (map [string ]any {
227+ "a" : time .Unix (123 , 456 ),
228+ "b" : time .Duration (123 ) * time .Microsecond ,
229+ })
230+ pp , err := params .ToYDB (& allocator.Allocator {})
231+ require .NoError (t , err )
232+ require .EqualValues (t ,
233+ fmt .Sprintf ("%+v" , map [string ]* Ydb.TypedValue {
234+ "$a" : {
235+ Type : & Ydb.Type {
236+ Type : & Ydb.Type_TypeId {
237+ TypeId : Ydb .Type_TIMESTAMP ,
238+ },
239+ },
240+ Value : & Ydb.Value {
241+ Value : & Ydb.Value_Uint64Value {
242+ Uint64Value : 123000000 ,
243+ },
244+ },
245+ },
246+ "$b" : {
247+ Type : & Ydb.Type {
248+ Type : & Ydb.Type_TypeId {
249+ TypeId : Ydb .Type_INTERVAL ,
250+ },
251+ },
252+ Value : & Ydb.Value {
253+ Value : & Ydb.Value_Int64Value {
254+ Int64Value : 123 ,
255+ },
256+ },
257+ },
258+ }),
259+ fmt .Sprintf ("%+v" , pp ),
260+ )
261+ })
262+ t .Run ("BindWideTimeTypes" , func (t * testing.T ) {
263+ params := ydb .ParamsFromMap (map [string ]any {
264+ "a" : time .Date (1900 , 1 , 1 , 0 , 0 , 0 , 123456 , time .UTC ),
265+ "b" : time .Duration (123 ) * time .Nanosecond ,
266+ }, ydb .WithWideTimeTypes (true ))
267+ pp , err := params .ToYDB (& allocator.Allocator {})
268+ require .NoError (t , err )
269+ require .EqualValues (t ,
270+ fmt .Sprintf ("%+v" , map [string ]* Ydb.TypedValue {
271+ "$a" : {
272+ Type : & Ydb.Type {
273+ Type : & Ydb.Type_TypeId {
274+ TypeId : Ydb .Type_TIMESTAMP64 ,
275+ },
276+ },
277+ Value : & Ydb.Value {
278+ Value : & Ydb.Value_Int64Value {
279+ Int64Value : - 2208988799999877 ,
280+ },
281+ },
282+ },
283+ "$b" : {
284+ Type : & Ydb.Type {
285+ Type : & Ydb.Type_TypeId {
286+ TypeId : Ydb .Type_INTERVAL64 ,
287+ },
288+ },
289+ Value : & Ydb.Value {
290+ Value : & Ydb.Value_Int64Value {
291+ Int64Value : 123 ,
292+ },
293+ },
294+ },
295+ }),
296+ fmt .Sprintf ("%+v" , pp ),
297+ )
298+ })
299+ t .Run ("WrongBindings" , func (t * testing.T ) {
300+ params := ydb .ParamsFromMap (map [string ]any {
301+ "a" : time .Unix (123 , 456 ),
302+ "b" : time .Duration (123 ),
303+ }, ydb .WithTablePathPrefix ("" ))
304+ pp , err := params .ToYDB (& allocator.Allocator {})
305+ require .ErrorIs (t , err , bind .ErrUnsupportedBindingType )
306+ require .Nil (t , pp )
307+ })
308+ }
0 commit comments