github.com/matrixorigin/matrixone@v0.7.0/pkg/frontend/show_account_test.go (about)

     1  // Copyright 2021 Matrix Origin
     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 frontend
    16  
    17  import (
    18  	"context"
    19  	"github.com/golang/mock/gomock"
    20  	"github.com/matrixorigin/matrixone/pkg/defines"
    21  	"github.com/matrixorigin/matrixone/pkg/sql/parsers"
    22  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/dialect"
    23  	"github.com/matrixorigin/matrixone/pkg/sql/parsers/tree"
    24  	"github.com/prashantv/gostub"
    25  	"github.com/stretchr/testify/assert"
    26  	"testing"
    27  )
    28  
    29  func Test_getSqlForAccountInfo(t *testing.T) {
    30  	type arg struct {
    31  		s    string
    32  		want string
    33  	}
    34  	args := []arg{
    35  		{
    36  			s:    "show accounts;",
    37  			want: "select account_id as `account_id`, account_name as `account_name`, created_time as `created`, status as `status`, suspended_time as `suspended_time`, comments as `comment` from mo_catalog.mo_account ;",
    38  		},
    39  		{
    40  			s:    "show accounts like '%abc';",
    41  			want: "select account_id as `account_id`, account_name as `account_name`, created_time as `created`, status as `status`, suspended_time as `suspended_time`, comments as `comment` from mo_catalog.mo_account where account_name like '%abc';",
    42  		},
    43  	}
    44  
    45  	for _, a := range args {
    46  		one, err := parsers.ParseOne(context.Background(), dialect.MYSQL, a.s)
    47  		assert.NoError(t, err)
    48  		sa1 := one.(*tree.ShowAccounts)
    49  		r1 := getSqlForAllAccountInfo(sa1.Like)
    50  		assert.Equal(t, a.want, r1)
    51  	}
    52  }
    53  
    54  func getColumnDef(name string, typ defines.MysqlType) Column {
    55  	return &MysqlColumn{
    56  		ColumnImpl: ColumnImpl{
    57  			name:       name,
    58  			columnType: typ,
    59  		},
    60  	}
    61  }
    62  
    63  func newAccountInfo() *MysqlResultSet {
    64  	rsFromMoAccout := &MysqlResultSet{}
    65  	rsFromMoAccout.AddColumn(getColumnDef("account_id", defines.MYSQL_TYPE_LONG))
    66  	rsFromMoAccout.AddColumn(getColumnDef("account_name", defines.MYSQL_TYPE_VARCHAR))
    67  	rsFromMoAccout.AddColumn(getColumnDef("created", defines.MYSQL_TYPE_TIMESTAMP))
    68  	rsFromMoAccout.AddColumn(getColumnDef("status", defines.MYSQL_TYPE_VARCHAR))
    69  	rsFromMoAccout.AddColumn(getColumnDef("suspended_time", defines.MYSQL_TYPE_TIMESTAMP))
    70  	rsFromMoAccout.AddColumn(getColumnDef("comment", defines.MYSQL_TYPE_VARCHAR))
    71  	rsFromMoAccout.AddRow(make([]interface{}, rsFromMoAccout.GetColumnCount()))
    72  	return rsFromMoAccout
    73  }
    74  
    75  func newTableStatsResult() *MysqlResultSet {
    76  	rs1 := &MysqlResultSet{}
    77  	rs1.AddColumn(getColumnDef("admin_name", defines.MYSQL_TYPE_VARCHAR))
    78  	rs1.AddColumn(getColumnDef("db_count", defines.MYSQL_TYPE_LONG))
    79  	rs1.AddColumn(getColumnDef("table_count", defines.MYSQL_TYPE_LONG))
    80  	rs1.AddColumn(getColumnDef("row_count", defines.MYSQL_TYPE_LONG))
    81  	rs1.AddColumn(getColumnDef("size", defines.MYSQL_TYPE_DECIMAL))
    82  	rs1.AddRow(make([]interface{}, rs1.GetColumnCount()))
    83  	return rs1
    84  }
    85  
    86  func Test_mergeResult(t *testing.T) {
    87  	rsFromMoAccount := newAccountInfo()
    88  	rs1 := newTableStatsResult()
    89  	ans1 := &MysqlResultSet{}
    90  	err := mergeOutputResult(context.Background(), ans1, rsFromMoAccount, []*MysqlResultSet{rs1})
    91  	assert.NoError(t, err)
    92  	assert.Equal(t, ans1.GetColumnCount(), uint64(finalColumnCount))
    93  }
    94  
    95  func Test_tableStats(t *testing.T) {
    96  	ctrl := gomock.NewController(t)
    97  	defer ctrl.Finish()
    98  	ctx := context.Background()
    99  	sql2result := make(map[string]ExecResult)
   100  
   101  	sql := getSqlForTableStats(10)
   102  	sql2result[sql] = newTableStatsResult()
   103  
   104  	bh := newBh(ctrl, sql2result)
   105  
   106  	rs, err := getTableStats(ctx, bh, 10)
   107  	assert.NoError(t, err)
   108  	assert.NotNil(t, rs)
   109  }
   110  
   111  func TestGetAccountInfo(t *testing.T) {
   112  	ctrl := gomock.NewController(t)
   113  	defer ctrl.Finish()
   114  	ctx := context.Background()
   115  	sql2result := make(map[string]ExecResult)
   116  	sql := getSqlForAccountInfo(10)
   117  	info := newAccountInfo()
   118  	info.Data[0][idxOfAccountId] = 10
   119  	sql2result[sql] = info
   120  
   121  	bh := newBh(ctrl, sql2result)
   122  
   123  	rs, ids, err := getAccountInfo(ctx, bh, sql, true)
   124  	assert.NoError(t, err)
   125  	assert.NotNil(t, rs)
   126  	assert.Equal(t, ids[0], uint64(10))
   127  }
   128  
   129  func TestDoShowAccounts(t *testing.T) {
   130  	ctrl := gomock.NewController(t)
   131  	defer ctrl.Finish()
   132  	ctx := context.Background()
   133  	ses := newTestSession(t, ctrl)
   134  	defer ses.Dispose()
   135  
   136  	tenant := &TenantInfo{
   137  		Tenant:   sysAccountName,
   138  		TenantID: sysAccountID,
   139  	}
   140  	ses.SetTenantInfo(tenant)
   141  
   142  	sa := &tree.ShowAccounts{}
   143  	bh := &backgroundExecTest{}
   144  	bh.init()
   145  
   146  	bhStub := gostub.StubFunc(&NewBackgroundHandler, bh)
   147  	defer bhStub.Reset()
   148  
   149  	bh.sql2result["begin;"] = nil
   150  	bh.sql2result["commit;"] = nil
   151  	bh.sql2result["rollback;"] = nil
   152  
   153  	sql := getSqlForAllAccountInfo(nil)
   154  	info := newAccountInfo()
   155  	info.Data[0][idxOfAccountId] = 10
   156  	bh.sql2result[sql] = info
   157  
   158  	sql = getSqlForTableStats(10)
   159  	bh.sql2result[sql] = newTableStatsResult()
   160  
   161  	err := doShowAccounts(ctx, ses, sa)
   162  	assert.NoError(t, err)
   163  	rs := ses.GetMysqlResultSet()
   164  	assert.Equal(t, rs.GetColumnCount(), uint64(finalColumnCount))
   165  }