Skip to content

Commit

Permalink
add serialization unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfkeepers committed Nov 7, 2024
1 parent aa96c82 commit 0cb2ac7
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 4 deletions.
11 changes: 8 additions & 3 deletions datanode.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,6 @@ func FromBytes(bs []byte) (*dataNode, error) {
}

node := dataNode{
values: map[string]any{},
// FIXME: do something with the serialized commments.
// I'm punting on this for now because I want to figure
// out the best middle ground between avoiding a slice of
Expand All @@ -663,12 +662,18 @@ func FromBytes(bs []byte) (*dataNode, error) {
// need to introduce a delimiter.
}

if len(core.Values) > 0 {
node.values = map[string]any{}
}

for k, v := range core.Values {
node.values[k] = v
}

node.otel = &otelClient{
serviceName: core.OTELServiceName,
if len(core.OTELServiceName) > 0 {
node.otel = &otelClient{
serviceName: core.OTELServiceName,
}
}

return &node, nil
Expand Down
81 changes: 80 additions & 1 deletion datanode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand All @@ -12,7 +13,6 @@ import (
// ---------------------------------------------------------------------------

func TestDataNode_Init(t *testing.T) {

table := []struct {
name string
node *dataNode
Expand Down Expand Up @@ -42,3 +42,82 @@ func TestDataNode_Init(t *testing.T) {
})
}
}

func TestBytes(t *testing.T) {
table := []struct {
name string
node func() *dataNode
expectSerialized []byte
expectDeserialized *dataNode
expectDeserializeErr require.ErrorAssertionFunc
}{
{
name: "nil",
node: func() *dataNode {
return nil
},
expectSerialized: []byte{},
expectDeserialized: nil,
expectDeserializeErr: require.Error,
},
{
name: "empty",
node: func() *dataNode {
return &dataNode{}
},
expectSerialized: []byte(`{"otelServiceName":"","values":{},"comments":[]}`),
expectDeserialized: &dataNode{},
expectDeserializeErr: require.NoError,
},
{
name: "with values",
node: func() *dataNode {
return &dataNode{
otel: &otelClient{
serviceName: "serviceName",
},
values: map[string]any{
"fisher": "flannigan",
"fitzbog": nil,
},
comment: comment{
Caller: "i am caller",
File: "i am file",
Message: "i am message",
},
}
},
expectSerialized: []byte(`{"otelServiceName":"serviceName",` +
`"values":{"fisher":"flannigan","fitzbog":""},` +
`"comments":[{"Caller":"i am caller","File":"i am file","Message":"i am message"}]}`),
expectDeserialized: &dataNode{
otel: &otelClient{
serviceName: "serviceName",
},
values: map[string]any{
"fisher": "flannigan",
"fitzbog": "",
},
},
expectDeserializeErr: require.NoError,
},
}
for _, test := range table {
t.Run(test.name, func(t *testing.T) {
serialized, err := test.node().Bytes()
require.NoError(t, err)

assert.Equalf(
t,
test.expectSerialized,
serialized,
"expected:\t%s\ngot:\t\t%s\n",
string(test.expectSerialized),
string(serialized))

deserialized, err := FromBytes(serialized)
test.expectDeserializeErr(t, err)
require.Equal(t, test.expectDeserialized, deserialized)
})
}
}

0 comments on commit 0cb2ac7

Please sign in to comment.