github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/params/parameters_test.go (about)

     1  package params
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xtest"
    11  )
    12  
    13  func TestParameter(t *testing.T) {
    14  	p := Named("x", value.TextValue("X"))
    15  	require.Equal(t, "x", p.Name())
    16  	require.EqualValues(t, "X", p.Value())
    17  	require.Equal(t, "DECLARE x AS Utf8", Declare(p))
    18  }
    19  
    20  func TestParameters(t *testing.T) {
    21  	p := &Parameters{}
    22  	p.Add(
    23  		Named("x", value.TextValue("X")),
    24  		Named("y", value.TextValue("Y")),
    25  	)
    26  	require.Equal(t, "{\"x\":\"X\"u,\"y\":\"Y\"u}", p.String())
    27  	require.Equal(t, 2, p.Count())
    28  	visited := make(map[string]value.Value, 2)
    29  	p.Each(func(name string, v value.Value) {
    30  		visited[name] = v
    31  	})
    32  	require.Len(t, visited, 2)
    33  	require.EqualValues(t, map[string]value.Value{
    34  		"x": value.TextValue("X"),
    35  		"y": value.TextValue("Y"),
    36  	}, visited)
    37  }
    38  
    39  func TestNil(t *testing.T) {
    40  	for _, tt := range []struct {
    41  		name string
    42  		p    *Parameters
    43  	}{
    44  		{
    45  			name: xtest.CurrentFileLine(),
    46  			p:    nil,
    47  		},
    48  		{
    49  			name: xtest.CurrentFileLine(),
    50  			p:    &Parameters{},
    51  		},
    52  		{
    53  			name: xtest.CurrentFileLine(),
    54  			p:    Builder{}.Build(),
    55  		},
    56  	} {
    57  		t.Run(tt.name, func(t *testing.T) {
    58  			require.Equal(t, "{}", tt.p.String())
    59  			require.Equal(t, 0, tt.p.Count())
    60  			visited := make(map[string]value.Value, 1)
    61  			tt.p.Each(func(name string, v value.Value) {
    62  				visited[name] = v
    63  			})
    64  			require.Empty(t, visited)
    65  			a := allocator.New()
    66  			defer a.Free()
    67  			require.Empty(t, tt.p.ToYDB(a))
    68  		})
    69  	}
    70  }