github.com/dolthub/go-mysql-server@v0.18.0/sql/viewregistry_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 sql
    16  
    17  import (
    18  	"strconv"
    19  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  var (
    25  	dbName   = "db"
    26  	viewName = "myview"
    27  	testView = NewView(viewName, nil, "", "")
    28  )
    29  
    30  func newRegistry(require *require.Assertions) *ViewRegistry {
    31  	registry := NewViewRegistry()
    32  
    33  	err := registry.Register(dbName, testView)
    34  	require.NoError(err)
    35  	require.Equal(1, len(registry.views))
    36  
    37  	return registry
    38  }
    39  
    40  // Tests the creation of an empty ViewRegistry with no views registered.
    41  func TestNewViewRegistry(t *testing.T) {
    42  	require := require.New(t)
    43  
    44  	registry := NewViewRegistry()
    45  	require.Equal(0, len(registry.views))
    46  }
    47  
    48  // Tests that registering a non-existing view succeeds.
    49  func TestRegisterNonExistingView(t *testing.T) {
    50  	require := require.New(t)
    51  
    52  	registry := newRegistry(require)
    53  
    54  	actualView, ok := registry.View(dbName, viewName)
    55  	require.True(ok)
    56  	require.Equal(testView, actualView)
    57  }
    58  
    59  // Tests that registering an existing view fails.
    60  func TestRegisterExistingVIew(t *testing.T) {
    61  	require := require.New(t)
    62  
    63  	registry := newRegistry(require)
    64  
    65  	err := registry.Register(dbName, testView)
    66  	require.Error(err)
    67  	require.True(ErrExistingView.Is(err))
    68  }
    69  
    70  // Tests that deleting an existing view succeeds.
    71  func TestDeleteExistingView(t *testing.T) {
    72  	require := require.New(t)
    73  
    74  	registry := newRegistry(require)
    75  
    76  	err := registry.Delete(dbName, viewName)
    77  	require.NoError(err)
    78  	require.Equal(0, len(registry.views))
    79  }
    80  
    81  // Tests that deleting a non-existing view fails.
    82  func TestDeleteNonExistingView(t *testing.T) {
    83  	require := require.New(t)
    84  
    85  	registry := NewViewRegistry()
    86  
    87  	err := registry.Delete("random", "randomer")
    88  	require.Error(err)
    89  	require.True(ErrViewDoesNotExist.Is(err))
    90  }
    91  
    92  // Tests that retrieving an existing view succeeds and that the view returned
    93  // is the correct one.
    94  func TestGetExistingView(t *testing.T) {
    95  	require := require.New(t)
    96  
    97  	registry := newRegistry(require)
    98  
    99  	actualView, ok := registry.View(dbName, viewName)
   100  	require.True(ok)
   101  	require.Equal(testView, actualView)
   102  }
   103  
   104  // Tests that retrieving a non-existing view fails.
   105  func TestGetNonExistingView(t *testing.T) {
   106  	require := require.New(t)
   107  
   108  	registry := NewViewRegistry()
   109  
   110  	actualView, ok := registry.View(dbName, viewName)
   111  	require.False(ok)
   112  	require.Nil(actualView)
   113  }
   114  
   115  // Tests that retrieving the views registered under a database succeeds,
   116  // returning the list of all the correct views.
   117  func TestViewsInDatabase(t *testing.T) {
   118  	require := require.New(t)
   119  
   120  	registry := NewViewRegistry()
   121  
   122  	databases := []struct {
   123  		name     string
   124  		numViews int
   125  	}{
   126  		{"db0", 0},
   127  		{"db1", 5},
   128  		{"db2", 10},
   129  	}
   130  
   131  	for _, db := range databases {
   132  		for i := 0; i < db.numViews; i++ {
   133  			view := NewView(viewName+strconv.Itoa(i), nil, "", "")
   134  			err := registry.Register(db.name, view)
   135  			require.NoError(err)
   136  		}
   137  
   138  		views := registry.ViewsInDatabase(db.name)
   139  		require.Equal(db.numViews, len(views))
   140  	}
   141  }
   142  
   143  var viewKeys = []ViewKey{
   144  	{
   145  		"db1",
   146  		"view1",
   147  	},
   148  	{
   149  		"db1",
   150  		"view2",
   151  	},
   152  	{
   153  		"db2",
   154  		"view1",
   155  	},
   156  }
   157  
   158  func registerKeys(registry *ViewRegistry, require *require.Assertions) {
   159  	for _, key := range viewKeys {
   160  		err := registry.Register(key.dbName, NewView(key.viewName, nil, "", ""))
   161  		require.NoError(err)
   162  	}
   163  	require.Equal(len(viewKeys), len(registry.views))
   164  }
   165  
   166  func TestExistsOnExistingView(t *testing.T) {
   167  	require := require.New(t)
   168  
   169  	registry := newRegistry(require)
   170  
   171  	require.True(registry.Exists(dbName, viewName))
   172  }
   173  
   174  func TestExistsOnNonExistingView(t *testing.T) {
   175  	require := require.New(t)
   176  
   177  	registry := newRegistry(require)
   178  
   179  	require.False(registry.Exists("non", "existing"))
   180  }