@ -13,14 +13,14 @@ import (
func TestAlertQuery ( t * testing . T ) {
func TestAlertQuery ( t * testing . T ) {
testCases := [ ] struct {
testCases := [ ] struct {
desc string
desc string
alertQuery AlertQuery
alertQuery AlertQuery
expectedIsExpression bool
expectedIsExpression bool
expectedDatasource string
expectedDatasource string
expectedDatasourceID int64
expectedDatasourceUID string
expectedMaxPoints int64
expectedMaxPoints int64
expectedIntervalMS int64
expectedIntervalMS int64
err error
err error
} {
} {
{
{
desc : "given an expression query" ,
desc : "given an expression query" ,
@ -32,11 +32,11 @@ func TestAlertQuery(t *testing.T) {
"extraParam" : "some text"
"extraParam" : "some text"
} ` ) ,
} ` ) ,
} ,
} ,
expectedIsExpression : true ,
expectedIsExpression : true ,
expectedDatasource : expr . DatasourceName ,
expectedDatasource : expr . DatasourceName ,
expectedDatasourceID : int64 ( expr . DatasourceID ) ,
expectedDatasourceU ID : expr . DatasourceU ID ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
} ,
} ,
{
{
desc : "given a query" ,
desc : "given a query" ,
@ -44,16 +44,16 @@ func TestAlertQuery(t *testing.T) {
RefID : "A" ,
RefID : "A" ,
Model : json . RawMessage ( ` {
Model : json . RawMessage ( ` {
"datasource" : "my datasource" ,
"datasource" : "my datasource" ,
"datasourceId" : 1 ,
"datasourceUid" : "000000001" ,
"queryType" : "metricQuery" ,
"queryType" : "metricQuery" ,
"extraParam" : "some text"
"extraParam" : "some text"
} ` ) ,
} ` ) ,
} ,
} ,
expectedIsExpression : false ,
expectedIsExpression : false ,
expectedDatasource : "my datasource" ,
expectedDatasource : "my datasource" ,
expectedDatasourceID : 1 ,
expectedDatasourceUID : "000000001" ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
} ,
} ,
{
{
desc : "given a query with valid maxDataPoints" ,
desc : "given a query with valid maxDataPoints" ,
@ -61,17 +61,17 @@ func TestAlertQuery(t *testing.T) {
RefID : "A" ,
RefID : "A" ,
Model : json . RawMessage ( ` {
Model : json . RawMessage ( ` {
"datasource" : "my datasource" ,
"datasource" : "my datasource" ,
"datasourceId" : 1 ,
"datasourceUid" : "000000001" ,
"queryType" : "metricQuery" ,
"queryType" : "metricQuery" ,
"maxDataPoints" : 200 ,
"maxDataPoints" : 200 ,
"extraParam" : "some text"
"extraParam" : "some text"
} ` ) ,
} ` ) ,
} ,
} ,
expectedIsExpression : false ,
expectedIsExpression : false ,
expectedDatasource : "my datasource" ,
expectedDatasource : "my datasource" ,
expectedDatasourceID : 1 ,
expectedDatasourceUID : "000000001" ,
expectedMaxPoints : 200 ,
expectedMaxPoints : 200 ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
} ,
} ,
{
{
desc : "given a query with invalid maxDataPoints" ,
desc : "given a query with invalid maxDataPoints" ,
@ -79,17 +79,17 @@ func TestAlertQuery(t *testing.T) {
RefID : "A" ,
RefID : "A" ,
Model : json . RawMessage ( ` {
Model : json . RawMessage ( ` {
"datasource" : "my datasource" ,
"datasource" : "my datasource" ,
"datasourceId" : 1 ,
"datasourceUid" : "000000001" ,
"queryType" : "metricQuery" ,
"queryType" : "metricQuery" ,
"maxDataPoints" : "invalid" ,
"maxDataPoints" : "invalid" ,
"extraParam" : "some text"
"extraParam" : "some text"
} ` ) ,
} ` ) ,
} ,
} ,
expectedIsExpression : false ,
expectedIsExpression : false ,
expectedDatasource : "my datasource" ,
expectedDatasource : "my datasource" ,
expectedDatasourceID : 1 ,
expectedDatasourceUID : "000000001" ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
} ,
} ,
{
{
desc : "given a query with zero maxDataPoints" ,
desc : "given a query with zero maxDataPoints" ,
@ -97,17 +97,17 @@ func TestAlertQuery(t *testing.T) {
RefID : "A" ,
RefID : "A" ,
Model : json . RawMessage ( ` {
Model : json . RawMessage ( ` {
"datasource" : "my datasource" ,
"datasource" : "my datasource" ,
"datasourceId" : 1 ,
"datasourceUid" : "000000001" ,
"queryType" : "metricQuery" ,
"queryType" : "metricQuery" ,
"maxDataPoints" : 0 ,
"maxDataPoints" : 0 ,
"extraParam" : "some text"
"extraParam" : "some text"
} ` ) ,
} ` ) ,
} ,
} ,
expectedIsExpression : false ,
expectedIsExpression : false ,
expectedDatasource : "my datasource" ,
expectedDatasource : "my datasource" ,
expectedDatasourceID : 1 ,
expectedDatasourceUID : "000000001" ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
} ,
} ,
{
{
desc : "given a query with valid intervalMs" ,
desc : "given a query with valid intervalMs" ,
@ -115,17 +115,17 @@ func TestAlertQuery(t *testing.T) {
RefID : "A" ,
RefID : "A" ,
Model : json . RawMessage ( ` {
Model : json . RawMessage ( ` {
"datasource" : "my datasource" ,
"datasource" : "my datasource" ,
"datasourceId" : 1 ,
"datasourceUid" : "000000001" ,
"queryType" : "metricQuery" ,
"queryType" : "metricQuery" ,
"intervalMs" : 2000 ,
"intervalMs" : 2000 ,
"extraParam" : "some text"
"extraParam" : "some text"
} ` ) ,
} ` ) ,
} ,
} ,
expectedIsExpression : false ,
expectedIsExpression : false ,
expectedDatasource : "my datasource" ,
expectedDatasource : "my datasource" ,
expectedDatasourceID : 1 ,
expectedDatasourceUID : "000000001" ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedIntervalMS : 2000 ,
expectedIntervalMS : 2000 ,
} ,
} ,
{
{
desc : "given a query with invalid intervalMs" ,
desc : "given a query with invalid intervalMs" ,
@ -133,17 +133,17 @@ func TestAlertQuery(t *testing.T) {
RefID : "A" ,
RefID : "A" ,
Model : json . RawMessage ( ` {
Model : json . RawMessage ( ` {
"datasource" : "my datasource" ,
"datasource" : "my datasource" ,
"datasourceId" : 1 ,
"datasourceUid" : "000000001" ,
"queryType" : "metricQuery" ,
"queryType" : "metricQuery" ,
"intervalMs" : "invalid" ,
"intervalMs" : "invalid" ,
"extraParam" : "some text"
"extraParam" : "some text"
} ` ) ,
} ` ) ,
} ,
} ,
expectedIsExpression : false ,
expectedIsExpression : false ,
expectedDatasource : "my datasource" ,
expectedDatasource : "my datasource" ,
expectedDatasourceID : 1 ,
expectedDatasourceUID : "000000001" ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
} ,
} ,
{
{
desc : "given a query with invalid intervalMs" ,
desc : "given a query with invalid intervalMs" ,
@ -151,17 +151,17 @@ func TestAlertQuery(t *testing.T) {
RefID : "A" ,
RefID : "A" ,
Model : json . RawMessage ( ` {
Model : json . RawMessage ( ` {
"datasource" : "my datasource" ,
"datasource" : "my datasource" ,
"datasourceId" : 1 ,
"datasourceUid" : "000000001" ,
"queryType" : "metricQuery" ,
"queryType" : "metricQuery" ,
"intervalMs" : 0 ,
"intervalMs" : 0 ,
"extraParam" : "some text"
"extraParam" : "some text"
} ` ) ,
} ` ) ,
} ,
} ,
expectedIsExpression : false ,
expectedIsExpression : false ,
expectedDatasource : "my datasource" ,
expectedDatasource : "my datasource" ,
expectedDatasourceID : 1 ,
expectedDatasourceUID : "000000001" ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedMaxPoints : int64 ( defaultMaxDataPoints ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
expectedIntervalMS : int64 ( defaultIntervalMS ) ,
} ,
} ,
}
}
@ -176,7 +176,7 @@ func TestAlertQuery(t *testing.T) {
t . Run ( "can set datasource for expression" , func ( t * testing . T ) {
t . Run ( "can set datasource for expression" , func ( t * testing . T ) {
err := tc . alertQuery . setDatasource ( )
err := tc . alertQuery . setDatasource ( )
require . NoError ( t , err )
require . NoError ( t , err )
require . Equal ( t , tc . expectedDatasourceID , tc . alertQuery . DatasourceID )
require . Equal ( t , tc . expectedDatasourceU ID , tc . alertQuery . DatasourceU ID )
} )
} )
t . Run ( "can set queryType for expression" , func ( t * testing . T ) {
t . Run ( "can set queryType for expression" , func ( t * testing . T ) {
@ -204,17 +204,18 @@ func TestAlertQuery(t *testing.T) {
err = json . Unmarshal ( blob , & model )
err = json . Unmarshal ( blob , & model )
require . NoError ( t , err )
require . NoError ( t , err )
fmt . Printf ( ">>>>>>> %+v %+v\n" , tc . alertQuery , model )
i , ok := model [ "datasource" ]
i , ok := model [ "datasource" ]
require . True ( t , ok )
require . True ( t , ok )
datasource , ok := i . ( string )
datasource , ok := i . ( string )
require . True ( t , ok )
require . True ( t , ok )
require . Equal ( t , tc . expectedDatasource , datasource )
require . Equal ( t , tc . expectedDatasource , datasource )
i , ok = model [ "datasourceI d" ]
i , ok = model [ "datasourceUi d" ]
require . True ( t , ok )
require . True ( t , ok )
datasourceID , ok := i . ( float64 )
datasourceU ID , ok := i . ( string )
require . True ( t , ok )
require . True ( t , ok )
require . Equal ( t , tc . expectedDatasourceID , int64 ( datasourceID ) )
require . Equal ( t , tc . expectedDatasourceU ID , datasourceU ID )
i , ok = model [ "maxDataPoints" ]
i , ok = model [ "maxDataPoints" ]
require . True ( t , ok )
require . True ( t , ok )