@@ -3,12 +3,14 @@ package v2
33import (
44 "testing"
55
6+ "github.com/stretchr/testify/assert"
67 "github.com/stretchr/testify/require"
78
89 "github.com/smartcontractkit/chainlink-common/pkg/settings"
910 "github.com/smartcontractkit/chainlink-common/pkg/settings/cresettings"
1011 "github.com/smartcontractkit/chainlink-common/pkg/settings/limits"
1112 "github.com/smartcontractkit/chainlink-protos/cre/go/sdk"
13+ eventsv2 "github.com/smartcontractkit/chainlink-protos/workflows/go/v2"
1214 "github.com/smartcontractkit/chainlink/v2/core/logger"
1315)
1416
@@ -51,3 +53,91 @@ func TestExecutionHelper_ConfidentialHTTPPerWorkflowLimit(t *testing.T) {
5153 _ , err = exec .CallCapability (t .Context (), req )
5254 require .Error (t , err , "expected CallCapability to fail when per-workflow confidential-http call limit is exceeded" )
5355}
56+
57+ func TestUserMetricTypeSuffix (t * testing.T ) {
58+ t .Parallel ()
59+
60+ tests := []struct {
61+ name string
62+ metricType eventsv2.UserMetricType
63+ wantSuffix string
64+ wantErr bool
65+ }{
66+ {
67+ name : "counter" ,
68+ metricType : eventsv2 .UserMetricType_USER_METRIC_TYPE_COUNTER ,
69+ wantSuffix : "_counter" ,
70+ },
71+ {
72+ name : "gauge" ,
73+ metricType : eventsv2 .UserMetricType_USER_METRIC_TYPE_GAUGE ,
74+ wantSuffix : "_gauge" ,
75+ },
76+ {
77+ name : "unspecified" ,
78+ metricType : eventsv2 .UserMetricType_USER_METRIC_TYPE_UNSPECIFIED ,
79+ wantErr : true ,
80+ },
81+ {
82+ name : "unknown numeric value" ,
83+ metricType : eventsv2 .UserMetricType (999 ),
84+ wantErr : true ,
85+ },
86+ }
87+
88+ for _ , tc := range tests {
89+ t .Run (tc .name , func (t * testing.T ) {
90+ t .Parallel ()
91+ suffix , err := userMetricTypeSuffix (tc .metricType )
92+ if tc .wantErr {
93+ require .Error (t , err )
94+ assert .Contains (t , err .Error (), "unsupported user metric type" )
95+ } else {
96+ require .NoError (t , err )
97+ assert .Equal (t , tc .wantSuffix , suffix )
98+ }
99+ })
100+ }
101+ }
102+
103+ func TestUserMetricNameFormatting (t * testing.T ) {
104+ t .Parallel ()
105+
106+ tests := []struct {
107+ name string
108+ metricName string
109+ metricType eventsv2.UserMetricType
110+ wantName string
111+ }{
112+ {
113+ name : "counter metric gets prefix and suffix" ,
114+ metricName : "price" ,
115+ metricType : eventsv2 .UserMetricType_USER_METRIC_TYPE_COUNTER ,
116+ wantName : "user_workflow_price_counter" ,
117+ },
118+ {
119+ name : "gauge metric gets prefix and suffix" ,
120+ metricName : "temperature" ,
121+ metricType : eventsv2 .UserMetricType_USER_METRIC_TYPE_GAUGE ,
122+ wantName : "user_workflow_temperature_gauge" ,
123+ },
124+ }
125+
126+ for _ , tc := range tests {
127+ t .Run (tc .name , func (t * testing.T ) {
128+ t .Parallel ()
129+ suffix , err := userMetricTypeSuffix (tc .metricType )
130+ require .NoError (t , err )
131+ got := userMetricPrefix + tc .metricName + suffix
132+ assert .Equal (t , tc .wantName , got )
133+ })
134+ }
135+ }
136+
137+ func TestUserMetricUnsupportedTypeRejected (t * testing.T ) {
138+ t .Parallel ()
139+
140+ _ , err := userMetricTypeSuffix (eventsv2 .UserMetricType_USER_METRIC_TYPE_UNSPECIFIED )
141+ require .Error (t , err )
142+ assert .Contains (t , err .Error (), "unsupported user metric type" )
143+ }
0 commit comments