@@ -3,12 +3,12 @@ package plugin
33import (
44 "context"
55 "encoding/json"
6- "fmt"
76 "os"
87 "testing"
98 "time"
109
1110 "github.com/NeedleInAJayStack/haystack/client"
11+ "github.com/google/go-cmp/cmp"
1212 "github.com/grafana/grafana-plugin-sdk-go/backend"
1313 "github.com/grafana/grafana-plugin-sdk-go/backend/log"
1414 "github.com/grafana/grafana-plugin-sdk-go/data"
@@ -20,35 +20,62 @@ import (
2020// 1. Set up a `.env` file in the `/pkg` directory with these env vars: `TEST_URL`, `TEST_USERNAME`, `TEST_PASSWORD`
2121
2222func TestQueryData_Eval (t * testing.T ) {
23- data := getResponse (
23+ actual := getResponse (
2424 & QueryModel {
2525 Type : "Eval" ,
26- Eval : "[{ts: now()-1hr, v0: 0}, {ts: now(), v0: 10}].toGrid " ,
26+ Eval : "{a: \" a \" , b: \" b \" } " ,
2727 },
2828 backend.TimeRange {},
2929 t ,
3030 )
31- table , _ := data .StringTable (10 , 100 )
32- fmt .Printf ("frame: %v\n " , table )
31+
32+ aVal := "a"
33+ bVal := "b"
34+ expected := data .NewFrame ("" ,
35+ data .NewField ("a" , nil , []* string {& aVal }).SetConfig (& data.FieldConfig {DisplayName : "a" }),
36+ data .NewField ("b" , nil , []* string {& bVal }).SetConfig (& data.FieldConfig {DisplayName : "b" }),
37+ )
38+
39+ if ! cmp .Equal (actual , expected , data .FrameTestCompareOptions ()... ) {
40+ t .Error (cmp .Diff (actual , expected , data .FrameTestCompareOptions ()... ))
41+ }
3342}
3443
3544func TestQueryData_Eval_Variables (t * testing.T ) {
36- data := getResponse (
45+ actual := getResponse (
3746 & QueryModel {
3847 Type : "Eval" ,
3948 Eval : "[{ts: $__timeRange_start, v0: 0}, {ts: $__timeRange_end, v0: 10}].toGrid" ,
4049 },
4150 backend.TimeRange {
42- From : time .Now (). Add ( - 1 * time . Hour ),
43- To : time .Now ( ),
51+ From : time .Unix ( 0 , 0 ),
52+ To : time .Unix ( 60 , 0 ),
4453 },
4554 t ,
4655 )
47- table , _ := data .StringTable (10 , 100 )
48- fmt .Printf ("frame: %v\n " , table )
56+
57+ var v0_0 , v0_1 float64
58+ ts_0 := time .Unix (0 , 0 )
59+ v0_0 = 0
60+ ts_1 := time .Unix (60 , 0 )
61+ v0_1 = 10
62+ expected := data .NewFrame ("" ,
63+ data .NewField ("ts" , nil , []* time.Time {
64+ & ts_0 ,
65+ & ts_1 ,
66+ }).SetConfig (& data.FieldConfig {DisplayName : "ts" }),
67+ data .NewField ("v0" , nil , []* float64 {
68+ & v0_0 ,
69+ & v0_1 ,
70+ }).SetConfig (& data.FieldConfig {DisplayName : "v0" }),
71+ )
72+
73+ if ! cmp .Equal (actual , expected , data .FrameTestCompareOptions ()... ) {
74+ t .Error (cmp .Diff (actual , expected , data .FrameTestCompareOptions ()... ))
75+ }
4976}
5077
51- func getResponse (queryModel * QueryModel , timeRange backend.TimeRange , t * testing.T ) data.Frame {
78+ func getResponse (queryModel * QueryModel , timeRange backend.TimeRange , t * testing.T ) * data.Frame {
5279 err := godotenv .Load ("../.env" )
5380 if err != nil {
5481 log .DefaultLogger .Warn (".env file not found, falling back to local environment" )
@@ -103,5 +130,5 @@ func getResponse(queryModel *QueryModel, timeRange backend.TimeRange, t *testing
103130 if len (queryResponse .Frames ) != 1 {
104131 t .Fatal ("Currently only support single-frame results" )
105132 }
106- return * queryResponse .Frames [0 ]
133+ return queryResponse .Frames [0 ]
107134}
0 commit comments