@@ -20,3 +20,41 @@ func TestConversionsDefaults(t *testing.T) {
2020 require .Equal (t , int64 (100 ), res .MaxDataPoints )
2121 require .Equal (t , time .Second , res .Interval )
2222}
23+
24+ func TestToBackendDataQueryJSON (t * testing.T ) {
25+ q := DataQuery {
26+ CommonQueryProperties : CommonQueryProperties {
27+ RefID : "A" ,
28+ TimeRange : & TimeRange {
29+ From : "12345678" ,
30+ To : "87654321" ,
31+ },
32+ Datasource : & DataSourceRef {
33+ Type : "prometheus" ,
34+ UID : "hello-world" ,
35+ },
36+ QueryType : "interesting" ,
37+ MaxDataPoints : 42 ,
38+ IntervalMS : 15.0 ,
39+ },
40+ }
41+
42+ q .Set ("key1" , "value1" )
43+ q .Set ("key2" , "value2" )
44+
45+ bq , err := toBackendDataQuery (q , nil )
46+ require .NoError (t , err )
47+
48+ require .Equal (t , "A" , bq .RefID )
49+ require .Equal (t , "interesting" , bq .QueryType )
50+ require .Equal (t , int64 (42 ), bq .MaxDataPoints )
51+ require .Equal (t , time .Millisecond * 15 , bq .Interval )
52+
53+ require .NotNil (t , bq .TimeRange )
54+ require .Equal (t , time .UnixMilli (12345678 ).UTC (), bq .TimeRange .From )
55+ require .Equal (t , time .UnixMilli (87654321 ).UTC (), bq .TimeRange .To )
56+
57+ jsonData := `{"refId":"A","timeRange":{"from":"12345678","to":"87654321"},"datasource":{"type":"prometheus","uid":"hello-world"},"queryType":"interesting","maxDataPoints":42,"intervalMs":15,"key1":"value1","key2":"value2"}`
58+
59+ require .Equal (t , jsonData , string (bq .JSON ))
60+ }
0 commit comments