github.com/ydb-platform/ydb-go-sdk/v3@v3.89.2/table/options/options_test.go (about)

     1  package options
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb"
     8  	"github.com/ydb-platform/ydb-go-genproto/protos/Ydb_Table"
     9  
    10  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/allocator"
    11  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/feature"
    12  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/types"
    13  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/value"
    14  )
    15  
    16  var abc = "abc"
    17  
    18  func TestSessionOptionsProfile(t *testing.T) {
    19  	a := allocator.New()
    20  	defer a.Free()
    21  	{
    22  		opt := WithProfile(
    23  			WithProfilePreset(abc),
    24  		)
    25  		req := Ydb_Table.CreateTableRequest{}
    26  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
    27  		if req.GetProfile().GetPresetName() != abc {
    28  			t.Errorf("Preset is not as expected")
    29  		}
    30  	}
    31  	{
    32  		opt := WithProfile(
    33  			WithCompactionPolicy(WithCompactionPolicyPreset(abc)),
    34  		)
    35  		req := Ydb_Table.CreateTableRequest{}
    36  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
    37  		if req.GetProfile().GetCompactionPolicy().GetPresetName() != abc {
    38  			t.Errorf("Compaction policy is not as expected")
    39  		}
    40  	}
    41  	{
    42  		opt := WithProfile(
    43  			WithPartitioningPolicy(
    44  				WithPartitioningPolicyPreset(abc),
    45  				WithPartitioningPolicyMode(PartitioningAutoSplit),
    46  			),
    47  		)
    48  		req := Ydb_Table.CreateTableRequest{}
    49  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
    50  		p := req.GetProfile().GetPartitioningPolicy()
    51  		if p.GetPresetName() != abc || p.GetAutoPartitioning() != Ydb_Table.PartitioningPolicy_AUTO_SPLIT {
    52  			t.Errorf("Partitioning policy is not as expected")
    53  		}
    54  	}
    55  	{
    56  		opt := WithPartitions(
    57  			WithUniformPartitions(3),
    58  		)
    59  		req := Ydb_Table.CreateTableRequest{}
    60  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
    61  		if p, ok := req.GetPartitions().(*Ydb_Table.CreateTableRequest_UniformPartitions); !ok || p.UniformPartitions != 3 {
    62  			t.Errorf("Uniform partitioning policy is not as expected")
    63  		}
    64  	}
    65  	{
    66  		opt := WithPartitions(
    67  			WithExplicitPartitions(
    68  				value.Int64Value(1),
    69  			),
    70  		)
    71  		req := Ydb_Table.CreateTableRequest{}
    72  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
    73  		p, ok := req.GetPartitions().(*Ydb_Table.CreateTableRequest_PartitionAtKeys)
    74  		if !ok {
    75  			t.Errorf("Explicitly partitioning policy is not as expected")
    76  		} else {
    77  			require.Equal(
    78  				t,
    79  				[]*Ydb.TypedValue{value.ToYDB(value.Int64Value(1), a)},
    80  				p.PartitionAtKeys.GetSplitPoints(),
    81  			)
    82  		}
    83  	}
    84  	{
    85  		opt := WithProfile(
    86  			WithExecutionPolicy(WithExecutionPolicyPreset(abc)),
    87  		)
    88  		req := Ydb_Table.CreateTableRequest{}
    89  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
    90  		if req.GetProfile().GetExecutionPolicy().GetPresetName() != abc {
    91  			t.Errorf("Execution policy is not as expected")
    92  		}
    93  	}
    94  	{
    95  		opt := WithProfile(
    96  			WithReplicationPolicy(
    97  				WithReplicationPolicyPreset(abc),
    98  				WithReplicationPolicyReplicasCount(3),
    99  				WithReplicationPolicyCreatePerAZ(FeatureEnabled),
   100  				WithReplicationPolicyAllowPromotion(FeatureDisabled),
   101  			),
   102  		)
   103  		req := Ydb_Table.CreateTableRequest{}
   104  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
   105  		p := req.GetProfile().GetReplicationPolicy()
   106  		if p.GetPresetName() != abc ||
   107  			p.GetReplicasCount() != 3 ||
   108  			p.GetCreatePerAvailabilityZone() != Ydb.FeatureFlag_ENABLED ||
   109  			p.GetAllowPromotion() != Ydb.FeatureFlag_DISABLED {
   110  			t.Errorf("Replication policy is not as expected")
   111  		}
   112  	}
   113  	{
   114  		opt := WithProfile(
   115  			WithCachingPolicy(WithCachingPolicyPreset(abc)),
   116  		)
   117  		req := Ydb_Table.CreateTableRequest{}
   118  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
   119  		if req.GetProfile().GetCachingPolicy().GetPresetName() != abc {
   120  			t.Errorf("Caching policy is not as expected")
   121  		}
   122  	}
   123  }
   124  
   125  func TestStoragePolicyOptions(t *testing.T) {
   126  	a := allocator.New()
   127  	defer a.Free()
   128  	{
   129  		opt := WithProfile(
   130  			WithStoragePolicy(
   131  				WithStoragePolicyPreset(abc),
   132  				WithStoragePolicySyslog("any1"),
   133  				WithStoragePolicyLog("any2"),
   134  				WithStoragePolicyData("any3"),
   135  				WithStoragePolicyExternal("any4"),
   136  				WithStoragePolicyKeepInMemory(FeatureEnabled),
   137  			),
   138  		)
   139  		req := Ydb_Table.CreateTableRequest{}
   140  		opt.ApplyCreateTableOption((*CreateTableDesc)(&req), a)
   141  		p := req.GetProfile().GetStoragePolicy()
   142  		if p.GetPresetName() != abc ||
   143  			p.GetSyslog().GetMedia() != "any1" ||
   144  			p.GetLog().GetMedia() != "any2" ||
   145  			p.GetData().GetMedia() != "any3" ||
   146  			p.GetExternal().GetMedia() != "any4" ||
   147  			p.GetKeepInMemory() != Ydb.FeatureFlag_ENABLED {
   148  			t.Errorf("Storage policy is not as expected")
   149  		}
   150  	}
   151  }
   152  
   153  func TestAlterTableOptions(t *testing.T) {
   154  	a := allocator.New()
   155  	defer a.Free()
   156  	{
   157  		opt := WithAddColumn("a", types.Bool)
   158  		req := Ydb_Table.AlterTableRequest{}
   159  		opt.ApplyAlterTableOption((*AlterTableDesc)(&req), a)
   160  		if len(req.GetAddColumns()) != 1 ||
   161  			req.GetAddColumns()[0].GetName() != "a" {
   162  			t.Errorf("Alter table options is not as expected")
   163  		}
   164  	}
   165  	{
   166  		column := Column{
   167  			Name:   "a",
   168  			Type:   types.Bool,
   169  			Family: "b",
   170  		}
   171  		opt := WithAddColumnMeta(column)
   172  		req := Ydb_Table.AlterTableRequest{}
   173  		opt.ApplyAlterTableOption((*AlterTableDesc)(&req), a)
   174  		if len(req.GetAddColumns()) != 1 ||
   175  			req.GetAddColumns()[0].GetName() != column.Name ||
   176  			req.GetAddColumns()[0].GetType() != types.TypeToYDB(column.Type, a) ||
   177  			req.GetAddColumns()[0].GetFamily() != column.Family {
   178  			t.Errorf("Alter table options is not as expected")
   179  		}
   180  	}
   181  	{
   182  		opt := WithDropColumn("a")
   183  		req := Ydb_Table.AlterTableRequest{}
   184  		opt.ApplyAlterTableOption((*AlterTableDesc)(&req), a)
   185  		if len(req.GetDropColumns()) != 1 ||
   186  			req.GetDropColumns()[0] != "a" {
   187  			t.Errorf("Alter table options is not as expected")
   188  		}
   189  	}
   190  	{
   191  		cf := ColumnFamily{
   192  			Name: "a",
   193  			Data: StoragePool{
   194  				Media: "ssd",
   195  			},
   196  			Compression:  ColumnFamilyCompressionLZ4,
   197  			KeepInMemory: FeatureEnabled,
   198  		}
   199  		opt := WithAlterColumnFamilies(cf)
   200  		req := Ydb_Table.AlterTableRequest{}
   201  		opt.ApplyAlterTableOption((*AlterTableDesc)(&req), a)
   202  		if len(req.GetAddColumnFamilies()) != 1 ||
   203  			req.GetAddColumnFamilies()[0].GetName() != cf.Name ||
   204  			req.GetAddColumnFamilies()[0].GetData().GetMedia() != cf.Data.Media ||
   205  			req.GetAddColumnFamilies()[0].GetCompression() != cf.Compression.toYDB() ||
   206  			req.GetAddColumnFamilies()[0].GetKeepInMemory() != cf.KeepInMemory.ToYDB() {
   207  			t.Errorf("Alter table options is not as expected")
   208  		}
   209  	}
   210  	{
   211  		cf := ColumnFamily{
   212  			Name:        "default",
   213  			Compression: ColumnFamilyCompressionLZ4,
   214  		}
   215  		opt := WithAlterColumnFamilies(cf)
   216  		req := Ydb_Table.AlterTableRequest{}
   217  		opt.ApplyAlterTableOption((*AlterTableDesc)(&req), a)
   218  		if len(req.GetAddColumnFamilies()) != 1 ||
   219  			req.GetAddColumnFamilies()[0].GetName() != cf.Name ||
   220  			req.GetAddColumnFamilies()[0].GetData() != nil ||
   221  			req.GetAddColumnFamilies()[0].GetCompression() != cf.Compression.toYDB() ||
   222  			req.GetAddColumnFamilies()[0].GetKeepInMemory() != Ydb.FeatureFlag_STATUS_UNSPECIFIED {
   223  			t.Errorf("Alter table options is not as expected")
   224  		}
   225  	}
   226  	{
   227  		rr := ReadReplicasSettings{
   228  			Type:  ReadReplicasAnyAzReadReplicas,
   229  			Count: 42,
   230  		}
   231  		opt := WithAlterReadReplicasSettings(rr)
   232  		req := Ydb_Table.AlterTableRequest{}
   233  		opt.ApplyAlterTableOption((*AlterTableDesc)(&req), a)
   234  		rrOut := NewReadReplicasSettings(req.GetSetReadReplicasSettings())
   235  		if rr != rrOut {
   236  			t.Errorf("Alter table set read replicas options is not as expected")
   237  		}
   238  	}
   239  	{
   240  		ss := StorageSettings{
   241  			TableCommitLog0:    StoragePool{Media: "m1"},
   242  			TableCommitLog1:    StoragePool{Media: "m2"},
   243  			External:           StoragePool{Media: "m3"},
   244  			StoreExternalBlobs: feature.Enabled,
   245  		}
   246  		opt := WithAlterStorageSettings(ss)
   247  		req := Ydb_Table.AlterTableRequest{}
   248  		opt.ApplyAlterTableOption((*AlterTableDesc)(&req), a)
   249  		rrOut := NewStorageSettings(req.GetAlterStorageSettings())
   250  		if ss != rrOut {
   251  			t.Errorf("Alter table storage settings options is not as expected")
   252  		}
   253  	}
   254  }