github.com/dolthub/go-mysql-server@v0.18.0/sql/rowexec/tablealias_test.go (about)

     1  // Copyright 2020-2021 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 rowexec
    16  
    17  import (
    18  	"io"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  
    23  	"github.com/dolthub/go-mysql-server/memory"
    24  	"github.com/dolthub/go-mysql-server/sql"
    25  	"github.com/dolthub/go-mysql-server/sql/plan"
    26  	"github.com/dolthub/go-mysql-server/sql/types"
    27  )
    28  
    29  func TestTableAlias(t *testing.T) {
    30  	require := require.New(t)
    31  
    32  	db := memory.NewDatabase("foo")
    33  	pro := memory.NewDBProvider(db)
    34  	ctx := newContext(pro)
    35  
    36  	table := memory.NewTable(db, "bar", sql.NewPrimaryKeySchema(sql.Schema{
    37  		{Name: "a", Type: types.Text, Nullable: true},
    38  		{Name: "b", Type: types.Text, Nullable: true},
    39  	}), nil)
    40  	alias := plan.NewTableAlias("foo", plan.NewResolvedTable(table, nil, nil))
    41  
    42  	var rows = []sql.Row{
    43  		sql.NewRow("1", "2"),
    44  		sql.NewRow("3", "4"),
    45  		sql.NewRow("5", "6"),
    46  	}
    47  
    48  	for _, r := range rows {
    49  		require.NoError(table.Insert(ctx, r))
    50  	}
    51  
    52  	require.Equal(sql.Schema{
    53  		{Name: "a", Source: "foo", Type: types.Text, Nullable: true},
    54  		{Name: "b", Source: "foo", Type: types.Text, Nullable: true},
    55  	}, alias.Schema())
    56  	iter, err := DefaultBuilder.Build(ctx, alias, nil)
    57  	require.NoError(err)
    58  
    59  	var i int
    60  	for {
    61  		row, err := iter.Next(ctx)
    62  		if err == io.EOF {
    63  			break
    64  		}
    65  
    66  		require.NoError(err)
    67  		require.Equal(rows[i], row)
    68  		i++
    69  	}
    70  
    71  	require.Equal(len(rows), i)
    72  }