github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/sqle/enginetest/testgen_test.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package enginetest
    16  
    17  import (
    18  	"bufio"
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"strings"
    23  	"testing"
    24  
    25  	"github.com/dolthub/go-mysql-server/sql"
    26  	"github.com/dolthub/go-mysql-server/sql/planbuilder"
    27  
    28  	"github.com/dolthub/go-mysql-server/enginetest"
    29  	"github.com/dolthub/go-mysql-server/enginetest/queries"
    30  	"github.com/dolthub/go-mysql-server/enginetest/scriptgen/setup"
    31  	"github.com/stretchr/testify/require"
    32  )
    33  
    34  func TestGenNewFormatQueryPlans(t *testing.T) {
    35  	// must run with env var: DOLT_DEFAULT_BIN_FORMAT="__DOLT__"
    36  	t.Skip()
    37  	harness := newDoltHarness(t).WithParallelism(1)
    38  	harness.Setup(setup.SimpleSetup...)
    39  	engine, err := harness.NewEngine(t)
    40  	require.NoError(t, err)
    41  
    42  	outputPath := filepath.Join(t.TempDir(), "queryPlans.txt")
    43  	f, err := os.Create(outputPath)
    44  	require.NoError(t, err)
    45  
    46  	w := bufio.NewWriter(f)
    47  	_, _ = w.WriteString("var NewFormatQueryPlanTests = []queries.QueryPlanTest{\n")
    48  	for _, tt := range queries.PlanTests {
    49  		_, _ = w.WriteString("\t{\n")
    50  		ctx := enginetest.NewContextWithEngine(harness, engine)
    51  		binder := planbuilder.New(ctx, engine.EngineAnalyzer().Catalog, sql.NewMysqlParser())
    52  		parsed, _, _, err := binder.Parse(tt.Query, false)
    53  		require.NoError(t, err)
    54  
    55  		node, err := engine.EngineAnalyzer().Analyze(ctx, parsed, nil)
    56  		require.NoError(t, err)
    57  		planString := enginetest.ExtractQueryNode(node).String()
    58  
    59  		if strings.Contains(tt.Query, "`") {
    60  			_, _ = w.WriteString(fmt.Sprintf(`Query: "%s",`, tt.Query))
    61  		} else {
    62  			_, _ = w.WriteString(fmt.Sprintf("Query: `%s`,", tt.Query))
    63  		}
    64  		_, _ = w.WriteString("\n")
    65  
    66  		_, _ = w.WriteString(`ExpectedPlan: `)
    67  		for i, line := range strings.Split(planString, "\n") {
    68  			if i > 0 {
    69  				_, _ = w.WriteString(" + \n")
    70  			}
    71  			if len(line) > 0 {
    72  				_, _ = w.WriteString(fmt.Sprintf(`"%s\n"`, strings.ReplaceAll(line, `"`, `\"`)))
    73  			} else {
    74  				// final line with comma
    75  				_, _ = w.WriteString("\"\",\n")
    76  			}
    77  		}
    78  		_, _ = w.WriteString("\t},\n")
    79  	}
    80  	_, _ = w.WriteString("}")
    81  
    82  	_ = w.Flush()
    83  
    84  	t.Logf("Query plans in %s", outputPath)
    85  }