2023-10-09 06:41:17 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type nodeMeta struct {
|
|
|
|
key string
|
|
|
|
value []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m nodeMeta) GetKey() string {
|
|
|
|
return m.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m nodeMeta) GetValue() []byte {
|
|
|
|
return m.value
|
|
|
|
}
|
|
|
|
|
|
|
|
type nodeResponse struct {
|
|
|
|
meta []nodeMeta
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp []uint64
|
2023-10-09 06:41:17 +00:00
|
|
|
}
|
|
|
|
|
2024-09-26 14:32:27 +00:00
|
|
|
func (n nodeResponse) GetTimestamp() []uint64 {
|
2024-09-18 04:35:26 +00:00
|
|
|
return n.timestamp
|
2023-10-09 06:41:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (n nodeResponse) GetMeta() []Meta {
|
|
|
|
res := make([]Meta, len(n.meta))
|
|
|
|
for i, value := range n.meta {
|
|
|
|
res[i] = value
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2024-09-26 14:32:27 +00:00
|
|
|
func (n nodeResponse) GetNodeID() []uint64 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (n nodeResponse) GetParentID() []uint64 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-09 06:41:17 +00:00
|
|
|
func TestGetLatestNode(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
nodes []NodeResponse
|
|
|
|
exceptedOID string
|
|
|
|
error bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty",
|
|
|
|
nodes: []NodeResponse{},
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one node of the object version",
|
|
|
|
nodes: []NodeResponse{
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{1},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{
|
|
|
|
{
|
|
|
|
key: oidKV,
|
|
|
|
value: []byte("oid1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exceptedOID: "oid1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "one node of the object version and one node of the secondary object",
|
|
|
|
nodes: []NodeResponse{
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{3},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{1},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{
|
|
|
|
{
|
|
|
|
key: oidKV,
|
|
|
|
value: []byte("oid1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exceptedOID: "oid1",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "all nodes represent a secondary object",
|
|
|
|
nodes: []NodeResponse{
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{3},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{5},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
error: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "several nodes of different types and with different timestamp",
|
|
|
|
nodes: []NodeResponse{
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{1},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{
|
|
|
|
{
|
|
|
|
key: oidKV,
|
|
|
|
value: []byte("oid1"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{3},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{4},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{
|
|
|
|
{
|
|
|
|
key: oidKV,
|
|
|
|
value: []byte("oid2"),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
nodeResponse{
|
2024-09-18 04:35:26 +00:00
|
|
|
timestamp: []uint64{6},
|
2023-10-09 06:41:17 +00:00
|
|
|
meta: []nodeMeta{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
exceptedOID: "oid2",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
2024-09-26 14:32:27 +00:00
|
|
|
actualNode, err := getLatestVersionNode(tc.nodes)
|
2023-10-09 06:41:17 +00:00
|
|
|
if tc.error {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tc.exceptedOID, string(actualNode.GetMeta()[0].GetValue()))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|