@@ -3,62 +3,120 @@ package plugin
33import (
44 "context"
55 "encoding/json"
6- "fmt"
7- "os"
86 "testing"
97 "time"
108
11- "github.com/NeedleInAJayStack/haystack/client"
9+ "github.com/NeedleInAJayStack/haystack"
10+ "github.com/google/go-cmp/cmp"
1211 "github.com/grafana/grafana-plugin-sdk-go/backend"
13- "github.com/grafana/grafana-plugin-sdk-go/backend/log"
1412 "github.com/grafana/grafana-plugin-sdk-go/data"
15- "github.com/joho/godotenv"
1613)
1714
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-
2215func TestQueryData_Eval (t * testing.T ) {
23- data := getResponse (
16+ response := haystack .NewGridBuilder ()
17+ response .AddCol ("a" , map [string ]haystack.Val {})
18+ response .AddCol ("b" , map [string ]haystack.Val {})
19+ response .AddRow ([]haystack.Val {haystack .NewStr ("a" ), haystack .NewStr ("b" )})
20+
21+ client := & testHaystackClient {
22+ evalResponse : response .ToGrid (),
23+ }
24+
25+ actual := getResponse (
26+ client ,
2427 & QueryModel {
2528 Type : "Eval" ,
26- Eval : "[{ts: now()-1hr, v0: 0}, {ts: now(), v0: 10}].toGrid " ,
29+ Eval : "{a: \" a \" , b: \" b \" } " ,
2730 },
28- backend.TimeRange {},
2931 t ,
3032 )
31- table , _ := data .StringTable (10 , 100 )
32- fmt .Printf ("frame: %v\n " , table )
33+
34+ aVal := "a"
35+ bVal := "b"
36+ expected := data .NewFrame ("" ,
37+ data .NewField ("a" , nil , []* string {& aVal }).SetConfig (& data.FieldConfig {DisplayName : "a" }),
38+ data .NewField ("b" , nil , []* string {& bVal }).SetConfig (& data.FieldConfig {DisplayName : "b" }),
39+ )
40+
41+ if ! cmp .Equal (actual , expected , data .FrameTestCompareOptions ()... ) {
42+ t .Error (cmp .Diff (actual , expected , data .FrameTestCompareOptions ()... ))
43+ }
3344}
3445
35- func TestQueryData_Eval_Variables (t * testing.T ) {
36- data := getResponse (
46+ func TestQueryData_HisRead (t * testing.T ) {
47+ response := haystack .NewGridBuilder ()
48+ response .AddCol ("ts" , map [string ]haystack.Val {})
49+ response .AddCol ("v0" , map [string ]haystack.Val {})
50+ response .AddRow ([]haystack.Val {haystack .NewDateTimeFromGo (time .Unix (0 , 0 )), haystack .NewNumber (5 , "kWh" )})
51+
52+ client := & testHaystackClient {
53+ hisReadResponse : response .ToGrid (),
54+ }
55+
56+ actual := getResponse (
57+ client ,
3758 & QueryModel {
38- Type : "Eval" ,
39- Eval : "[{ts: $__timeRange_start, v0: 0}, {ts: $__timeRange_end, v0: 10}].toGrid" ,
40- },
41- backend.TimeRange {
42- From : time .Now ().Add (- 1 * time .Hour ),
43- To : time .Now (),
59+ Type : "HisRead" ,
60+ HisRead : "abcdefg-12345678" ,
4461 },
4562 t ,
4663 )
47- table , _ := data .StringTable (10 , 100 )
48- fmt .Printf ("frame: %v\n " , table )
64+
65+ tsVal := time .Unix (0 , 0 )
66+ v0Val := 5.0
67+ expected := data .NewFrame ("" ,
68+ data .NewField ("ts" , nil , []* time.Time {& tsVal }).SetConfig (& data.FieldConfig {DisplayName : "ts" }),
69+ data .NewField ("v0" , nil , []* float64 {& v0Val }).SetConfig (& data.FieldConfig {DisplayName : "v0" }),
70+ )
71+
72+ if ! cmp .Equal (actual , expected , data .FrameTestCompareOptions ()... ) {
73+ t .Error (cmp .Diff (actual , expected , data .FrameTestCompareOptions ()... ))
74+ }
4975}
5076
51- func getResponse (queryModel * QueryModel , timeRange backend.TimeRange , t * testing.T ) data.Frame {
52- err := godotenv .Load ("../.env" )
53- if err != nil {
54- log .DefaultLogger .Warn (".env file not found, falling back to local environment" )
77+ func TestQueryData_Read (t * testing.T ) {
78+ response := haystack .NewGridBuilder ()
79+ response .AddCol ("id" , map [string ]haystack.Val {})
80+ response .AddCol ("dis" , map [string ]haystack.Val {})
81+ response .AddCol ("ahu" , map [string ]haystack.Val {})
82+ response .AddRow ([]haystack.Val {
83+ haystack .NewRef ("abcdefg-12345678" , "AHU-1" ),
84+ haystack .NewStr ("AHU-1" ),
85+ haystack .NewMarker (),
86+ })
87+
88+ client := & testHaystackClient {
89+ readResponse : response .ToGrid (),
5590 }
5691
57- client := client .NewClient (
58- os .Getenv ("TEST_URL" ),
59- os .Getenv ("TEST_USERNAME" ),
60- os .Getenv ("TEST_PASSWORD" ),
92+ actual := getResponse (
93+ client ,
94+ & QueryModel {
95+ Type : "Read" ,
96+ Read : "ahu" ,
97+ },
98+ t ,
99+ )
100+
101+ idVal := "@abcdefg-12345678 \" AHU-1\" "
102+ disVal := "AHU-1"
103+ ahuVal := "M"
104+ expected := data .NewFrame ("" ,
105+ data .NewField ("id" , nil , []* string {& idVal }).SetConfig (& data.FieldConfig {DisplayName : "id" }),
106+ data .NewField ("dis" , nil , []* string {& disVal }).SetConfig (& data.FieldConfig {DisplayName : "dis" }),
107+ data .NewField ("ahu" , nil , []* string {& ahuVal }).SetConfig (& data.FieldConfig {DisplayName : "ahu" }),
61108 )
109+
110+ if ! cmp .Equal (actual , expected , data .FrameTestCompareOptions ()... ) {
111+ t .Error (cmp .Diff (actual , expected , data .FrameTestCompareOptions ()... ))
112+ }
113+ }
114+
115+ func getResponse (
116+ client HaystackClient ,
117+ queryModel * QueryModel ,
118+ t * testing.T ,
119+ ) * data.Frame {
62120 if client .Open () != nil {
63121 t .Fatal ("Failed to open connection. Is a local Haxall server running?" )
64122 }
@@ -79,9 +137,8 @@ func getResponse(queryModel *QueryModel, timeRange backend.TimeRange, t *testing
79137 & backend.QueryDataRequest {
80138 Queries : []backend.DataQuery {
81139 {
82- RefID : refID ,
83- JSON : rawJson ,
84- TimeRange : timeRange ,
140+ RefID : refID ,
141+ JSON : rawJson ,
85142 },
86143 },
87144 },
@@ -103,5 +160,42 @@ func getResponse(queryModel *QueryModel, timeRange backend.TimeRange, t *testing
103160 if len (queryResponse .Frames ) != 1 {
104161 t .Fatal ("Currently only support single-frame results" )
105162 }
106- return * queryResponse .Frames [0 ]
163+ return queryResponse .Frames [0 ]
164+ }
165+
166+ // TestHaystackClient is a mock of the HaystackClient interface
167+ type testHaystackClient struct {
168+ evalResponse haystack.Grid
169+ hisReadResponse haystack.Grid
170+ readResponse haystack.Grid
171+ }
172+
173+ // Open is a no-op
174+ func (c * testHaystackClient ) Open () error {
175+ return nil
176+ }
177+
178+ // Close is a no-op
179+ func (c * testHaystackClient ) Close () error {
180+ return nil
181+ }
182+
183+ // About returns an empty dict
184+ func (c * testHaystackClient ) About () (haystack.Dict , error ) {
185+ return haystack.Dict {}, nil
186+ }
187+
188+ // Eval returns the EvalResponse
189+ func (c * testHaystackClient ) Eval (query string ) (haystack.Grid , error ) {
190+ return c .evalResponse , nil
191+ }
192+
193+ // HisRead returns the HisReadResponse
194+ func (c * testHaystackClient ) HisReadAbsDateTime (ref haystack.Ref , start haystack.DateTime , end haystack.DateTime ) (haystack.Grid , error ) {
195+ return c .hisReadResponse , nil
196+ }
197+
198+ // Read returns the ReadResponse
199+ func (c * testHaystackClient ) Read (query string ) (haystack.Grid , error ) {
200+ return c .readResponse , nil
107201}
0 commit comments