github.com/matrixorigin/matrixone@v1.2.0/pkg/vm/engine/tae/db/test/compatibility/registry.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 compatibility
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"testing"
    21  
    22  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/catalog"
    23  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/containers"
    24  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/db/testutil"
    25  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/options"
    26  	"github.com/matrixorigin/matrixone/pkg/vm/engine/tae/testutils/config"
    27  	"github.com/stretchr/testify/assert"
    28  )
    29  
    30  var (
    31  	version = 6
    32  )
    33  
    34  type optType int
    35  
    36  const (
    37  	quickOpt optType = iota
    38  	longOpt
    39  )
    40  
    41  func initPrepareTest(pc PrepareCase, opts *options.Options, t *testing.T) *testutil.TestEngine {
    42  	dir, err := InitPrepareDirByName(pc.name)
    43  	assert.NoError(t, err)
    44  	ctx := context.Background()
    45  	tae := testutil.NewTestEngineWithDir(ctx, dir, t, opts)
    46  	tae.BindSchema(pc.GetSchema(t))
    47  	return tae
    48  }
    49  func initTestEngine(tc TestCase, t *testing.T) *testutil.TestEngine {
    50  	pc := GetPrepareCase(tc.dependsOn)
    51  	opts := pc.GetOptions(t)
    52  	dir, err := InitTestCaseExecuteDir(tc.name)
    53  	assert.NoError(t, err)
    54  	err = CopyDir(GetPrepareDirByName(pc.name), dir)
    55  	assert.NoError(t, err)
    56  	ctx := context.Background()
    57  	tae := testutil.NewTestEngineWithDir(ctx, dir, t, opts)
    58  	tae.BindSchema(pc.GetSchema(t))
    59  	return tae
    60  }
    61  
    62  type schemaCfg struct {
    63  	blockMaxRows    uint32
    64  	ObjectMaxBlocks uint16
    65  	colCnt          int
    66  	pkIdx           int
    67  }
    68  
    69  type PrepareCase struct {
    70  	name      string
    71  	desc      string
    72  	schemaCfg schemaCfg
    73  	batchSize int
    74  	optType   optType
    75  	prepareFn func(tc PrepareCase, t *testing.T)
    76  }
    77  
    78  func (pc PrepareCase) GetOptions(t *testing.T) *options.Options {
    79  	switch pc.optType {
    80  	case quickOpt:
    81  		return config.WithQuickScanAndCKPOpts(nil)
    82  	case longOpt:
    83  		return config.WithLongScanAndCKPOpts(nil)
    84  	default:
    85  		panic("PrepareCase.GetOptions: unknown optType")
    86  	}
    87  }
    88  
    89  func (pc PrepareCase) GetEngine(t *testing.T) *testutil.TestEngine {
    90  	opts := pc.GetOptions(t)
    91  	e := initPrepareTest(pc, opts, t)
    92  	return e
    93  }
    94  
    95  func (pc PrepareCase) GetSchema(t *testing.T) *catalog.Schema {
    96  	schema := catalog.MockSchemaAll(pc.schemaCfg.colCnt, pc.schemaCfg.pkIdx)
    97  	schema.BlockMaxRows = pc.schemaCfg.blockMaxRows
    98  	schema.ObjectMaxBlocks = pc.schemaCfg.ObjectMaxBlocks
    99  	ver, err := ReadPrepareVersion()
   100  	if err != nil {
   101  		t.Error(err)
   102  	}
   103  	schema.Name = fmt.Sprintf("test_%d", ver)
   104  	return schema
   105  }
   106  
   107  func (pc PrepareCase) GetBatch(t *testing.T) *containers.Batch {
   108  	schema := pc.GetSchema(t)
   109  	bat := catalog.MockBatch(schema, pc.batchSize)
   110  	return bat
   111  }
   112  
   113  type TestCase struct {
   114  	name      string
   115  	desc      string
   116  	dependsOn string
   117  	testFn    func(tc TestCase, t *testing.T)
   118  }
   119  
   120  func (tc TestCase) GetEngine(t *testing.T) *testutil.TestEngine {
   121  	tae := initTestEngine(tc, t)
   122  	return tae
   123  }
   124  
   125  var PrepareCases map[string]PrepareCase
   126  var TestCases map[string]TestCase
   127  
   128  func TestCaseRegister(testCase TestCase) {
   129  	if TestCases == nil {
   130  		TestCases = make(map[string]TestCase)
   131  	}
   132  	if _, ok := TestCases[testCase.name]; ok {
   133  		panic("TestCaseRegister: duplicate test case name")
   134  	}
   135  	TestCases[testCase.name] = testCase
   136  }
   137  
   138  func PrepareCaseRegister(prepareCase PrepareCase) {
   139  	if PrepareCases == nil {
   140  		PrepareCases = make(map[string]PrepareCase)
   141  	}
   142  	if _, ok := PrepareCases[prepareCase.name]; ok {
   143  		panic("PrepareCaseRegister: duplicate prepare case type")
   144  	}
   145  	PrepareCases[prepareCase.name] = prepareCase
   146  }
   147  
   148  func GetPrepareCase(name string) PrepareCase {
   149  	if prepareCase, ok := PrepareCases[name]; ok {
   150  		return prepareCase
   151  	}
   152  	panic("GetPrepareCase: prepare case not found")
   153  }
   154  
   155  func MakePrepareCase(
   156  	prepareFn func(tc PrepareCase, t *testing.T),
   157  	name string,
   158  	desc string,
   159  	schemaCfg schemaCfg,
   160  	batchSize int,
   161  	optType optType,
   162  ) PrepareCase {
   163  	return PrepareCase{
   164  		name:      name,
   165  		desc:      desc,
   166  		schemaCfg: schemaCfg,
   167  		batchSize: batchSize,
   168  		optType:   optType,
   169  		prepareFn: prepareFn,
   170  	}
   171  }
   172  
   173  func MakeTestCase(
   174  	testFn func(tc TestCase, t *testing.T),
   175  	dependsOn string,
   176  	name string,
   177  	desc string,
   178  ) TestCase {
   179  	return TestCase{
   180  		name:      name,
   181  		desc:      desc,
   182  		dependsOn: dependsOn,
   183  		testFn:    testFn,
   184  	}
   185  }