|
1 | 1 | package plugin |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/json" |
5 | 6 | "errors" |
6 | 7 | "fmt" |
7 | 8 | "testing" |
8 | 9 |
|
9 | 10 | "github.com/ClickHouse/clickhouse-go/v2" |
| 11 | + "github.com/grafana/grafana-plugin-sdk-go/backend" |
10 | 12 | "github.com/grafana/grafana-plugin-sdk-go/data" |
11 | 13 | "github.com/stretchr/testify/assert" |
12 | 14 | ) |
@@ -317,3 +319,109 @@ func TestContainsClickHouseException(t *testing.T) { |
317 | 319 | assert.True(t, result) |
318 | 320 | }) |
319 | 321 | } |
| 322 | + |
| 323 | +func TestMutateQueryData(t *testing.T) { |
| 324 | + h := &Clickhouse{} |
| 325 | + |
| 326 | + tests := []struct { |
| 327 | + name string |
| 328 | + headers map[string]string |
| 329 | + want grafanaHeaders |
| 330 | + stored bool |
| 331 | + }{ |
| 332 | + { |
| 333 | + name: "all headers", |
| 334 | + headers: map[string]string{ |
| 335 | + "http_X-Dashboard-Uid": "dash-abc123", |
| 336 | + "http_X-Panel-Id": "42", |
| 337 | + "http_X-Rule-Uid": "rule-xyz", |
| 338 | + }, |
| 339 | + want: grafanaHeaders{DashboardUID: "dash-abc123", PanelID: "42", RuleUID: "rule-xyz"}, |
| 340 | + stored: true, |
| 341 | + }, |
| 342 | + { |
| 343 | + name: "empty headers", |
| 344 | + headers: map[string]string{}, |
| 345 | + stored: false, |
| 346 | + }, |
| 347 | + { |
| 348 | + name: "only dashboard", |
| 349 | + headers: map[string]string{"http_X-Dashboard-Uid": "dash-only"}, |
| 350 | + want: grafanaHeaders{DashboardUID: "dash-only"}, |
| 351 | + stored: true, |
| 352 | + }, |
| 353 | + { |
| 354 | + name: "only panel", |
| 355 | + headers: map[string]string{"http_X-Panel-Id": "99"}, |
| 356 | + want: grafanaHeaders{PanelID: "99"}, |
| 357 | + stored: true, |
| 358 | + }, |
| 359 | + { |
| 360 | + name: "only rule", |
| 361 | + headers: map[string]string{"http_X-Rule-Uid": "alert-rule-1"}, |
| 362 | + want: grafanaHeaders{RuleUID: "alert-rule-1"}, |
| 363 | + stored: true, |
| 364 | + }, |
| 365 | + } |
| 366 | + |
| 367 | + for _, tt := range tests { |
| 368 | + t.Run(tt.name, func(t *testing.T) { |
| 369 | + req := &backend.QueryDataRequest{Headers: tt.headers} |
| 370 | + newCtx, _ := h.MutateQueryData(t.Context(), req) |
| 371 | + |
| 372 | + gh, ok := newCtx.Value(grafanaHeadersKey).(grafanaHeaders) |
| 373 | + assert.Equal(t, tt.stored, ok) |
| 374 | + if tt.stored { |
| 375 | + assert.Equal(t, tt.want, gh) |
| 376 | + } |
| 377 | + }) |
| 378 | + } |
| 379 | + |
| 380 | + t.Run("nil headers does not panic", func(t *testing.T) { |
| 381 | + newCtx, newReq := h.MutateQueryData(t.Context(), &backend.QueryDataRequest{}) |
| 382 | + assert.NotNil(t, newCtx) |
| 383 | + assert.NotNil(t, newReq) |
| 384 | + }) |
| 385 | +} |
| 386 | + |
| 387 | +func TestMutateQuery_GrafanaMetadata(t *testing.T) { |
| 388 | + h := &Clickhouse{} |
| 389 | + |
| 390 | + t.Run("includes dashboard and panel from context", func(t *testing.T) { |
| 391 | + ctx := context.WithValue(t.Context(), grafanaHeadersKey, grafanaHeaders{ |
| 392 | + DashboardUID: "my-dashboard", |
| 393 | + PanelID: "7", |
| 394 | + RuleUID: "alert-1", |
| 395 | + }) |
| 396 | + |
| 397 | + newCtx, _ := h.MutateQuery(ctx, backend.DataQuery{ |
| 398 | + JSON: []byte(`{}`), |
| 399 | + }) |
| 400 | + |
| 401 | + assert.NotEqual(t, ctx, newCtx) |
| 402 | + }) |
| 403 | + |
| 404 | + t.Run("no grafana headers in context still works", func(t *testing.T) { |
| 405 | + ctx := t.Context() |
| 406 | + |
| 407 | + newCtx, _ := h.MutateQuery(ctx, backend.DataQuery{ |
| 408 | + JSON: []byte(`{}`), |
| 409 | + }) |
| 410 | + |
| 411 | + assert.NotNil(t, newCtx) |
| 412 | + _, ok := newCtx.Value(grafanaHeadersKey).(grafanaHeaders) |
| 413 | + assert.False(t, ok) |
| 414 | + }) |
| 415 | + |
| 416 | + t.Run("handles invalid JSON gracefully", func(t *testing.T) { |
| 417 | + ctx := context.WithValue(t.Context(), grafanaHeadersKey, grafanaHeaders{ |
| 418 | + DashboardUID: "dash1", |
| 419 | + }) |
| 420 | + |
| 421 | + newCtx, _ := h.MutateQuery(ctx, backend.DataQuery{ |
| 422 | + JSON: []byte(`invalid json`), |
| 423 | + }) |
| 424 | + |
| 425 | + assert.NotEqual(t, ctx, newCtx) |
| 426 | + }) |
| 427 | +} |
0 commit comments