@@ -3,29 +3,30 @@ package plugin
33import (
44 "context"
55 "encoding/json"
6- "os"
76 "testing"
8- "time"
97
10- "github.com/NeedleInAJayStack/haystack/client "
8+ "github.com/NeedleInAJayStack/haystack"
119 "github.com/google/go-cmp/cmp"
1210 "github.com/grafana/grafana-plugin-sdk-go/backend"
13- "github.com/grafana/grafana-plugin-sdk-go/backend/log"
1411 "github.com/grafana/grafana-plugin-sdk-go/data"
15- "github.com/joho/godotenv"
1612)
1713
18- // To run these tests, do the following:
19- // 1. Start a Haystack server you can access
20- // 1. Set up a `.env` file in the `/pkg` directory with these env vars: `TEST_URL`, `TEST_USERNAME`, `TEST_PASSWORD`
21-
2214func TestQueryData_Eval (t * testing.T ) {
15+ evalResponse := haystack .NewGridBuilder ()
16+ evalResponse .AddCol ("a" , map [string ]haystack.Val {})
17+ evalResponse .AddCol ("b" , map [string ]haystack.Val {})
18+ evalResponse .AddRow ([]haystack.Val {haystack .NewStr ("a" ), haystack .NewStr ("b" )})
19+
20+ client := & testHaystackClient {
21+ evalResponse : evalResponse .ToGrid (),
22+ }
23+
2324 actual := getResponse (
25+ client ,
2426 & QueryModel {
2527 Type : "Eval" ,
2628 Eval : "{a: \" a\" , b: \" b\" }" ,
2729 },
28- backend.TimeRange {},
2930 t ,
3031 )
3132
@@ -41,51 +42,11 @@ func TestQueryData_Eval(t *testing.T) {
4142 }
4243}
4344
44- func TestQueryData_Eval_Variables (t * testing.T ) {
45- actual := getResponse (
46- & QueryModel {
47- Type : "Eval" ,
48- Eval : "[{ts: $__timeRange_start, v0: 0}, {ts: $__timeRange_end, v0: 10}].toGrid" ,
49- },
50- backend.TimeRange {
51- From : time .Unix (0 , 0 ),
52- To : time .Unix (60 , 0 ),
53- },
54- t ,
55- )
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- }
76- }
77-
78- func getResponse (queryModel * QueryModel , timeRange backend.TimeRange , t * testing.T ) * data.Frame {
79- err := godotenv .Load ("../.env" )
80- if err != nil {
81- log .DefaultLogger .Warn (".env file not found, falling back to local environment" )
82- }
83-
84- client := client .NewClient (
85- os .Getenv ("TEST_URL" ),
86- os .Getenv ("TEST_USERNAME" ),
87- os .Getenv ("TEST_PASSWORD" ),
88- )
45+ func getResponse (
46+ client HaystackClient ,
47+ queryModel * QueryModel ,
48+ t * testing.T ,
49+ ) * data.Frame {
8950 if client .Open () != nil {
9051 t .Fatal ("Failed to open connection. Is a local Haxall server running?" )
9152 }
@@ -106,9 +67,8 @@ func getResponse(queryModel *QueryModel, timeRange backend.TimeRange, t *testing
10667 & backend.QueryDataRequest {
10768 Queries : []backend.DataQuery {
10869 {
109- RefID : refID ,
110- JSON : rawJson ,
111- TimeRange : timeRange ,
70+ RefID : refID ,
71+ JSON : rawJson ,
11272 },
11373 },
11474 },
@@ -132,3 +92,40 @@ func getResponse(queryModel *QueryModel, timeRange backend.TimeRange, t *testing
13292 }
13393 return queryResponse .Frames [0 ]
13494}
95+
96+ // TestHaystackClient is a mock of the HaystackClient interface
97+ type testHaystackClient struct {
98+ evalResponse haystack.Grid
99+ hisReadResponse haystack.Grid
100+ readResponse haystack.Grid
101+ }
102+
103+ // Open is a no-op
104+ func (c * testHaystackClient ) Open () error {
105+ return nil
106+ }
107+
108+ // Close is a no-op
109+ func (c * testHaystackClient ) Close () error {
110+ return nil
111+ }
112+
113+ // About returns an empty dict
114+ func (c * testHaystackClient ) About () (haystack.Dict , error ) {
115+ return haystack.Dict {}, nil
116+ }
117+
118+ // Eval returns the EvalResponse
119+ func (c * testHaystackClient ) Eval (query string ) (haystack.Grid , error ) {
120+ return c .evalResponse , nil
121+ }
122+
123+ // HisRead returns the HisReadResponse
124+ func (c * testHaystackClient ) HisReadAbsDateTime (ref haystack.Ref , start haystack.DateTime , end haystack.DateTime ) (haystack.Grid , error ) {
125+ return c .hisReadResponse , nil
126+ }
127+
128+ // Read returns the ReadResponse
129+ func (c * testHaystackClient ) Read (query string ) (haystack.Grid , error ) {
130+ return c .readResponse , nil
131+ }
0 commit comments