11import unittest
22from test .elements .test_datasource_fixtures import (
3+ DATAFRAME_RESPONSE_HEALTH_ELASTICSEARCH_VALID ,
34 DATAFRAME_RESPONSE_HEALTH_PROMETHEUS ,
45 ELASTICSEARCH_DATASOURCE ,
56 INFLUXDB1_DATASOURCE ,
@@ -271,7 +272,29 @@ def test_series(self, m):
271272 self .assertEqual (len (result ["data" ]["result" ][0 ]["values" ]), 6 )
272273
273274 @requests_mock .Mocker ()
274- def test_query_with_datasource_prometheus (self , m ):
275+ def test_query_with_datasource_prometheus_grafana7 (self , m ):
276+ # Mock the version inquiry request, because `smartquery` needs
277+ # it, as Prometheus responses differ between versions.
278+ m .get (
279+ "http://localhost/api/health" ,
280+ json = {"commit" : "unknown" , "database" : "ok" , "version" : "7.0.1" },
281+ )
282+ m .post (
283+ "http://localhost/api/ds/query" ,
284+ json = DATAFRAME_RESPONSE_HEALTH_PROMETHEUS ,
285+ )
286+ datasource = PROMETHEUS_DATASOURCE .copy ()
287+ response = self .grafana .datasource .smartquery (datasource , "1+1" )
288+ self .assertEqual (response , DATAFRAME_RESPONSE_HEALTH_PROMETHEUS )
289+
290+ @requests_mock .Mocker ()
291+ def test_query_with_datasource_prometheus_grafana9 (self , m ):
292+ # Mock the version inquiry request, because `smartquery` needs
293+ # it, as Prometheus responses differ between versions.
294+ m .get (
295+ "http://localhost/api/health" ,
296+ json = {"commit" : "14e988bd22" , "database" : "ok" , "version" : "9.0.1" },
297+ )
275298 m .post (
276299 "http://localhost/api/ds/query" ,
277300 json = DATAFRAME_RESPONSE_HEALTH_PROMETHEUS ,
@@ -298,12 +321,22 @@ def test_query_with_datasource_elasticsearch(self, m):
298321 "http://localhost/api/datasources/proxy/44/bazqux/_mapping" ,
299322 json = {},
300323 )
324+ m .post (
325+ "http://localhost/api/ds/query" ,
326+ json = DATAFRAME_RESPONSE_HEALTH_ELASTICSEARCH_VALID ,
327+ )
301328 datasource = ELASTICSEARCH_DATASOURCE .copy ()
302329 _ = self .grafana .datasource .smartquery (datasource , "url:///datasources/proxy/44/bazqux/_mapping" )
303- # TODO: No response payload yet.
330+ # TODO: Response payload not reflected and validated yet.
304331
305332 @requests_mock .Mocker ()
306333 def test_query_with_datasource_identifier (self , m ):
334+ # Mock the version inquiry request, because `smartquery` needs
335+ # it, as Prometheus responses differ between versions.
336+ m .get (
337+ "http://localhost/api/health" ,
338+ json = {"commit" : "14e988bd22" , "database" : "ok" , "version" : "9.0.1" },
339+ )
307340 m .get (
308341 "http://localhost/api/datasources/uid/h8KkCLt7z" ,
309342 json = PROMETHEUS_DATASOURCE ,
@@ -315,7 +348,14 @@ def test_query_with_datasource_identifier(self, m):
315348 response = self .grafana .datasource .smartquery (DatasourceIdentifier (uid = "h8KkCLt7z" ), "1+1" )
316349 self .assertEqual (response , DATAFRAME_RESPONSE_HEALTH_PROMETHEUS )
317350
318- def test_query_unknown_access_type_failure (self ):
351+ @requests_mock .Mocker ()
352+ def test_query_unknown_access_type_failure (self , m ):
353+ # Mock the version inquiry request, because `smartquery` needs
354+ # it, as Prometheus responses differ between versions.
355+ m .get (
356+ "http://localhost/api/health" ,
357+ json = {"commit" : "14e988bd22" , "database" : "ok" , "version" : "9.0.1" },
358+ )
319359 datasource = PROMETHEUS_DATASOURCE .copy ()
320360 datasource ["access" ] = "__UNKNOWN__"
321361 self .assertRaises (NotImplementedError , lambda : self .grafana .datasource .smartquery (datasource , expression = "1+1" ))
0 commit comments