@@ -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,83 @@ 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 , map [string ]* Ydb.TypedValue {
233+ "$a" : {
234+ Type : & Ydb.Type {
235+ Type : & Ydb.Type_TypeId {
236+ TypeId : Ydb .Type_TIMESTAMP ,
237+ },
238+ },
239+ Value : & Ydb.Value {
240+ Value : & Ydb.Value_Uint64Value {
241+ Uint64Value : 123000000 ,
242+ },
243+ },
244+ },
245+ "$b" : {
246+ Type : & Ydb.Type {
247+ Type : & Ydb.Type_TypeId {
248+ TypeId : Ydb .Type_INTERVAL ,
249+ },
250+ },
251+ Value : & Ydb.Value {
252+ Value : & Ydb.Value_Int64Value {
253+ Int64Value : 123 ,
254+ },
255+ },
256+ },
257+ }, pp )
258+ })
259+ t .Run ("BindWideTimeTypes" , func (t * testing.T ) {
260+ params := ydb .ParamsFromMap (map [string ]any {
261+ "a" : time .Date (1900 , 1 , 1 , 0 , 0 , 0 , 123456 , time .UTC ),
262+ "b" : time .Duration (123 ) * time .Nanosecond ,
263+ }, ydb .WithWideTimeTypes ())
264+ pp , err := params .ToYDB (& allocator.Allocator {})
265+ require .NoError (t , err )
266+ require .EqualValues (t , map [string ]* Ydb.TypedValue {
267+ "$a" : {
268+ Type : & Ydb.Type {
269+ Type : & Ydb.Type_TypeId {
270+ TypeId : Ydb .Type_TIMESTAMP64 ,
271+ },
272+ },
273+ Value : & Ydb.Value {
274+ Value : & Ydb.Value_Int64Value {
275+ Int64Value : - 2208988799999877 ,
276+ },
277+ },
278+ },
279+ "$b" : {
280+ Type : & Ydb.Type {
281+ Type : & Ydb.Type_TypeId {
282+ TypeId : Ydb .Type_INTERVAL64 ,
283+ },
284+ },
285+ Value : & Ydb.Value {
286+ Value : & Ydb.Value_Int64Value {
287+ Int64Value : 123 ,
288+ },
289+ },
290+ },
291+ }, pp )
292+ })
293+ t .Run ("WrongBindings" , func (t * testing.T ) {
294+ params := ydb .ParamsFromMap (map [string ]any {
295+ "a" : time .Unix (123 , 456 ),
296+ "b" : time .Duration (123 ),
297+ }, ydb .WithTablePathPrefix ("" ))
298+ pp , err := params .ToYDB (& allocator.Allocator {})
299+ require .ErrorIs (t , err , bind .ErrUnsupportedBindingType )
300+ require .Nil (t , pp )
301+ })
302+ }
0 commit comments