Skip to content

Commit

Permalink
Add tests for pkg/cassandra/gocql/testutils (#5153)
Browse files Browse the repository at this point in the history
**Which problem is this PR solving?**

This PR addresses a part of the issue
[#5068](#5068) by adding
tests for the `pkg/cassandra/gocql/testutils` package.

**Description of the changes**

The commit introduces tests for the `pkg/cassandra/gocql/testutils`
package to ensure the correctness of the User Defined Type (UDT)
functionality. These tests cover the `MarshalUDT` and `UnmarshalUDT`
methods of the `CustomUDT` type.

**How was this change tested?**

The changes were tested by running the following command:

```bash
make test
```

**Checklist**

- [x] I have read
[CONTRIBUTING_GUIDELINES.md](https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md)
- [x] I have signed all commits
- [x] I have added unit tests for the new functionality
- [x] I have run lint and test steps successfully
  - `for jaeger: make lint test`
  - `for jaeger-ui: yarn lint` and `yarn test`

---------

Signed-off-by: Wise-Wizard <saransh.shankar@gmail.com>
  • Loading branch information
Wise-Wizard authored Jan 30, 2024
1 parent ecf1dc0 commit e08f576
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 1 deletion.
1 change: 0 additions & 1 deletion pkg/cassandra/gocql/testutils/.nocover

This file was deleted.

81 changes: 81 additions & 0 deletions pkg/cassandra/gocql/testutils/udt_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright (c) 2024 The Jaeger Authors.
// SPDX-License-Identifier: Apache-2.0

package testutils_test

import (
"testing"

"github.com/gocql/gocql"

"github.com/jaegertracing/jaeger/pkg/cassandra/gocql/testutils"
)

// CustomUDT is a custom type that implements gocql.UDTMarshaler and gocql.UDTUnmarshaler interfaces.
type CustomUDT struct {
Field1 int
Field2 string
}

// MarshalUDT implements the gocql.UDTMarshaler interface.
func (c *CustomUDT) MarshalUDT(name string, info gocql.TypeInfo) ([]byte, error) {
switch name {
case "Field1":
return gocql.Marshal(info, c.Field1)
case "Field2":
return gocql.Marshal(info, c.Field2)
default:
return nil, gocql.ErrNotFound
}
}

// UnmarshalUDT implements the gocql.UDTUnmarshaler interface.
func (c *CustomUDT) UnmarshalUDT(name string, info gocql.TypeInfo, data []byte) error {
switch name {
case "Field1":
return gocql.Unmarshal(info, data, &c.Field1)
case "Field2":
return gocql.Unmarshal(info, data, &c.Field2)
default:
return gocql.ErrNotFound
}
}

func TestUDTTestCase(t *testing.T) {
udtInstance := &CustomUDT{
Field1: 1,
Field2: "test",
}

// Define UDT fields for testing
udtFields := []testutils.UDTField{
{
Name: "Field1",
Type: gocql.TypeBigInt,
ValIn: []byte{0, 0, 0, 0, 0, 0, 0, 1},
Err: false,
},
{
Name: "Field2",
Type: gocql.TypeVarchar,
ValIn: []byte("test"),
Err: false,
},
{
Name: "InvalidField",
Type: gocql.TypeBigInt,
ValIn: []byte("test"),
Err: true,
},
}

// Create a UDTTestCase
testCase := testutils.UDTTestCase{
Obj: udtInstance,
ObjName: "CustomUDT",
New: func() gocql.UDTUnmarshaler { return &CustomUDT{} },
Fields: udtFields,
}

testCase.Run(t)
}

0 comments on commit e08f576

Please sign in to comment.