github.com/datastax/go-cassandra-native-protocol@v0.0.0-20220706104457-5e8aad05cf90/primitive/constants_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 primitive
    16  
    17  import "testing"
    18  
    19  func TestProtocolVersion_String(t *testing.T) {
    20  	tests := []struct {
    21  		name string
    22  		v    ProtocolVersion
    23  		want string
    24  	}{
    25  		{"v2", ProtocolVersion2, "ProtocolVersion OSS 2"},
    26  		{"v3", ProtocolVersion3, "ProtocolVersion OSS 3"},
    27  		{"v4", ProtocolVersion4, "ProtocolVersion OSS 4"},
    28  		{"v5", ProtocolVersion5, "ProtocolVersion OSS 5"},
    29  		{"DSE v1", ProtocolVersionDse1, "ProtocolVersion DSE 1"},
    30  		{"DSE v2", ProtocolVersionDse2, "ProtocolVersion DSE 2"},
    31  		{"unknown", ProtocolVersion(6), "ProtocolVersion ? [0X06]"},
    32  	}
    33  	for _, tt := range tests {
    34  		t.Run(tt.name, func(t *testing.T) {
    35  			if got := tt.v.String(); got != tt.want {
    36  				t.Errorf("String() = %v, want %v", got, tt.want)
    37  			}
    38  		})
    39  	}
    40  }
    41  
    42  func TestDataTypeCode_IsValid(t *testing.T) {
    43  	tests := []struct {
    44  		name          string
    45  		dtc           DataTypeCode
    46  		shouldBeValid bool
    47  	}{
    48  		{"DataTypeCodeCustom", DataTypeCodeCustom, true},
    49  		{"DataTypeCodeAscii", DataTypeCodeAscii, true},
    50  		{"DataTypeCodeBigint", DataTypeCodeBigint, true},
    51  		{"DataTypeCodeBlob", DataTypeCodeBlob, true},
    52  		{"DataTypeCodeBoolean", DataTypeCodeBoolean, true},
    53  		{"DataTypeCodeCounter", DataTypeCodeCounter, true},
    54  		{"DataTypeCodeDecimal", DataTypeCodeDecimal, true},
    55  		{"DataTypeCodeDouble", DataTypeCodeDouble, true},
    56  		{"DataTypeCodeFloat", DataTypeCodeFloat, true},
    57  		{"DataTypeCodeInt", DataTypeCodeInt, true},
    58  		{"DataTypeCodeText", DataTypeCodeText, true},
    59  		{"DataTypeCodeTimestamp", DataTypeCodeTimestamp, true},
    60  		{"DataTypeCodeUuid", DataTypeCodeUuid, true},
    61  		{"DataTypeCodeVarchar", DataTypeCodeVarchar, true},
    62  		{"DataTypeCodeVarint", DataTypeCodeVarint, true},
    63  		{"DataTypeCodeTimeuuid", DataTypeCodeTimeuuid, true},
    64  		{"DataTypeCodeInet", DataTypeCodeInet, true},
    65  		{"DataTypeCodeDate", DataTypeCodeDate, true},
    66  		{"DataTypeCodeTime", DataTypeCodeTime, true},
    67  		{"DataTypeCodeSmallint", DataTypeCodeSmallint, true},
    68  		{"DataTypeCodeTinyint", DataTypeCodeTinyint, true},
    69  		{"DataTypeCodeDuration", DataTypeCodeDuration, true},
    70  		{"DataTypeCodeList", DataTypeCodeList, true},
    71  		{"DataTypeCodeMap", DataTypeCodeMap, true},
    72  		{"DataTypeCodeSet", DataTypeCodeSet, true},
    73  		{"DataTypeCodeUdt", DataTypeCodeUdt, true},
    74  		{"DataTypeCodeTuple", DataTypeCodeTuple, true},
    75  		{"Nonsense", DataTypeCode(0x0023), false},
    76  	}
    77  
    78  	for _, tt := range tests {
    79  		t.Run(tt.name, func(t *testing.T) {
    80  			if isValid := tt.dtc.IsValid(); isValid != tt.shouldBeValid {
    81  				t.Errorf("IsValid() = %v, shouldBeValid %v", isValid, tt.shouldBeValid)
    82  			}
    83  		})
    84  	}
    85  }