From 0eafd0641f5ba773635a1d868b06fe31a849e8a8 Mon Sep 17 00:00:00 2001 From: Sam Jewell <2903904+samjewell@users.noreply.github.com> Date: Thu, 6 Mar 2025 17:02:31 +0000 Subject: [PATCH] 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. --- pkg/expr/service_sql_test.go | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/pkg/expr/service_sql_test.go b/pkg/expr/service_sql_test.go index 2ad5df4baa8..0a1d991da68 100644 --- a/pkg/expr/service_sql_test.go +++ b/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 -}