Skip to content

Commit

Permalink
add cassandra serialization tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Jan 19, 2025
1 parent 4d213a5 commit 45ef9a1
Showing 1 changed file with 248 additions and 0 deletions.
248 changes: 248 additions & 0 deletions cassandra_db_serialization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
//go:build integration && cassandra && serialization
// +build integration,cassandra,serialization

package gocql

import (
"fmt"
"gopkg.in/inf.v0"
"math"
"reflect"
"testing"
)

func TestCassandraSerializationSimpleTypes(t *testing.T) {
pkColumn := "test_id"
testColumn := "test_col"

type valueCase struct {
name string
value interface{}
data []byte
errDB bool
}

type typeCase struct {
table string
tp TypeInfo
valCases []valueCase
}

nilBytes := ([]byte)(nil)

nilRef := func(in interface{}) interface{} {
out := reflect.NewAt(reflect.TypeOf(in), nil).Interface()
return out
}

typeCases := []typeCase{
{
tp: NewNativeType(4, TypeBoolean, ""),
valCases: []valueCase{
{name: "true", value: true, data: []byte{1}, errDB: false},
{name: "false", value: false, data: []byte{0}, errDB: false},
{name: "nil", value: nilRef(false), data: nilBytes, errDB: false},
},
},
{
tp: NewNativeType(4, TypeTinyInt, ""),
valCases: []valueCase{
{name: "max", value: int8(math.MaxInt8), data: []byte("\x7f"), errDB: false},
{name: "min", value: int8(math.MinInt8), data: []byte("\x80"), errDB: false},
{name: "+1", value: int8(1), data: []byte("\x01"), errDB: false},
{name: "-1", value: int8(-1), data: []byte("\xff"), errDB: false},
{name: "zero", value: int8(0), data: []byte("\x00"), errDB: false},
{name: "nil", value: nilRef(int8(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeSmallInt, ""),
valCases: []valueCase{
{name: "max", value: int16(math.MaxInt16), data: []byte("\x7f\xff"), errDB: false},
{name: "min", value: int16(math.MinInt16), data: []byte("\x80\x00"), errDB: false},
{name: "+1", value: int16(1), data: []byte("\x00\x01"), errDB: false},
{name: "-1", value: int16(-1), data: []byte("\xff\xff"), errDB: false},
{name: "zero", value: int16(0), data: []byte("\x00\x00"), errDB: false},
{name: "nil", value: nilRef(int16(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeInt, ""),
valCases: []valueCase{
{name: "max", value: int32(math.MaxInt32), data: []byte("\x7f\xff\xff\xff"), errDB: false},
{name: "min", value: int32(math.MinInt32), data: []byte("\x80\x00\x00\x00"), errDB: false},
{name: "+1", value: int32(1), data: []byte("\x00\x00\x00\x01"), errDB: false},
{name: "-1", value: int32(-1), data: []byte("\xff\xff\xff\xff"), errDB: false},
{name: "zero", value: int32(0), data: []byte("\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(int32(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeBigInt, ""),
valCases: []valueCase{
{name: "max", value: int64(math.MaxInt64), data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "min", value: int64(math.MinInt64), data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "+1", value: int64(1), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x01"), errDB: false},
{name: "-1", value: int64(-1), data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "zero", value: int64(0), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(int64(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeVarint, ""),
valCases: []valueCase{
{name: "max", value: int64(math.MaxInt64), data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "min", value: int64(math.MinInt64), data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "+1", value: int64(1), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x01"), errDB: false},
{name: "-1", value: int64(-1), data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "zero", value: int64(0), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(int64(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeFloat, ""),
valCases: []valueCase{
{name: "max", value: float32(math.MaxFloat32), data: []byte("\x7f\xff\xff\xff"), errDB: false},
{name: "min", value: float32(math.SmallestNonzeroFloat32), data: []byte("\x00\x00\x00\x01"), errDB: false},
{name: "zero", value: float32(0), data: []byte("\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(float32(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeDouble, ""),
valCases: []valueCase{
{name: "max", value: math.MaxFloat64, data: []byte("\x7f\xef\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "min", value: math.SmallestNonzeroFloat64, data: []byte("\x00\x00\x00\x00\x00\x00\x00\x01"), errDB: false},
{name: "zero", value: float64(0), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(float64(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeDecimal, ""),
valCases: []valueCase{
{name: "max", value: *inf.NewDec(math.MaxInt64, math.MaxInt16), data: []byte("\x00\x00\x7f\xff\x7f\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "min", value: *inf.NewDec(math.MinInt64, math.MinInt16), data: []byte("\x00\x00\x7f\xff\x80\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "zero", value: *inf.NewDec(0, 0), data: []byte("\x00\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(inf.Dec{}), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeVarchar, ""),
valCases: []valueCase{
{name: "val", value: "test string", data: []byte("test string"), errDB: false},
{name: "zero", value: "", data: make([]byte, 0), errDB: false},
{name: "nil", value: nilRef(""), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeText, ""),
valCases: []valueCase{
{name: "val", value: "test string", data: []byte("test string"), errDB: false},
{name: "zero", value: "", data: make([]byte, 0), errDB: false},
{name: "nil", value: nilRef(""), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeBlob, ""),
valCases: []valueCase{
{name: "val", value: "test string", data: []byte("test string"), errDB: false},
{name: "zero", value: "", data: make([]byte, 0), errDB: false},
{name: "nil", value: nilRef(""), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeAscii, ""),
valCases: []valueCase{
{name: "val", value: "test string", data: []byte("test string"), errDB: false},
{name: "zero", value: "", data: make([]byte, 0), errDB: false},
{name: "nil", value: nilRef(""), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeUUID, ""),
valCases: []valueCase{
{name: "val", value: "b6b77c23-c776-40ff-828d-a385f3e8a2af", data: []byte("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2\xaf"), errDB: false},
{name: "zero", value: "00000000-0000-0000-0000-000000000000", data: make([]byte, 16), errDB: false},
{name: "nil", value: nilRef(""), data: nilBytes, errDB: false},
{name: "nil_val", value: "", data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeTimeUUID, ""),
valCases: []valueCase{
{name: "val", value: "b6b77c23-c776-40ff-828d-a385f3e8a2af", data: []byte("\xb6\xb7\x7c\x23\xc7\x76\x40\xff\x82\x8d\xa3\x85\xf3\xe8\xa2\xaf"), errDB: false},
{name: "zero", value: "00000000-0000-0000-0000-000000000000", data: make([]byte, 16), errDB: false},
{name: "nil", value: nilRef(""), data: nilBytes, errDB: false},
{name: "nil_val", value: "", data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeInet, ""),
valCases: []valueCase{
{name: "val_v6", value: "fe80:cd00:0:cde:1257:0:211e:729c", data: []byte("\xfe\x80\xcd\x00\x00\x00\x0c\xde\x12\x57\x00\x00\x21\x1e\x72\x9c"), errDB: false},
{name: "val_v4", value: "192.168.0.1", data: []byte{192, 168, 0, 1}, errDB: false},
{name: "zero_v6", value: "::", data: make([]byte, 16), errDB: false},
{name: "zero_v4", value: "0.0.0.0", data: make([]byte, 4), errDB: false},
{name: "nil", value: nilRef(""), data: nilBytes, errDB: false},
{name: "nil_val", value: "", data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeTime, ""),
valCases: []valueCase{
{name: "max", value: int64(math.MaxInt64), data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "min", value: int64(math.MinInt64), data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "+1", value: int64(1), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x01"), errDB: false},
{name: "-1", value: int64(-1), data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "zero", value: int64(0), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(int64(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeTimestamp, ""),
valCases: []valueCase{
{name: "max", value: int64(math.MaxInt64), data: []byte("\x7f\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "min", value: int64(math.MinInt64), data: []byte("\x80\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "+1", value: int64(1), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x01"), errDB: false},
{name: "-1", value: int64(-1), data: []byte("\xff\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "zero", value: int64(0), data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(int64(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeDate, ""),
valCases: []valueCase{
{name: "max", value: int32(math.MaxInt32), data: []byte("\x7f\xff\xff\xff"), errDB: false},
{name: "min", value: int32(math.MinInt32), data: []byte("\x80\x00\x00\x00"), errDB: false},
{name: "+1", value: int32(1), data: []byte("\x00\x00\x00\x01"), errDB: false},
{name: "-1", value: int32(-1), data: []byte("\xff\xff\xff\xff"), errDB: false},
{name: "zero", value: int32(0), data: []byte("\x00\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(int32(0)), data: nilBytes, errDB: false},
},
},
{tp: NewNativeType(4, TypeDuration, ""),
valCases: []valueCase{
{name: "max", value: Duration{Days: math.MaxInt32, Months: math.MaxInt32, Nanoseconds: math.MaxInt64}, data: []byte("\xf0\xff\xff\xff\xfe\xf0\xff\xff\xff\xfe\xff\xff\xff\xff\xff\xff\xff\xff\xfe"), errDB: false},
{name: "min", value: Duration{Days: math.MinInt32, Months: math.MinInt32, Nanoseconds: math.MinInt64}, data: []byte("\xf0\xff\xff\xff\xff\xf0\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"), errDB: false},
{name: "+1", value: Duration{Days: 1, Months: 1, Nanoseconds: 1}, data: []byte("\x02\x02\x02"), errDB: false},
{name: "-1", value: Duration{Days: -1, Months: -1, Nanoseconds: -1}, data: []byte("\x01\x01\x01"), errDB: false},
{name: "zero", value: Duration{}, data: []byte("\x00\x00\x00"), errDB: false},
{name: "nil", value: nilRef(Duration{}), data: nilBytes, errDB: false},
},
},
}

keySpaseOpt := func(c *ClusterConfig) {
c.Keyspace = "marshal_test"
}

session := createSession(t, keySpaseOpt)
defer session.Close()

//Create are tables
for i, tc := range typeCases {
cqlType := tc.tp.Type().String()
table := "gocql_test." + cqlType

stmt := fmt.Sprintf(`CREATE TABLE %s (%s text, %s %s, PRIMARY KEY (test_id))`, table, pkColumn, testColumn, cqlType)
if err := createTable(session, stmt); err != nil {
t.Fatalf("failed to create table for cqltype %s with error '%v'", cqlType, err)
}
typeCases[i].table = table
}

//Insert are values
for _, tc := range typeCases {
for _, valCase := range tc.valCases {
err := session.Query("INSERT INTO %s (%s, %s) VALUES(?, ?)", tc.table, pkColumn, testColumn, valCase.name, valCase.value).Exec()
if err != nil {
t.Fatalf("failed to insert (%s) value (%T)(%[2]v) for cqltype %s with error '%v'", valCase.name, valCase.value, tc.tp.Type().String(), err)
}
}
}

fmt.Println("The TestCassandraSerializationSimpleTypes is done")
}

0 comments on commit 45ef9a1

Please sign in to comment.