github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/table/composite_table_reader_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 table
    16  
    17  import (
    18  	"context"
    19  	"io"
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"github.com/stretchr/testify/require"
    24  
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/row"
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    27  	"github.com/dolthub/dolt/go/store/types"
    28  )
    29  
    30  func TestCompositeTableReader(t *testing.T) {
    31  	const (
    32  		numReaders        = 7
    33  		numItemsPerReader = 7
    34  	)
    35  
    36  	ctx := context.Background()
    37  
    38  	coll := schema.NewColCollection(
    39  		schema.NewColumn("id", 0, types.UintKind, true, schema.NotNullConstraint{}),
    40  		schema.NewColumn("val", 1, types.IntKind, false),
    41  	)
    42  	sch, err := schema.SchemaFromCols(coll)
    43  	require.NoError(t, err)
    44  
    45  	var readers []TableReadCloser
    46  	var expectedKeys []uint64
    47  	var expectedVals []int64
    48  	for i := 0; i < numReaders; i++ {
    49  		var rows []row.Row
    50  		for j := 0; j < numItemsPerReader; j++ {
    51  			idx := j + (i * numItemsPerReader)
    52  			expectedKeys = append(expectedKeys, uint64(idx))
    53  			expectedVals = append(expectedVals, int64(idx))
    54  			r, err := row.New(types.Format_Default, sch, row.TaggedValues{
    55  				0: types.Uint(uint64(idx)),
    56  				1: types.Int(idx),
    57  			})
    58  			require.NoError(t, err)
    59  			rows = append(rows, r)
    60  		}
    61  		imt := NewInMemTableWithData(sch, rows)
    62  		rd := NewInMemTableReader(imt)
    63  		readers = append(readers, rd)
    64  	}
    65  
    66  	compositeRd, err := NewCompositeTableReader(readers)
    67  	require.NoError(t, err)
    68  
    69  	var keys []uint64
    70  	var vals []int64
    71  	for {
    72  		r, err := compositeRd.ReadRow(ctx)
    73  
    74  		if err == io.EOF {
    75  			break
    76  		}
    77  
    78  		assert.NoError(t, err)
    79  		val0, ok := r.GetColVal(0)
    80  		assert.True(t, ok)
    81  		val1, ok := r.GetColVal(1)
    82  		assert.True(t, ok)
    83  
    84  		keys = append(keys, uint64(val0.(types.Uint)))
    85  		vals = append(vals, int64(val1.(types.Int)))
    86  	}
    87  
    88  	assert.Equal(t, expectedKeys, keys)
    89  	assert.Equal(t, expectedVals, vals)
    90  
    91  	err = compositeRd.Close(ctx)
    92  	assert.NoError(t, err)
    93  }