Skip to content

Commit

Permalink
fix varchar, text, blob tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Nov 7, 2024
1 parent d400fc7 commit 77fdebd
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 117 deletions.
104 changes: 2 additions & 102 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,55 +31,6 @@ var marshalTests = []struct {
MarshalError error
UnmarshalError error
}{
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte("hello world"),
[]byte("hello world"),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte("hello world"),
"hello world",
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte(nil),
[]byte(nil),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte("hello world"),
MyString("hello world"),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte("HELLO WORLD"),
CustomString("hello world"),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBlob},
[]byte("hello\x00"),
[]byte("hello\x00"),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBlob},
[]byte(nil),
[]byte(nil),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimeUUID},
[]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
Expand Down Expand Up @@ -314,7 +265,7 @@ var marshalTests = []struct {
},
[]byte("\x00\x01\x00\x03foo\x00\x05\x01\x02\x03\x04\x05"),
map[string]interface{}{
"foo": []byte{0x01, 0x02, 0x03, 0x04, 0x05},
"foo": string([]byte{0x01, 0x02, 0x03, 0x04, 0x05}),
},
nil,
nil,
Expand Down Expand Up @@ -453,23 +404,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte("nullable string"),
func() *string {
value := "nullable string"
return &value
}(),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte(nil),
(*string)(nil),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimeUUID},
[]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
Expand Down Expand Up @@ -606,23 +540,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte("HELLO WORLD"),
func() *CustomString {
customString := CustomString("hello world")
return &customString
}(),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte(nil),
(*CustomString)(nil),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTinyInt},
[]byte("\x7f"),
Expand Down Expand Up @@ -721,23 +638,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBlob},
[]byte(nil),
([]byte)(nil),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeVarchar},
[]byte{},
func() interface{} {
var s string
return &s
}(),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTime},
encBigInt(1000),
Expand Down Expand Up @@ -1842,7 +1742,7 @@ func BenchmarkUnmarshalVarchar(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := unmarshalVarchar(NativeType{}, src, &dst); err != nil {
if err := unmarshalVarchar(src, &dst); err != nil {
b.Fatal(err)
}
}
Expand Down
72 changes: 57 additions & 15 deletions tests/serialization/marshal_11_texts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,68 @@ import (
"github.com/gocql/gocql"
"github.com/gocql/gocql/internal/tests/serialization"
"github.com/gocql/gocql/internal/tests/serialization/mod"
"github.com/gocql/gocql/serialization/blob"
"github.com/gocql/gocql/serialization/text"
"github.com/gocql/gocql/serialization/varchar"
)

func TestMarshalTexts(t *testing.T) {
tTypes := []gocql.NativeType{
gocql.NewNativeType(4, gocql.TypeVarchar, ""),
gocql.NewNativeType(4, gocql.TypeText, ""),
gocql.NewNativeType(4, gocql.TypeBlob, ""),
type testSuite struct {
name string
marshal func(interface{}) ([]byte, error)
unmarshal func(bytes []byte, i interface{}) error
}

// unmarshall `zero` data into ([]byte)(nil), (*[]byte)(*[nil])
brokenZeroSlices := serialization.GetTypes(make([]byte, 0), (*[]byte)(nil))
testSuites := []testSuite{
{
name: "serialization.varchar",
marshal: varchar.Marshal,
unmarshal: varchar.Unmarshal,
},
{
name: "serialization.text",
marshal: text.Marshal,
unmarshal: text.Unmarshal,
},
{
name: "serialization.blob",
marshal: blob.Marshal,
unmarshal: blob.Unmarshal,
},
{
name: "glob.varchar",
marshal: func(i interface{}) ([]byte, error) {
return gocql.Marshal(gocql.NewNativeType(4, gocql.TypeVarchar, ""), i)
},
unmarshal: func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(gocql.NewNativeType(4, gocql.TypeVarchar, ""), bytes, i)
},
},
{
name: "glob.text",
marshal: func(i interface{}) ([]byte, error) {
return gocql.Marshal(gocql.NewNativeType(4, gocql.TypeText, ""), i)
},
unmarshal: func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(gocql.NewNativeType(4, gocql.TypeText, ""), bytes, i)
},
},
{
name: "glob.blob",
marshal: func(i interface{}) ([]byte, error) {
return gocql.Marshal(gocql.NewNativeType(4, gocql.TypeBlob, ""), i)
},
unmarshal: func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(gocql.NewNativeType(4, gocql.TypeBlob, ""), bytes, i)
},
},
}

for _, tType := range tTypes {
marshal := func(i interface{}) ([]byte, error) { return gocql.Marshal(tType, i) }
unmarshal := func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(tType, bytes, i)
}
for _, tSuite := range testSuites {
marshal := tSuite.marshal
unmarshal := tSuite.unmarshal

t.Run(tType.String(), func(t *testing.T) {
t.Run(tSuite.name, func(t *testing.T) {
serialization.PositiveSet{
Data: nil,
Values: mod.Values{
Expand All @@ -40,9 +83,8 @@ func TestMarshalTexts(t *testing.T) {
}.Run("[nil]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: make([]byte, 0),
Values: mod.Values{make([]byte, 0), ""}.AddVariants(mod.All...),
BrokenUnmarshalTypes: brokenZeroSlices,
Data: make([]byte, 0),
Values: mod.Values{make([]byte, 0), ""}.AddVariants(mod.All...),
}.Run("[]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Expand Down

0 comments on commit 77fdebd

Please sign in to comment.