github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/sugar/dsn_test.go (about)

     1  package sugar
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/config"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/dsn"
    10  )
    11  
    12  func TestDSN(t *testing.T) {
    13  	for _, tt := range []struct {
    14  		endpoint string
    15  		database string
    16  		secure   bool
    17  		dsn      string
    18  	}{
    19  		{
    20  			"localhost:2135",
    21  			"/local",
    22  			false,
    23  			"grpc://localhost:2135/local",
    24  		},
    25  		{
    26  			"ydb-ru.yandex.net:2135",
    27  			"/ru/home/gvit/mydb",
    28  			false,
    29  			"grpc://ydb-ru.yandex.net:2135/ru/home/gvit/mydb",
    30  		},
    31  		{
    32  			"ydb.serverless.yandexcloud.net:2135",
    33  			"/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1",
    34  			true,
    35  			"grpcs://ydb.serverless.yandexcloud.net:2135/ru-central1/b1g8skpblkos03malf3s/etn02qso4v3isjb00te1",
    36  		},
    37  		{
    38  			"lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135",
    39  			"/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv",
    40  			true,
    41  			"grpcs://lb.etn03r9df42nb631unbv.ydb.mdb.yandexcloud.net:2135/ru-central1/b1g8skpblkos03malf3s/etn03r9df42nb631unbv",
    42  		},
    43  	} {
    44  		t.Run(tt.dsn, func(t *testing.T) {
    45  			s := DSN(tt.endpoint, tt.database, tt.secure)
    46  			if s != tt.dsn {
    47  				t.Fatalf("Unexpected result: %s, exp: %s", s, tt.dsn)
    48  			}
    49  			info, err := dsn.Parse(s)
    50  			if err != nil {
    51  				t.Fatalf("")
    52  			}
    53  			lhs, rhs := config.New(info.Options...), config.New(
    54  				config.WithSecure(tt.secure),
    55  				config.WithEndpoint(tt.endpoint),
    56  				config.WithDatabase(tt.database),
    57  			)
    58  			require.Equal(t, lhs.Endpoint(), rhs.Endpoint())
    59  			require.Equal(t, lhs.Database(), rhs.Database())
    60  			require.Equal(t, lhs.Secure(), rhs.Secure())
    61  		})
    62  	}
    63  }