2025-05-16 20:09:57 +05:30
package telemetrytraces
import (
"context"
"testing"
qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetFieldKeyName ( t * testing . T ) {
ctx := context . Background ( )
testCases := [ ] struct {
name string
key telemetrytypes . TelemetryFieldKey
expectedResult string
expectedError error
} {
{
name : "Simple column type - timestamp" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "timestamp" ,
FieldContext : telemetrytypes . FieldContextSpan ,
} ,
expectedResult : "timestamp" ,
expectedError : nil ,
} ,
{
name : "Map column type - string attribute" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "user.id" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeString ,
} ,
expectedResult : "attributes_string['user.id']" ,
expectedError : nil ,
} ,
{
name : "Map column type - number attribute" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "request.size" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeNumber ,
} ,
expectedResult : "attributes_number['request.size']" ,
expectedError : nil ,
} ,
{
name : "Map column type - bool attribute" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "request.success" ,
FieldContext : telemetrytypes . FieldContextAttribute ,
FieldDataType : telemetrytypes . FieldDataTypeBool ,
} ,
expectedResult : "attributes_bool['request.success']" ,
expectedError : nil ,
} ,
{
name : "Map column type - resource attribute" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "service.name" ,
FieldContext : telemetrytypes . FieldContextResource ,
} ,
2025-09-14 18:18:39 +05:30
expectedResult : "multiIf(resource.`service.name` IS NOT NULL, resource.`service.name`::String, mapContains(resources_string, 'service.name'), resources_string['service.name'], NULL)" ,
expectedError : nil ,
} ,
{
2025-10-03 11:12:42 +05:30
name : "Map column type - resource attribute - materialized" ,
2025-09-14 18:18:39 +05:30
key : telemetrytypes . TelemetryFieldKey {
2025-10-03 11:12:42 +05:30
Name : "deployment.environment" ,
FieldContext : telemetrytypes . FieldContextResource ,
FieldDataType : telemetrytypes . FieldDataTypeString ,
Materialized : true ,
2025-09-14 18:18:39 +05:30
} ,
2025-10-03 11:12:42 +05:30
expectedResult : "multiIf(resource.`deployment.environment` IS NOT NULL, resource.`deployment.environment`::String, `resource_string_deployment$$environment_exists`==true, `resource_string_deployment$$environment`, NULL)" ,
2025-05-16 20:09:57 +05:30
expectedError : nil ,
} ,
{
name : "Non-existent column" ,
key : telemetrytypes . TelemetryFieldKey {
Name : "nonexistent_field" ,
FieldContext : telemetrytypes . FieldContextSpan ,
} ,
expectedResult : "" ,
expectedError : qbtypes . ErrColumnNotFound ,
} ,
}
for _ , tc := range testCases {
t . Run ( tc . name , func ( t * testing . T ) {
2025-09-14 18:18:39 +05:30
fm := NewFieldMapper ( )
2025-05-16 20:09:57 +05:30
result , err := fm . FieldFor ( ctx , & tc . key )
if tc . expectedError != nil {
assert . Equal ( t , tc . expectedError , err )
} else {
require . NoError ( t , err )
assert . Equal ( t , tc . expectedResult , result )
}
} )
}
}