Skip to content

Commit

Permalink
fix uuid, timeuuid tests
Browse files Browse the repository at this point in the history
  • Loading branch information
illia-li committed Nov 14, 2024
1 parent e3ca4c9 commit efccc5f
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 71 deletions.
41 changes: 1 addition & 40 deletions marshal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,6 @@ var marshalTests = []struct {
MarshalError error
UnmarshalError error
}{
{
NativeType{proto: 2, typ: TypeTimeUUID},
[]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
func() UUID {
x, _ := UUIDFromBytes([]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0})
return x
}(),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimeUUID},
[]byte{0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
[]byte{0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
marshalErrorf("can not marshal []byte 6 bytes long into timeuuid, must be exactly 16 bytes long"),
unmarshalErrorf("unable to parse UUID: UUIDs must be exactly 16 bytes long"),
},
{
NativeType{proto: 2, typ: TypeTimeUUID},
[]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
[16]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeBoolean},
[]byte("\x00"),
Expand Down Expand Up @@ -404,20 +380,6 @@ var marshalTests = []struct {
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimeUUID},
[]byte{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
&UUID{0x3d, 0xcd, 0x98, 0x0, 0xf3, 0xd9, 0x11, 0xbf, 0x86, 0xd4, 0xb8, 0xe8, 0x56, 0x2c, 0xc, 0xd0},
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimeUUID},
[]byte(nil),
(*UUID)(nil),
nil,
nil,
},
{
NativeType{proto: 2, typ: TypeTimestamp},
[]byte("\x00\x00\x01\x40\x77\x16\xe1\xb8"),
Expand Down Expand Up @@ -1882,11 +1844,10 @@ func BenchmarkUnmarshalUUID(b *testing.B) {
b.ReportAllocs()
src := make([]byte, 16)
dst := UUID{}
var ti TypeInfo = NativeType{}

b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := unmarshalUUID(ti, src, &dst); err != nil {
if err := unmarshalUUID(src, &dst); err != nil {
b.Fatal(err)
}
}
Expand Down
45 changes: 39 additions & 6 deletions tests/serialization/marshal_13_uuids_corrupt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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/timeuuid"
"github.com/gocql/gocql/serialization/uuid"
)

func TestMarshalUUIDsMustFail(t *testing.T) {
Expand All @@ -14,13 +16,44 @@ func TestMarshalUUIDsMustFail(t *testing.T) {
gocql.NewNativeType(4, gocql.TypeTimeUUID, ""),
}

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)
}
type testSuite struct {
name string
marshal func(interface{}) ([]byte, error)
unmarshal func(bytes []byte, i interface{}) error
}

testSuites := [4]testSuite{
{
name: "serialization.uuid",
marshal: uuid.Marshal,
unmarshal: uuid.Unmarshal,
},
{
name: "glob.uuid",
marshal: func(i interface{}) ([]byte, error) {
return gocql.Marshal(tTypes[0], i)
},
unmarshal: func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(tTypes[0], bytes, i)
},
},
{
name: "serialization.timeuuid",
marshal: timeuuid.Marshal,
unmarshal: timeuuid.Unmarshal,
},
{
name: "glob.timeuuid",
marshal: func(i interface{}) ([]byte, error) { return gocql.Marshal(tTypes[1], i) },
unmarshal: func(bytes []byte, i interface{}) error { return gocql.Unmarshal(tTypes[1], 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.NegativeMarshalSet{
Values: mod.Values{
"b6b77c23-c776-40ff-828d-a385f3e8a2aff",
Expand Down
69 changes: 44 additions & 25 deletions tests/serialization/marshal_13_uuids_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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/timeuuid"
"github.com/gocql/gocql/serialization/uuid"
)

func TestMarshalUUIDs(t *testing.T) {
Expand All @@ -14,62 +16,81 @@ func TestMarshalUUIDs(t *testing.T) {
gocql.NewNativeType(4, gocql.TypeTimeUUID, ""),
}

// marshal and unmarshal `custom string` and `custom []byte` unsupported in `nil` data case
brokenCustomTypes := serialization.GetTypes(mod.Bytes{}, mod.String(""), mod.Bytes16{})

// marshal and unmarshal `custom types` unsupported in not `nil` data cases
brokenCustomTypesFull := serialization.GetTypes(mod.Values{mod.Bytes{}, mod.String(""), mod.Bytes16{}}.AddVariants(mod.Reference)...)
type testSuite struct {
name string
marshal func(interface{}) ([]byte, error)
unmarshal func(bytes []byte, i interface{}) error
}

// marshal (string)("") and ([]byte)(nil) unsupported
brokenNullableTypes := serialization.GetTypes("", []byte{})
testSuites := [4]testSuite{
{
name: "serialization.uuid",
marshal: uuid.Marshal,
unmarshal: uuid.Unmarshal,
},
{
name: "glob.uuid",
marshal: func(i interface{}) ([]byte, error) {
return gocql.Marshal(tTypes[0], i)
},
unmarshal: func(bytes []byte, i interface{}) error {
return gocql.Unmarshal(tTypes[0], bytes, i)
},
},
{
name: "serialization.timeuuid",
marshal: timeuuid.Marshal,
unmarshal: timeuuid.Unmarshal,
},
{
name: "glob.timeuuid",
marshal: func(i interface{}) ([]byte, error) { return gocql.Marshal(tTypes[1], i) },
unmarshal: func(bytes []byte, i interface{}) error { return gocql.Unmarshal(tTypes[1], bytes, i) },
},
}

// unmarshal `zero` data into ([]byte)(nil), (*[]byte)(*[nil])
brokenZeroSlices := serialization.GetTypes(make([]byte, 0), (*[]byte)(nil))
for _, tSuite := range testSuites {
marshal := tSuite.marshal
unmarshal := tSuite.unmarshal

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)
}
t.Run(tSuite.name, func(t *testing.T) {

t.Run(tType.String(), func(t *testing.T) {
serialization.PositiveSet{
Data: nil,
Values: mod.Values{
([]byte)(nil), (*[]byte)(nil),
"", (*string)(nil),
(*[16]byte)(nil),
(*gocql.UUID)(nil),
}.AddVariants(mod.CustomType),
BrokenUnmarshalTypes: brokenCustomTypes,
BrokenMarshalTypes: append(brokenNullableTypes, brokenCustomTypes...),
}.Run("[nil]nullable", t, marshal, unmarshal)

serialization.PositiveSet{
Data: nil,
Values: mod.Values{
[16]byte{},
gocql.UUID{},
}.AddVariants(mod.CustomType),
BrokenUnmarshalTypes: brokenCustomTypes,
}.Run("[nil]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: make([]byte, 0),
Values: mod.Values{
make([]byte, 0), [16]byte{}, "", gocql.UUID{},
"00000000-0000-0000-0000-000000000000",
make([]byte, 0),
[16]byte{},
gocql.UUID{},
}.AddVariants(mod.All...),
BrokenUnmarshalTypes: append(brokenCustomTypesFull, brokenZeroSlices...),
}.Run("[]unmarshal", t, nil, unmarshal)

serialization.PositiveSet{
Data: []byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"),
Data: []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
Values: mod.Values{
"00000000-0000-0000-0000-000000000000",
[]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
[16]byte{},
gocql.UUID{},
}.AddVariants(mod.All...),
BrokenMarshalTypes: brokenCustomTypesFull,
BrokenUnmarshalTypes: brokenCustomTypesFull,
}.Run("zeros", t, marshal, unmarshal)

serialization.PositiveSet{
Expand All @@ -80,8 +101,6 @@ func TestMarshalUUIDs(t *testing.T) {
[16]byte{182, 183, 124, 35, 199, 118, 64, 255, 130, 141, 163, 133, 243, 232, 162, 175},
gocql.UUID{182, 183, 124, 35, 199, 118, 64, 255, 130, 141, 163, 133, 243, 232, 162, 175},
}.AddVariants(mod.All...),
BrokenMarshalTypes: brokenCustomTypesFull,
BrokenUnmarshalTypes: brokenCustomTypesFull,
}.Run("uuid", t, marshal, unmarshal)
})
}
Expand Down

0 comments on commit efccc5f

Please sign in to comment.