Skip to content

dlog v1 finalization: unit-tests for common above 80% #837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions token/core/common/meta/metadata_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright IBM Corp. All Rights Reserved.

SPDX-License-Identifier: Apache-2.0
*/

package meta

import (
"testing"

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

func TestTransferActionMetadata(t *testing.T) {
tests := []struct {
name string
attrs map[interface{}]interface{}
expected map[string][]byte
}{
{
name: "Basic metadata extraction",
attrs: map[interface{}]interface{}{
"TransferMetadataPrefixKey1": []byte("value1"),
"TransferMetadataPrefixKey2": []byte("value2"),
},
expected: map[string][]byte{
"Key1": []byte("value1"),
"Key2": []byte("value2"),
},
},
{
name: "Empty attrs",
attrs: map[interface{}]interface{}{},
expected: map[string][]byte{},
},
{
name: "Non-string keys",
attrs: map[interface{}]interface{}{
123: []byte("value"),
},
expected: map[string][]byte{},
},
{
name: "Invalid value types",
attrs: map[interface{}]interface{}{
"TransferMetadataPrefixKey": 123,
},
expected: map[string][]byte{},
},
{
name: "Exact prefix match",
attrs: map[interface{}]interface{}{
"TransferMetadataPrefix": []byte("value"),
},
expected: map[string][]byte{
"": []byte("value"),
},
},
{
name: "No prefix match",
attrs: map[interface{}]interface{}{
"WrongPrefixKey": []byte("value"),
},
expected: map[string][]byte{},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := TransferActionMetadata(tt.attrs)
assert.Equal(t, len(result), len(tt.expected), "Expected %d items, got %d", len(tt.expected), len(result))
for key, value := range tt.expected {
gotValue, ok := result[key]
assert.True(t, ok, "Expected key %s to exist in result", key)
assert.Equal(t, value, gotValue, "Expected value for %s: %v, got: %v", key, value, gotValue)
}
})
}
}
4 changes: 2 additions & 2 deletions token/core/common/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/hyperledger-labs/fabric-smart-client/platform/view/services/metrics"
)

//go:generate counterfeiter -o mock/provider.go -fake-name Provider . Provider

type (
CounterOpts = metrics.CounterOpts
Counter = metrics.Counter
Expand All @@ -20,5 +22,3 @@ type (
Provider = metrics.Provider
MetricLabel = string
)

var GetProvider = metrics.GetProvider
260 changes: 260 additions & 0 deletions token/core/common/metrics/mock/provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions token/core/common/metrics/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func recoverFromDuplicate(recovered any) {
func AllLabelNames(extraLabels ...MetricLabel) []MetricLabel {
return append([]string{NetworkLabel, ChannelLabel, NamespaceLabel}, extraLabels...)
}

func StatsdFormat(extraLabels ...MetricLabel) string {
return "%{#fqname}.%{" + strings.Join(AllLabelNames(extraLabels...), "}.%{") + "}"
}
Loading
Loading