github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/result_schema_change_test.go (about)

     1  // Copyright 2020 DataStax
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package message
    16  
    17  import (
    18  	"bytes"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  
    24  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    25  )
    26  
    27  func TestSchemaChangeResult_DeepCopy(t *testing.T) {
    28  	msg := &SchemaChangeResult{
    29  		ChangeType: primitive.SchemaChangeTypeCreated,
    30  		Target:     primitive.SchemaChangeTargetAggregate,
    31  		Keyspace:   "ks1",
    32  		Object:     "aggregate",
    33  		Arguments:  []string{"arg1"},
    34  	}
    35  
    36  	cloned := msg.DeepCopy()
    37  	assert.Equal(t, msg, cloned)
    38  
    39  	cloned.ChangeType = primitive.SchemaChangeTypeDropped
    40  	cloned.Target = primitive.SchemaChangeTargetFunction
    41  	cloned.Keyspace = "ks2"
    42  	cloned.Object = "function"
    43  	cloned.Arguments = []string{"arg2"}
    44  
    45  	assert.Equal(t, primitive.SchemaChangeTypeCreated, msg.ChangeType)
    46  	assert.Equal(t, primitive.SchemaChangeTargetAggregate, msg.Target)
    47  	assert.Equal(t, "ks1", msg.Keyspace)
    48  	assert.Equal(t, "aggregate", msg.Object)
    49  	assert.Equal(t, []string{"arg1"}, msg.Arguments)
    50  
    51  	assert.Equal(t, primitive.SchemaChangeTypeDropped, cloned.ChangeType)
    52  	assert.Equal(t, primitive.SchemaChangeTargetFunction, cloned.Target)
    53  	assert.Equal(t, "ks2", cloned.Keyspace)
    54  	assert.Equal(t, "function", cloned.Object)
    55  	assert.Equal(t, []string{"arg2"}, cloned.Arguments)
    56  }
    57  
    58  func TestResultCodec_Encode_SchemaChange(test *testing.T) {
    59  	codec := &resultCodec{}
    60  	// version = 2
    61  	test.Run(primitive.ProtocolVersion2.String(), func(test *testing.T) {
    62  		tests := []encodeTestCase{
    63  			{
    64  				"schema change result keyspace",
    65  				&SchemaChangeResult{
    66  					ChangeType: primitive.SchemaChangeTypeCreated,
    67  					Target:     primitive.SchemaChangeTargetKeyspace,
    68  					Keyspace:   "ks1",
    69  				},
    70  				[]byte{
    71  					0, 0, 0, 5, // result type
    72  					0, 7, C, R, E, A, T, E, D,
    73  					0, 3, k, s, _1,
    74  					0, 0,
    75  				},
    76  				nil,
    77  			},
    78  			{
    79  				"schema change result table",
    80  				&SchemaChangeResult{
    81  					ChangeType: primitive.SchemaChangeTypeCreated,
    82  					Target:     primitive.SchemaChangeTargetTable,
    83  					Keyspace:   "ks1",
    84  					Object:     "table1",
    85  				},
    86  				[]byte{
    87  					0, 0, 0, 5, // result type
    88  					0, 7, C, R, E, A, T, E, D,
    89  					0, 3, k, s, _1,
    90  					0, 6, t, a, b, l, e, _1,
    91  				},
    92  				nil,
    93  			},
    94  			{
    95  				"schema change result type",
    96  				&SchemaChangeResult{
    97  					ChangeType: primitive.SchemaChangeTypeCreated,
    98  					Target:     primitive.SchemaChangeTargetType,
    99  					Keyspace:   "ks1",
   100  					Object:     "udt1",
   101  				},
   102  				[]byte{
   103  					0, 0, 0, 5, // result type
   104  					0, 7, C, R, E, A, T, E, D,
   105  				},
   106  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion2, primitive.SchemaChangeTargetType),
   107  			},
   108  			{
   109  				"schema change result function",
   110  				&SchemaChangeResult{
   111  					ChangeType: primitive.SchemaChangeTypeCreated,
   112  					Target:     primitive.SchemaChangeTargetFunction,
   113  					Keyspace:   "ks1",
   114  					Object:     "func1",
   115  					Arguments:  []string{"int", "varchar"},
   116  				},
   117  				[]byte{
   118  					0, 0, 0, 5, // result type
   119  					0, 7, C, R, E, A, T, E, D,
   120  				},
   121  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion2, primitive.SchemaChangeTargetFunction),
   122  			},
   123  			{
   124  				"schema change result aggregate",
   125  				&SchemaChangeResult{
   126  					ChangeType: primitive.SchemaChangeTypeCreated,
   127  					Target:     primitive.SchemaChangeTargetAggregate,
   128  					Keyspace:   "ks1",
   129  					Object:     "agg1",
   130  					Arguments:  []string{"int", "varchar"},
   131  				},
   132  				[]byte{
   133  					0, 0, 0, 5, // result type
   134  					0, 7, C, R, E, A, T, E, D,
   135  				},
   136  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion2, primitive.SchemaChangeTargetAggregate),
   137  			},
   138  		}
   139  		for _, tt := range tests {
   140  			test.Run(tt.name, func(t *testing.T) {
   141  				dest := &bytes.Buffer{}
   142  				err := codec.Encode(tt.input, dest, primitive.ProtocolVersion2)
   143  				assert.Equal(t, tt.expected, dest.Bytes())
   144  				assert.Equal(t, tt.err, err)
   145  			})
   146  		}
   147  	})
   148  	// version = 3
   149  	test.Run(primitive.ProtocolVersion3.String(), func(test *testing.T) {
   150  		tests := []encodeTestCase{
   151  			{
   152  				"schema change result keyspace",
   153  				&SchemaChangeResult{
   154  					ChangeType: primitive.SchemaChangeTypeCreated,
   155  					Target:     primitive.SchemaChangeTargetKeyspace,
   156  					Keyspace:   "ks1",
   157  				},
   158  				[]byte{
   159  					0, 0, 0, 5, // result type
   160  					0, 7, C, R, E, A, T, E, D,
   161  					0, 8, K, E, Y, S, P, A, C, E,
   162  					0, 3, k, s, _1,
   163  				},
   164  				nil,
   165  			},
   166  			{
   167  				"schema change result table",
   168  				&SchemaChangeResult{
   169  					ChangeType: primitive.SchemaChangeTypeCreated,
   170  					Target:     primitive.SchemaChangeTargetTable,
   171  					Keyspace:   "ks1",
   172  					Object:     "table1",
   173  				},
   174  				[]byte{
   175  					0, 0, 0, 5, // result type
   176  					0, 7, C, R, E, A, T, E, D,
   177  					0, 5, T, A, B, L, E,
   178  					0, 3, k, s, _1,
   179  					0, 6, t, a, b, l, e, _1,
   180  				},
   181  				nil,
   182  			},
   183  			{
   184  				"schema change result type",
   185  				&SchemaChangeResult{
   186  					ChangeType: primitive.SchemaChangeTypeCreated,
   187  					Target:     primitive.SchemaChangeTargetType,
   188  					Keyspace:   "ks1",
   189  					Object:     "udt1",
   190  				},
   191  				[]byte{
   192  					0, 0, 0, 5, // result type
   193  					0, 7, C, R, E, A, T, E, D,
   194  					0, 4, T, Y, P, E,
   195  					0, 3, k, s, _1,
   196  					0, 4, u, d, t, _1,
   197  				},
   198  				nil,
   199  			},
   200  			{
   201  				"schema change result function",
   202  				&SchemaChangeResult{
   203  					ChangeType: primitive.SchemaChangeTypeCreated,
   204  					Target:     primitive.SchemaChangeTargetFunction,
   205  					Keyspace:   "ks1",
   206  					Object:     "func1",
   207  					Arguments:  []string{"int", "varchar"},
   208  				},
   209  				[]byte{
   210  					0, 0, 0, 5, // result type
   211  					0, 7, C, R, E, A, T, E, D,
   212  				},
   213  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion3, primitive.SchemaChangeTargetFunction),
   214  			},
   215  			{
   216  				"schema change result aggregate",
   217  				&SchemaChangeResult{
   218  					ChangeType: primitive.SchemaChangeTypeCreated,
   219  					Target:     primitive.SchemaChangeTargetAggregate,
   220  					Keyspace:   "ks1",
   221  					Object:     "agg1",
   222  					Arguments:  []string{"int", "varchar"},
   223  				},
   224  				[]byte{
   225  					0, 0, 0, 5, // result type
   226  					0, 7, C, R, E, A, T, E, D,
   227  				},
   228  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion3, primitive.SchemaChangeTargetAggregate),
   229  			},
   230  		}
   231  		for _, tt := range tests {
   232  			test.Run(tt.name, func(t *testing.T) {
   233  				dest := &bytes.Buffer{}
   234  				err := codec.Encode(tt.input, dest, primitive.ProtocolVersion3)
   235  				assert.Equal(t, tt.expected, dest.Bytes())
   236  				assert.Equal(t, tt.err, err)
   237  			})
   238  		}
   239  	})
   240  	// versions >= 4
   241  	for _, version := range primitive.SupportedProtocolVersionsGreaterThanOrEqualTo(primitive.ProtocolVersion4) {
   242  		test.Run(version.String(), func(test *testing.T) {
   243  			tests := []encodeTestCase{
   244  				{
   245  					"schema change result keyspace",
   246  					&SchemaChangeResult{
   247  						ChangeType: primitive.SchemaChangeTypeCreated,
   248  						Target:     primitive.SchemaChangeTargetKeyspace,
   249  						Keyspace:   "ks1",
   250  					},
   251  					[]byte{
   252  						0, 0, 0, 5, // result type
   253  						0, 7, C, R, E, A, T, E, D,
   254  						0, 8, K, E, Y, S, P, A, C, E,
   255  						0, 3, k, s, _1,
   256  					},
   257  					nil,
   258  				},
   259  				{
   260  					"schema change result table",
   261  					&SchemaChangeResult{
   262  						ChangeType: primitive.SchemaChangeTypeCreated,
   263  						Target:     primitive.SchemaChangeTargetTable,
   264  						Keyspace:   "ks1",
   265  						Object:     "table1",
   266  					},
   267  					[]byte{
   268  						0, 0, 0, 5, // result type
   269  						0, 7, C, R, E, A, T, E, D,
   270  						0, 5, T, A, B, L, E,
   271  						0, 3, k, s, _1,
   272  						0, 6, t, a, b, l, e, _1,
   273  					},
   274  					nil,
   275  				},
   276  				{
   277  					"schema change result type",
   278  					&SchemaChangeResult{
   279  						ChangeType: primitive.SchemaChangeTypeCreated,
   280  						Target:     primitive.SchemaChangeTargetType,
   281  						Keyspace:   "ks1",
   282  						Object:     "udt1",
   283  					},
   284  					[]byte{
   285  						0, 0, 0, 5, // result type
   286  						0, 7, C, R, E, A, T, E, D,
   287  						0, 4, T, Y, P, E,
   288  						0, 3, k, s, _1,
   289  						0, 4, u, d, t, _1,
   290  					},
   291  					nil,
   292  				},
   293  				{
   294  					"schema change result function",
   295  					&SchemaChangeResult{
   296  						ChangeType: primitive.SchemaChangeTypeCreated,
   297  						Target:     primitive.SchemaChangeTargetFunction,
   298  						Keyspace:   "ks1",
   299  						Object:     "func1",
   300  						Arguments:  []string{"int", "varchar"},
   301  					},
   302  					[]byte{
   303  						0, 0, 0, 5, // result type
   304  						0, 7, C, R, E, A, T, E, D,
   305  						0, 8, F, U, N, C, T, I, O, N,
   306  						0, 3, k, s, _1,
   307  						0, 5, f, u, n, c, _1,
   308  						0, 2,
   309  						0, 3, i, n, t,
   310  						0, 7, v, a, r, c, h, a, r,
   311  					},
   312  					nil,
   313  				},
   314  				{
   315  					"schema change result aggregate",
   316  					&SchemaChangeResult{
   317  						ChangeType: primitive.SchemaChangeTypeCreated,
   318  						Target:     primitive.SchemaChangeTargetAggregate,
   319  						Keyspace:   "ks1",
   320  						Object:     "agg1",
   321  						Arguments:  []string{"int", "varchar"},
   322  					},
   323  					[]byte{
   324  						0, 0, 0, 5, // result type
   325  						0, 7, C, R, E, A, T, E, D,
   326  						0, 9, A, G, G, R, E, G, A, T, E,
   327  						0, 3, k, s, _1,
   328  						0, 4, a, g, g, _1,
   329  						0, 2,
   330  						0, 3, i, n, t,
   331  						0, 7, v, a, r, c, h, a, r,
   332  					},
   333  					nil,
   334  				},
   335  			}
   336  			for _, tt := range tests {
   337  				test.Run(tt.name, func(t *testing.T) {
   338  					dest := &bytes.Buffer{}
   339  					err := codec.Encode(tt.input, dest, version)
   340  					assert.Equal(t, tt.expected, dest.Bytes())
   341  					assert.Equal(t, tt.err, err)
   342  				})
   343  			}
   344  		})
   345  	}
   346  }
   347  
   348  func TestResultCodec_EncodedLength_SchemaChange(test *testing.T) {
   349  	codec := &resultCodec{}
   350  	// version = 2
   351  	test.Run(primitive.ProtocolVersion2.String(), func(test *testing.T) {
   352  		tests := []encodedLengthTestCase{
   353  			{
   354  				"schema change result keyspace",
   355  				&SchemaChangeResult{
   356  					ChangeType: primitive.SchemaChangeTypeCreated,
   357  					Target:     primitive.SchemaChangeTargetKeyspace,
   358  					Keyspace:   "ks1",
   359  				},
   360  				primitive.LengthOfInt +
   361  					primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   362  					primitive.LengthOfString("ks1") +
   363  					primitive.LengthOfString(""),
   364  				nil,
   365  			},
   366  			{
   367  				"schema change result table",
   368  				&SchemaChangeResult{
   369  					ChangeType: primitive.SchemaChangeTypeCreated,
   370  					Target:     primitive.SchemaChangeTargetTable,
   371  					Keyspace:   "ks1",
   372  					Object:     "table1",
   373  				},
   374  				primitive.LengthOfInt +
   375  					primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   376  					primitive.LengthOfString("ks1") +
   377  					primitive.LengthOfString("table1"),
   378  				nil,
   379  			},
   380  			{
   381  				"schema change result type",
   382  				&SchemaChangeResult{
   383  					ChangeType: primitive.SchemaChangeTypeCreated,
   384  					Target:     primitive.SchemaChangeTargetType,
   385  					Keyspace:   "ks1",
   386  					Object:     "udt1",
   387  				},
   388  				-1,
   389  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion2, primitive.SchemaChangeTargetType),
   390  			},
   391  			{
   392  				"schema change result function",
   393  				&SchemaChangeResult{
   394  					ChangeType: primitive.SchemaChangeTypeCreated,
   395  					Target:     primitive.SchemaChangeTargetFunction,
   396  					Keyspace:   "ks1",
   397  					Object:     "func1",
   398  					Arguments:  []string{"int", "varchar"},
   399  				},
   400  				-1,
   401  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion2, primitive.SchemaChangeTargetFunction),
   402  			},
   403  			{
   404  				"schema change result aggregate",
   405  				&SchemaChangeResult{
   406  					ChangeType: primitive.SchemaChangeTypeCreated,
   407  					Target:     primitive.SchemaChangeTargetAggregate,
   408  					Keyspace:   "ks1",
   409  					Object:     "agg1",
   410  					Arguments:  []string{"int", "varchar"},
   411  				},
   412  				-1,
   413  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion2, primitive.SchemaChangeTargetAggregate),
   414  			},
   415  		}
   416  		for _, tt := range tests {
   417  			test.Run(tt.name, func(t *testing.T) {
   418  				actual, err := codec.EncodedLength(tt.input, primitive.ProtocolVersion2)
   419  				assert.Equal(t, tt.expected, actual)
   420  				assert.Equal(t, tt.err, err)
   421  			})
   422  		}
   423  	})
   424  	// version = 3
   425  	test.Run(primitive.ProtocolVersion3.String(), func(test *testing.T) {
   426  		tests := []encodedLengthTestCase{
   427  			{
   428  				"schema change result keyspace",
   429  				&SchemaChangeResult{
   430  					ChangeType: primitive.SchemaChangeTypeCreated,
   431  					Target:     primitive.SchemaChangeTargetKeyspace,
   432  					Keyspace:   "ks1",
   433  				},
   434  				primitive.LengthOfInt +
   435  					primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   436  					primitive.LengthOfString(string(primitive.SchemaChangeTargetKeyspace)) +
   437  					primitive.LengthOfString("ks1"),
   438  				nil,
   439  			},
   440  			{
   441  				"schema change result table",
   442  				&SchemaChangeResult{
   443  					ChangeType: primitive.SchemaChangeTypeCreated,
   444  					Target:     primitive.SchemaChangeTargetTable,
   445  					Keyspace:   "ks1",
   446  					Object:     "table1",
   447  				},
   448  				primitive.LengthOfInt +
   449  					primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   450  					primitive.LengthOfString(string(primitive.SchemaChangeTargetTable)) +
   451  					primitive.LengthOfString("ks1") +
   452  					primitive.LengthOfString("table1"),
   453  				nil,
   454  			},
   455  			{
   456  				"schema change result type",
   457  				&SchemaChangeResult{
   458  					ChangeType: primitive.SchemaChangeTypeCreated,
   459  					Target:     primitive.SchemaChangeTargetType,
   460  					Keyspace:   "ks1",
   461  					Object:     "udt1",
   462  				},
   463  				primitive.LengthOfInt +
   464  					primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   465  					primitive.LengthOfString(string(primitive.SchemaChangeTargetType)) +
   466  					primitive.LengthOfString("ks1") +
   467  					primitive.LengthOfString("udt1"),
   468  				nil,
   469  			},
   470  			{
   471  				"schema change result function",
   472  				&SchemaChangeResult{
   473  					ChangeType: primitive.SchemaChangeTypeCreated,
   474  					Target:     primitive.SchemaChangeTargetFunction,
   475  					Keyspace:   "ks1",
   476  					Object:     "func1",
   477  					Arguments:  []string{"int", "varchar"},
   478  				},
   479  				-1,
   480  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion3, primitive.SchemaChangeTargetFunction),
   481  			},
   482  			{
   483  				"schema change result aggregate",
   484  				&SchemaChangeResult{
   485  					ChangeType: primitive.SchemaChangeTypeCreated,
   486  					Target:     primitive.SchemaChangeTargetAggregate,
   487  					Keyspace:   "ks1",
   488  					Object:     "agg1",
   489  					Arguments:  []string{"int", "varchar"},
   490  				},
   491  				-1,
   492  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion3, primitive.SchemaChangeTargetAggregate),
   493  			},
   494  		}
   495  		for _, tt := range tests {
   496  			test.Run(tt.name, func(t *testing.T) {
   497  				actual, err := codec.EncodedLength(tt.input, primitive.ProtocolVersion3)
   498  				assert.Equal(t, tt.expected, actual)
   499  				assert.Equal(t, tt.err, err)
   500  			})
   501  		}
   502  	})
   503  	// versions >= 4
   504  	for _, version := range primitive.SupportedProtocolVersionsGreaterThanOrEqualTo(primitive.ProtocolVersion4) {
   505  		test.Run(version.String(), func(test *testing.T) {
   506  			tests := []encodedLengthTestCase{
   507  				{
   508  					"schema change result keyspace",
   509  					&SchemaChangeResult{
   510  						ChangeType: primitive.SchemaChangeTypeCreated,
   511  						Target:     primitive.SchemaChangeTargetKeyspace,
   512  						Keyspace:   "ks1",
   513  					},
   514  					primitive.LengthOfInt +
   515  						primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   516  						primitive.LengthOfString(string(primitive.SchemaChangeTargetKeyspace)) +
   517  						primitive.LengthOfString("ks1"),
   518  					nil,
   519  				},
   520  				{
   521  					"schema change result table",
   522  					&SchemaChangeResult{
   523  						ChangeType: primitive.SchemaChangeTypeCreated,
   524  						Target:     primitive.SchemaChangeTargetTable,
   525  						Keyspace:   "ks1",
   526  						Object:     "table1",
   527  					},
   528  					primitive.LengthOfInt +
   529  						primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   530  						primitive.LengthOfString(string(primitive.SchemaChangeTargetTable)) +
   531  						primitive.LengthOfString("ks1") +
   532  						primitive.LengthOfString("table1"),
   533  					nil,
   534  				},
   535  				{
   536  					"schema change result type",
   537  					&SchemaChangeResult{
   538  						ChangeType: primitive.SchemaChangeTypeCreated,
   539  						Target:     primitive.SchemaChangeTargetType,
   540  						Keyspace:   "ks1",
   541  						Object:     "udt1",
   542  					},
   543  					primitive.LengthOfInt +
   544  						primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   545  						primitive.LengthOfString(string(primitive.SchemaChangeTargetType)) +
   546  						primitive.LengthOfString("ks1") +
   547  						primitive.LengthOfString("udt1"),
   548  					nil,
   549  				},
   550  				{
   551  					"schema change result function",
   552  					&SchemaChangeResult{
   553  						ChangeType: primitive.SchemaChangeTypeCreated,
   554  						Target:     primitive.SchemaChangeTargetFunction,
   555  						Keyspace:   "ks1",
   556  						Object:     "func1",
   557  						Arguments:  []string{"int", "varchar"},
   558  					},
   559  					primitive.LengthOfInt +
   560  						primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   561  						primitive.LengthOfString(string(primitive.SchemaChangeTargetFunction)) +
   562  						primitive.LengthOfString("ks1") +
   563  						primitive.LengthOfString("func1") +
   564  						primitive.LengthOfStringList([]string{"int", "varchar"}),
   565  					nil,
   566  				},
   567  				{
   568  					"schema change result aggregate",
   569  					&SchemaChangeResult{
   570  						ChangeType: primitive.SchemaChangeTypeCreated,
   571  						Target:     primitive.SchemaChangeTargetAggregate,
   572  						Keyspace:   "ks1",
   573  						Object:     "agg1",
   574  						Arguments:  []string{"int", "varchar"},
   575  					},
   576  					primitive.LengthOfInt +
   577  						primitive.LengthOfString(string(primitive.SchemaChangeTypeCreated)) +
   578  						primitive.LengthOfString(string(primitive.SchemaChangeTargetAggregate)) +
   579  						primitive.LengthOfString("ks1") +
   580  						primitive.LengthOfString("agg1") +
   581  						primitive.LengthOfStringList([]string{"int", "varchar"}),
   582  					nil,
   583  				},
   584  			}
   585  			for _, tt := range tests {
   586  				test.Run(tt.name, func(t *testing.T) {
   587  					actual, err := codec.EncodedLength(tt.input, version)
   588  					assert.Equal(t, tt.expected, actual)
   589  					assert.Equal(t, tt.err, err)
   590  				})
   591  			}
   592  		})
   593  	}
   594  }
   595  
   596  func TestResultCodec_Decode_SchemaChange(test *testing.T) {
   597  	codec := &resultCodec{}
   598  	// version = 2
   599  	test.Run(primitive.ProtocolVersion2.String(), func(test *testing.T) {
   600  		tests := []decodeTestCase{
   601  			{
   602  				"schema change result keyspace",
   603  				[]byte{
   604  					0, 0, 0, 5, // result type
   605  					0, 7, C, R, E, A, T, E, D,
   606  					0, 3, k, s, _1,
   607  					0, 0,
   608  				},
   609  				&SchemaChangeResult{
   610  					ChangeType: primitive.SchemaChangeTypeCreated,
   611  					Target:     primitive.SchemaChangeTargetKeyspace,
   612  					Keyspace:   "ks1",
   613  				},
   614  				nil,
   615  			},
   616  			{
   617  				"schema change result table",
   618  				[]byte{
   619  					0, 0, 0, 5, // result type
   620  					0, 7, C, R, E, A, T, E, D,
   621  					0, 3, k, s, _1,
   622  					0, 6, t, a, b, l, e, _1,
   623  				},
   624  				&SchemaChangeResult{
   625  					ChangeType: primitive.SchemaChangeTypeCreated,
   626  					Target:     primitive.SchemaChangeTargetTable,
   627  					Keyspace:   "ks1",
   628  					Object:     "table1",
   629  				},
   630  				nil,
   631  			},
   632  		}
   633  		for _, tt := range tests {
   634  			test.Run(tt.name, func(t *testing.T) {
   635  				source := bytes.NewBuffer(tt.input)
   636  				actual, err := codec.Decode(source, primitive.ProtocolVersion2)
   637  				assert.Equal(t, tt.expected, actual)
   638  				assert.Equal(t, tt.err, err)
   639  			})
   640  		}
   641  	})
   642  	// version = 3
   643  	test.Run(primitive.ProtocolVersion3.String(), func(test *testing.T) {
   644  		tests := []decodeTestCase{
   645  			{
   646  				"schema change result keyspace",
   647  				[]byte{
   648  					0, 0, 0, 5, // result type
   649  					0, 7, C, R, E, A, T, E, D,
   650  					0, 8, K, E, Y, S, P, A, C, E,
   651  					0, 3, k, s, _1,
   652  				},
   653  				&SchemaChangeResult{
   654  					ChangeType: primitive.SchemaChangeTypeCreated,
   655  					Target:     primitive.SchemaChangeTargetKeyspace,
   656  					Keyspace:   "ks1",
   657  				},
   658  				nil,
   659  			},
   660  			{
   661  				"schema change result table",
   662  				[]byte{
   663  					0, 0, 0, 5, // result type
   664  					0, 7, C, R, E, A, T, E, D,
   665  					0, 5, T, A, B, L, E,
   666  					0, 3, k, s, _1,
   667  					0, 6, t, a, b, l, e, _1,
   668  				},
   669  				&SchemaChangeResult{
   670  					ChangeType: primitive.SchemaChangeTypeCreated,
   671  					Target:     primitive.SchemaChangeTargetTable,
   672  					Keyspace:   "ks1",
   673  					Object:     "table1",
   674  				},
   675  				nil,
   676  			},
   677  			{
   678  				"schema change result type",
   679  				[]byte{
   680  					0, 0, 0, 5, // result type
   681  					0, 7, C, R, E, A, T, E, D,
   682  					0, 4, T, Y, P, E,
   683  					0, 3, k, s, _1,
   684  					0, 4, u, d, t, _1,
   685  				},
   686  				&SchemaChangeResult{
   687  					ChangeType: primitive.SchemaChangeTypeCreated,
   688  					Target:     primitive.SchemaChangeTargetType,
   689  					Keyspace:   "ks1",
   690  					Object:     "udt1",
   691  				},
   692  				nil,
   693  			},
   694  			{
   695  				"schema change result function",
   696  				[]byte{
   697  					0, 0, 0, 5, // result type
   698  					0, 7, C, R, E, A, T, E, D,
   699  					0, 8, F, U, N, C, T, I, O, N,
   700  					0, 3, k, s, _1,
   701  				},
   702  				nil,
   703  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion3, primitive.SchemaChangeTargetFunction),
   704  			},
   705  			{
   706  				"schema change result aggregate",
   707  				[]byte{
   708  					0, 0, 0, 5, // result type
   709  					0, 7, C, R, E, A, T, E, D,
   710  					0, 9, A, G, G, R, E, G, A, T, E,
   711  					0, 3, k, s, _1,
   712  				},
   713  				nil,
   714  				fmt.Errorf("invalid schema change target for %v: %v", primitive.ProtocolVersion3, primitive.SchemaChangeTargetAggregate),
   715  			},
   716  		}
   717  		for _, tt := range tests {
   718  			test.Run(tt.name, func(t *testing.T) {
   719  				source := bytes.NewBuffer(tt.input)
   720  				actual, err := codec.Decode(source, primitive.ProtocolVersion3)
   721  				assert.Equal(t, tt.expected, actual)
   722  				assert.Equal(t, tt.err, err)
   723  			})
   724  		}
   725  	})
   726  	// versions >= 4
   727  	for _, version := range primitive.SupportedProtocolVersionsGreaterThanOrEqualTo(primitive.ProtocolVersion4) {
   728  		test.Run(version.String(), func(test *testing.T) {
   729  			tests := []decodeTestCase{
   730  				{
   731  					"schema change result keyspace",
   732  					[]byte{
   733  						0, 0, 0, 5, // result type
   734  						0, 7, C, R, E, A, T, E, D,
   735  						0, 8, K, E, Y, S, P, A, C, E,
   736  						0, 3, k, s, _1,
   737  					},
   738  					&SchemaChangeResult{
   739  						ChangeType: primitive.SchemaChangeTypeCreated,
   740  						Target:     primitive.SchemaChangeTargetKeyspace,
   741  						Keyspace:   "ks1",
   742  					},
   743  					nil,
   744  				},
   745  				{
   746  					"schema change result table",
   747  					[]byte{
   748  						0, 0, 0, 5, // result type
   749  						0, 7, C, R, E, A, T, E, D,
   750  						0, 5, T, A, B, L, E,
   751  						0, 3, k, s, _1,
   752  						0, 6, t, a, b, l, e, _1,
   753  					},
   754  					&SchemaChangeResult{
   755  						ChangeType: primitive.SchemaChangeTypeCreated,
   756  						Target:     primitive.SchemaChangeTargetTable,
   757  						Keyspace:   "ks1",
   758  						Object:     "table1",
   759  					},
   760  					nil,
   761  				},
   762  				{
   763  					"schema change result type",
   764  					[]byte{
   765  						0, 0, 0, 5, // result type
   766  						0, 7, C, R, E, A, T, E, D,
   767  						0, 4, T, Y, P, E,
   768  						0, 3, k, s, _1,
   769  						0, 4, u, d, t, _1,
   770  					},
   771  					&SchemaChangeResult{
   772  						ChangeType: primitive.SchemaChangeTypeCreated,
   773  						Target:     primitive.SchemaChangeTargetType,
   774  						Keyspace:   "ks1",
   775  						Object:     "udt1",
   776  					},
   777  					nil,
   778  				},
   779  				{
   780  					"schema change result function",
   781  					[]byte{
   782  						0, 0, 0, 5, // result type
   783  						0, 7, C, R, E, A, T, E, D,
   784  						0, 8, F, U, N, C, T, I, O, N,
   785  						0, 3, k, s, _1,
   786  						0, 5, f, u, n, c, _1,
   787  						0, 2,
   788  						0, 3, i, n, t,
   789  						0, 7, v, a, r, c, h, a, r,
   790  					},
   791  					&SchemaChangeResult{
   792  						ChangeType: primitive.SchemaChangeTypeCreated,
   793  						Target:     primitive.SchemaChangeTargetFunction,
   794  						Keyspace:   "ks1",
   795  						Object:     "func1",
   796  						Arguments:  []string{"int", "varchar"},
   797  					},
   798  					nil,
   799  				},
   800  				{
   801  					"schema change result aggregate",
   802  					[]byte{
   803  						0, 0, 0, 5, // result type
   804  						0, 7, C, R, E, A, T, E, D,
   805  						0, 9, A, G, G, R, E, G, A, T, E,
   806  						0, 3, k, s, _1,
   807  						0, 4, a, g, g, _1,
   808  						0, 2,
   809  						0, 3, i, n, t,
   810  						0, 7, v, a, r, c, h, a, r,
   811  					},
   812  					&SchemaChangeResult{
   813  						ChangeType: primitive.SchemaChangeTypeCreated,
   814  						Target:     primitive.SchemaChangeTargetAggregate,
   815  						Keyspace:   "ks1",
   816  						Object:     "agg1",
   817  						Arguments:  []string{"int", "varchar"},
   818  					},
   819  					nil,
   820  				},
   821  			}
   822  			for _, tt := range tests {
   823  				test.Run(tt.name, func(t *testing.T) {
   824  					source := bytes.NewBuffer(tt.input)
   825  					actual, err := codec.Decode(source, version)
   826  					assert.Equal(t, tt.expected, actual)
   827  					assert.Equal(t, tt.err, err)
   828  				})
   829  			}
   830  		})
   831  	}
   832  }