github.com/matrixorigin/matrixone@v1.2.0/pkg/util/trace/impl/motrace/schema_test.go (about)

     1  // Copyright 2022 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 motrace
    16  
    17  import (
    18  	"context"
    19  	"strings"
    20  	"sync"
    21  	"testing"
    22  
    23  	ie "github.com/matrixorigin/matrixone/pkg/util/internalExecutor"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  var _ ie.InternalExecutor = &dummySqlExecutor{}
    28  
    29  type dummySqlExecutor struct {
    30  	opts ie.SessionOverrideOptions
    31  	ch   chan<- string
    32  }
    33  
    34  func (e *dummySqlExecutor) ApplySessionOverride(opts ie.SessionOverrideOptions) {}
    35  func (e *dummySqlExecutor) Query(ctx context.Context, s string, options ie.SessionOverrideOptions) ie.InternalExecResult {
    36  	return nil
    37  }
    38  func (e *dummySqlExecutor) Exec(ctx context.Context, sql string, opts ie.SessionOverrideOptions) error {
    39  	e.ch <- sql
    40  	return nil
    41  }
    42  
    43  // copy from /Users/jacksonxie/go/src/github.com/matrixorigin/matrixone/pkg/util/metric/metric_collector_test.go
    44  func newDummyExecutorFactory(sqlch chan string) func() ie.InternalExecutor {
    45  	return func() ie.InternalExecutor {
    46  		return &dummySqlExecutor{
    47  			opts: ie.NewOptsBuilder().Finish(),
    48  			ch:   sqlch,
    49  		}
    50  	}
    51  }
    52  
    53  func TestInitSchemaByInnerExecutor(t *testing.T) {
    54  	type args struct {
    55  		ctx context.Context
    56  		ch  chan string
    57  	}
    58  	tests := []struct {
    59  		name string
    60  		args args
    61  	}{
    62  		{
    63  			name: "dummy",
    64  			args: args{
    65  				ctx: context.Background(),
    66  				ch:  make(chan string, 10),
    67  			},
    68  		},
    69  		{
    70  			name: "dummyS3",
    71  			args: args{
    72  				ctx: context.Background(),
    73  				ch:  make(chan string, 10),
    74  			},
    75  		},
    76  	}
    77  
    78  	// (1 + 4) * n + 1
    79  	// 1: create database ...
    80  	// 4: create EXTERNAL table
    81  	for _, tt := range tests {
    82  		t.Run(tt.name, func(t *testing.T) {
    83  			var wg sync.WaitGroup
    84  			wg.Add(1 + len(tables) + len(views))
    85  			err := InitSchemaByInnerExecutor(tt.args.ctx, newDummyExecutorFactory(tt.args.ch))
    86  			require.Equal(t, nil, err)
    87  			go func() {
    88  				wg.Wait()
    89  				wg.Add(1)
    90  				close(tt.args.ch)
    91  			}()
    92  		loop:
    93  			for {
    94  				sql, ok := <-tt.args.ch
    95  				wg.Done()
    96  				if ok {
    97  					t.Logf("exec sql: %s", sql)
    98  					if sql == "create database if not exists system" {
    99  						continue
   100  					}
   101  					idx := strings.Index(sql, "CREATE EXTERNAL TABLE")
   102  					viewIdx := strings.Index(sql, "CREATE VIEW")
   103  					require.Equal(t, -1, idx|viewIdx)
   104  				} else {
   105  					t.Log("exec sql Done.")
   106  					wg.Wait()
   107  					break loop
   108  				}
   109  			}
   110  		})
   111  	}
   112  }
   113  
   114  func TestGetSchemaForAccount(t *testing.T) {
   115  	type args struct {
   116  		account string
   117  	}
   118  	tests := []struct {
   119  		name     string
   120  		args     args
   121  		wantPath string
   122  	}{
   123  		{
   124  			name: "with_account_user1",
   125  			args: args{
   126  				account: "user1",
   127  			},
   128  			wantPath: "/user1/*/*/*/*/statement_info/*",
   129  		},
   130  	}
   131  	ctx := context.Background()
   132  	for _, tt := range tests {
   133  		t.Run(tt.name, func(t *testing.T) {
   134  			schemas := GetSchemaForAccount(ctx, tt.args.account)
   135  			//found := false
   136  			//for _, sche := range schemas {
   137  			//	t.Logf("schma: %s", sche)
   138  			//	if strings.Contains(sche, tt.wantPath) {
   139  			//		found = true
   140  			//	}
   141  			//}
   142  			require.Equal(t, 1, len(schemas))
   143  			//require.Equal(t, true, found)
   144  			//found = false
   145  			//if strings.Contains(SingleStatementTable.ToCreateSql(ctx, true), "/*/*/*/*/*/statement_info/*") {
   146  			//	found = true
   147  			//}
   148  			//require.Equal(t, true, found)
   149  		})
   150  	}
   151  }