Skip to content

Commit

Permalink
refactor unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: rim99 <[email protected]>
  • Loading branch information
rim99 committed Nov 26, 2024
1 parent 5edb49b commit af0dee9
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions cmd/query/app/apiv3/grpc_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,53 +78,57 @@ func newTestServerClient(t *testing.T) *testServerClient {
}

func TestGetTrace(t *testing.T) {
traceIdLiteral := "156"
traceId, _ := model.TraceIDFromString(traceIdLiteral)
inputs := []struct {
traceId, _ := model.TraceIDFromString("156")
testCases := []struct {
name string
expectedQuery spanstore.GetTraceParameters
request api_v3.GetTraceRequest
}{
{
"TestGetTrace",
spanstore.GetTraceParameters{
TraceID: traceId,
StartTime: time.Time{},
EndTime: time.Time{},
},
api_v3.GetTraceRequest{TraceId: traceIdLiteral},
api_v3.GetTraceRequest{TraceId: "156"},
},
{
"TestGetTraceWithTimeWindow",
spanstore.GetTraceParameters{
TraceID: traceId,
StartTime: time.Unix(1, 2).UTC(),
EndTime: time.Unix(3, 4).UTC(),
},
api_v3.GetTraceRequest{
TraceId: traceIdLiteral,
TraceId: "156",
StartTime: time.Unix(1, 2).UTC(),
EndTime: time.Unix(3, 4).UTC(),
},
},
}

for _, input := range inputs {
tsc := newTestServerClient(t)
tsc.reader.On("GetTrace", matchContext, input.expectedQuery).Return(
&model.Trace{
Spans: []*model.Span{
{
OperationName: "foobar",
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
tsc := newTestServerClient(t)
tsc.reader.On("GetTrace", matchContext, tc.expectedQuery).Return(
&model.Trace{
Spans: []*model.Span{
{
OperationName: "foobar",
},
},
},
}, nil).Once()

getTraceStream, err := tsc.client.GetTrace(context.Background(), &input.request)
require.NoError(t, err)
recv, err := getTraceStream.Recv()
require.NoError(t, err)
td := recv.ToTraces()
require.EqualValues(t, 1, td.SpanCount())
assert.Equal(t, "foobar",
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name())
}, nil).Once()

getTraceStream, err := tsc.client.GetTrace(context.Background(), &tc.request)
require.NoError(t, err)
recv, err := getTraceStream.Recv()
require.NoError(t, err)
td := recv.ToTraces()
require.EqualValues(t, 1, td.SpanCount())
assert.Equal(t, "foobar",
td.ResourceSpans().At(0).ScopeSpans().At(0).Spans().At(0).Name())
})
}
}

Expand Down

0 comments on commit af0dee9

Please sign in to comment.