|
| 1 | +package querybackend |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + "google.golang.org/grpc/codes" |
| 9 | + "google.golang.org/grpc/status" |
| 10 | + |
| 11 | + typesv1 "github.com/grafana/pyroscope/api/gen/proto/go/types/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func TestValidateExemplarType(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + exemplarType typesv1.ExemplarType |
| 18 | + expectedInclude bool |
| 19 | + expectedErrorMsg string |
| 20 | + expectedCode codes.Code |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "UNSPECIFIED returns false, no error", |
| 24 | + exemplarType: typesv1.ExemplarType_EXEMPLAR_TYPE_UNSPECIFIED, |
| 25 | + expectedInclude: false, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "NONE returns false, no error", |
| 29 | + exemplarType: typesv1.ExemplarType_EXEMPLAR_TYPE_NONE, |
| 30 | + expectedInclude: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "INDIVIDUAL returns true, no error", |
| 34 | + exemplarType: typesv1.ExemplarType_EXEMPLAR_TYPE_INDIVIDUAL, |
| 35 | + expectedInclude: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "SPAN returns error with Unimplemented code", |
| 39 | + exemplarType: typesv1.ExemplarType_EXEMPLAR_TYPE_SPAN, |
| 40 | + expectedInclude: false, |
| 41 | + expectedErrorMsg: "exemplar type span is not implemented", |
| 42 | + expectedCode: codes.Unimplemented, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Unknown type returns error with InvalidArgument code", |
| 46 | + exemplarType: typesv1.ExemplarType(999), |
| 47 | + expectedInclude: false, |
| 48 | + expectedErrorMsg: "unknown exemplar type", |
| 49 | + expectedCode: codes.InvalidArgument, |
| 50 | + }, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tt := range tests { |
| 54 | + t.Run(tt.name, func(t *testing.T) { |
| 55 | + include, err := validateExemplarType(tt.exemplarType) |
| 56 | + if tt.expectedErrorMsg != "" { |
| 57 | + require.Error(t, err) |
| 58 | + assert.Contains(t, err.Error(), tt.expectedErrorMsg) |
| 59 | + st, ok := status.FromError(err) |
| 60 | + require.True(t, ok) |
| 61 | + assert.Equal(t, tt.expectedCode, st.Code()) |
| 62 | + } else { |
| 63 | + require.NoError(t, err) |
| 64 | + assert.Equal(t, tt.expectedInclude, include) |
| 65 | + } |
| 66 | + }) |
| 67 | + } |
| 68 | +} |
0 commit comments