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

     1  // Copyright 2020 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 sqle
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/dtestutils"
    26  )
    27  
    28  // Not an exhaustive test of views -- we rely on bats tests for end-to-end verification.
    29  func TestViews(t *testing.T) {
    30  	dEnv := dtestutils.CreateTestEnv()
    31  	defer dEnv.DoltDB.Close()
    32  
    33  	ctx := context.Background()
    34  	root, _ := dEnv.WorkingRoot(ctx)
    35  
    36  	var err error
    37  	root, err = ExecuteSql(dEnv, root, "create table test (a int primary key)")
    38  	require.NoError(t, err)
    39  
    40  	root, err = ExecuteSql(dEnv, root, "insert into test values (1), (2), (3)")
    41  	require.NoError(t, err)
    42  
    43  	root, err = ExecuteSql(dEnv, root, "create view plus1 as select a + 1 from test")
    44  	require.NoError(t, err)
    45  
    46  	expectedRows := []sql.Row{
    47  		{int64(2)},
    48  		{int64(3)},
    49  		{int64(4)},
    50  	}
    51  	rows, _, err := executeSelect(t, context.Background(), dEnv, root, "select * from plus1")
    52  	require.NoError(t, err)
    53  	assert.Equal(t, expectedRows, rows)
    54  
    55  	root, err = ExecuteSql(dEnv, root, "drop view plus1")
    56  	require.NoError(t, err)
    57  }