SQL Expressions: Simplify test code (#101718)

Simplify, by retaining quotes when escaping query

We also don't need to convert to string when passing to fmt.Sprintf
a slice of Bytes is accepted by Sprintf instead.
pull/100379/head^2
Sam Jewell 3 months ago committed by GitHub
parent 5996e102af
commit 0eafd0641f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 13
      pkg/expr/service_sql_test.go

@ -26,7 +26,7 @@ func TestSQLService(t *testing.T) {
}
newABSQLQueries := func(q string) []Query {
q, err := jsonEscape(q)
escaped, err := json.Marshal(q)
require.NoError(t, err)
return []Query{
{
@ -45,7 +45,7 @@ func TestSQLService(t *testing.T) {
{
RefID: "B",
DataSource: dataSourceModel(),
JSON: json.RawMessage(fmt.Sprintf(`{ "datasource": { "uid": "__expr__", "type": "__expr__"}, "type": "sql", "expression": "%s" }`, q)),
JSON: json.RawMessage(fmt.Sprintf(`{ "datasource": { "uid": "__expr__", "type": "__expr__"}, "type": "sql", "expression": %s }`, escaped)),
TimeRange: AbsoluteTimeRange{
From: time.Time{},
To: time.Time{},
@ -110,12 +110,3 @@ func TestSQLService(t *testing.T) {
require.ErrorContains(t, rsp.Responses["B"].Error, "limit expression expected to be numeric")
})
}
func jsonEscape(input string) (string, error) {
escaped, err := json.Marshal(input)
if err != nil {
return "", err
}
// json.Marshal returns the escaped string with quotes, so we need to trim them
return string(escaped[1 : len(escaped)-1]), nil
}

Loading…
Cancel
Save