github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/message/dse_continuous_paging_options_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  	"testing"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  
    23  	"github.com/datastax/go-cassandra-native-protocol/primitive"
    24  )
    25  
    26  func TestContinuousPagingOptions_DeepCopy(t *testing.T) {
    27  	obj := &ContinuousPagingOptions{
    28  		MaxPages:       1,
    29  		PagesPerSecond: 2,
    30  		NextPages:      3,
    31  	}
    32  	cloned := obj.DeepCopy()
    33  	assert.Equal(t, obj, cloned)
    34  	cloned.MaxPages = 5
    35  	cloned.PagesPerSecond = 6
    36  	cloned.NextPages = 7
    37  	assert.NotEqual(t, obj, cloned)
    38  	assert.EqualValues(t, 1, obj.MaxPages)
    39  	assert.EqualValues(t, 2, obj.PagesPerSecond)
    40  	assert.EqualValues(t, 3, obj.NextPages)
    41  	assert.EqualValues(t, 5, cloned.MaxPages)
    42  	assert.EqualValues(t, 6, cloned.PagesPerSecond)
    43  	assert.EqualValues(t, 7, cloned.NextPages)
    44  }
    45  
    46  func TestContinuousPagingOptions_Encode(t *testing.T) {
    47  	version := primitive.ProtocolVersionDse1
    48  	t.Run(version.String(), func(t *testing.T) {
    49  		tests := []struct {
    50  			name     string
    51  			input    *ContinuousPagingOptions
    52  			expected []byte
    53  			err      error
    54  		}{
    55  			{
    56  				"empty options",
    57  				&ContinuousPagingOptions{},
    58  				[]byte{
    59  					0, 0, 0, 0, // max pages
    60  					0, 0, 0, 0, // pages per sec
    61  				},
    62  				nil,
    63  			},
    64  			{
    65  				"simple options",
    66  				&ContinuousPagingOptions{
    67  					MaxPages:       200,
    68  					PagesPerSecond: 150,
    69  				},
    70  				[]byte{
    71  					0, 0, 0, 200, // max pages
    72  					0, 0, 0, 150, // pages per sec
    73  				},
    74  				nil,
    75  			},
    76  		}
    77  		for _, tt := range tests {
    78  			t.Run(tt.name, func(t *testing.T) {
    79  				dest := &bytes.Buffer{}
    80  				err := EncodeContinuousPagingOptions(tt.input, dest, version)
    81  				assert.Equal(t, tt.expected, dest.Bytes())
    82  				assert.Equal(t, tt.err, err)
    83  			})
    84  		}
    85  	})
    86  	version = primitive.ProtocolVersionDse2
    87  	t.Run(version.String(), func(t *testing.T) {
    88  		tests := []struct {
    89  			name     string
    90  			input    *ContinuousPagingOptions
    91  			expected []byte
    92  			err      error
    93  		}{
    94  			{
    95  				"empty options",
    96  				&ContinuousPagingOptions{},
    97  				[]byte{
    98  					0, 0, 0, 0, // max pages
    99  					0, 0, 0, 0, // pages per sec
   100  					0, 0, 0, 0, // next pages
   101  				},
   102  				nil,
   103  			},
   104  			{
   105  				"simple options",
   106  				&ContinuousPagingOptions{
   107  					MaxPages:       200,
   108  					PagesPerSecond: 150,
   109  					NextPages:      100,
   110  				},
   111  				[]byte{
   112  					0, 0, 0, 200, // max pages
   113  					0, 0, 0, 150, // pages per sec
   114  					0, 0, 0, 100, // next pages
   115  				},
   116  				nil,
   117  			},
   118  		}
   119  		for _, tt := range tests {
   120  			t.Run(tt.name, func(t *testing.T) {
   121  				dest := &bytes.Buffer{}
   122  				err := EncodeContinuousPagingOptions(tt.input, dest, version)
   123  				assert.Equal(t, tt.expected, dest.Bytes())
   124  				assert.Equal(t, tt.err, err)
   125  			})
   126  		}
   127  	})
   128  }
   129  
   130  func TestContinuousPagingOptions_EncodedLength(t *testing.T) {
   131  	version := primitive.ProtocolVersionDse1
   132  	t.Run(version.String(), func(t *testing.T) {
   133  		tests := []struct {
   134  			name     string
   135  			input    *ContinuousPagingOptions
   136  			expected int
   137  			err      error
   138  		}{
   139  			{
   140  				"empty options",
   141  				&ContinuousPagingOptions{},
   142  				primitive.LengthOfInt * 2,
   143  				nil,
   144  			},
   145  			{
   146  				"simple options",
   147  				&ContinuousPagingOptions{
   148  					MaxPages:       200,
   149  					PagesPerSecond: 150,
   150  				},
   151  				primitive.LengthOfInt * 2,
   152  				nil,
   153  			},
   154  		}
   155  		for _, tt := range tests {
   156  			t.Run(tt.name, func(t *testing.T) {
   157  				actual, err := LengthOfContinuousPagingOptions(tt.input, version)
   158  				assert.Equal(t, tt.expected, actual)
   159  				assert.Equal(t, tt.err, err)
   160  			})
   161  		}
   162  	})
   163  	version = primitive.ProtocolVersionDse2
   164  	t.Run(version.String(), func(t *testing.T) {
   165  		tests := []struct {
   166  			name     string
   167  			input    *ContinuousPagingOptions
   168  			expected int
   169  			err      error
   170  		}{
   171  			{
   172  				"empty options",
   173  				&ContinuousPagingOptions{},
   174  				primitive.LengthOfInt * 3,
   175  				nil,
   176  			},
   177  			{
   178  				"simple options",
   179  				&ContinuousPagingOptions{
   180  					MaxPages:       200,
   181  					PagesPerSecond: 150,
   182  					NextPages:      100,
   183  				},
   184  				primitive.LengthOfInt * 3,
   185  				nil,
   186  			},
   187  		}
   188  		for _, tt := range tests {
   189  			t.Run(tt.name, func(t *testing.T) {
   190  				actual, err := LengthOfContinuousPagingOptions(tt.input, version)
   191  				assert.Equal(t, tt.expected, actual)
   192  				assert.Equal(t, tt.err, err)
   193  			})
   194  		}
   195  	})
   196  }
   197  
   198  func TestContinuousPagingOptions_Decode(t *testing.T) {
   199  	version := primitive.ProtocolVersionDse1
   200  	t.Run(version.String(), func(t *testing.T) {
   201  		tests := []struct {
   202  			name     string
   203  			input    []byte
   204  			expected *ContinuousPagingOptions
   205  			err      error
   206  		}{
   207  			{
   208  				"empty options",
   209  				[]byte{
   210  					0, 0, 0, 0, // max pages
   211  					0, 0, 0, 0, // pages per sec
   212  				},
   213  				&ContinuousPagingOptions{},
   214  				nil,
   215  			},
   216  			{
   217  				"simple options",
   218  				[]byte{
   219  					0, 0, 0, 200, // max pages
   220  					0, 0, 0, 150, // pages per sec
   221  				},
   222  				&ContinuousPagingOptions{
   223  					MaxPages:       200,
   224  					PagesPerSecond: 150,
   225  				},
   226  				nil,
   227  			},
   228  		}
   229  		for _, tt := range tests {
   230  			t.Run(tt.name, func(t *testing.T) {
   231  				source := bytes.NewBuffer(tt.input)
   232  				actual, err := DecodeContinuousPagingOptions(source, version)
   233  				assert.Equal(t, tt.expected, actual)
   234  				assert.Equal(t, tt.err, err)
   235  			})
   236  		}
   237  	})
   238  	version = primitive.ProtocolVersionDse2
   239  	t.Run(version.String(), func(t *testing.T) {
   240  		tests := []struct {
   241  			name     string
   242  			input    []byte
   243  			expected *ContinuousPagingOptions
   244  			err      error
   245  		}{
   246  			{
   247  				"empty options",
   248  				[]byte{
   249  					0, 0, 0, 0, // max pages
   250  					0, 0, 0, 0, // pages per sec
   251  					0, 0, 0, 0, // next pages
   252  				},
   253  				&ContinuousPagingOptions{},
   254  				nil,
   255  			},
   256  			{
   257  				"simple options",
   258  				[]byte{
   259  					0, 0, 0, 200, // max pages
   260  					0, 0, 0, 150, // pages per sec
   261  					0, 0, 0, 100, // next pages
   262  				},
   263  				&ContinuousPagingOptions{
   264  					MaxPages:       200,
   265  					PagesPerSecond: 150,
   266  					NextPages:      100,
   267  				},
   268  				nil,
   269  			},
   270  		}
   271  		for _, tt := range tests {
   272  			t.Run(tt.name, func(t *testing.T) {
   273  				source := bytes.NewBuffer(tt.input)
   274  				actual, err := DecodeContinuousPagingOptions(source, version)
   275  				assert.Equal(t, tt.expected, actual)
   276  				assert.Equal(t, tt.err, err)
   277  			})
   278  		}
   279  	})
   280  }