vitess.io/vitess@v0.16.2/go/vt/vtctl/workflow/vexec/testutil/query.go (about)

     1  /*
     2  Copyright 2021 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testutil
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"vitess.io/vitess/go/vt/sqlparser"
    25  )
    26  
    27  // ParsedQueryFromString is a test helper that returns a *sqlparser.ParsedQuery
    28  // from a plain string. It marks the test as a failure if the query cannot be
    29  // parsed.
    30  func ParsedQueryFromString(t *testing.T, query string) *sqlparser.ParsedQuery {
    31  	t.Helper()
    32  
    33  	buf := sqlparser.NewTrackedBuffer(nil)
    34  	buf.Myprintf("%v", StatementFromString(t, query))
    35  
    36  	return buf.ParsedQuery()
    37  }
    38  
    39  // StatementFromString is a test helper that returns a sqlparser.Statement from
    40  // a plain string. It marks the test as a failure if the query cannot be parsed.
    41  func StatementFromString(t *testing.T, query string) sqlparser.Statement {
    42  	t.Helper()
    43  
    44  	stmt, err := sqlparser.Parse(query)
    45  	require.NoError(t, err, "could not parse query %v", query)
    46  
    47  	return stmt
    48  }