github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/table/inmem_table_test.go (about)

     1  // Copyright 2019 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/dolthub/dolt/go/libraries/doltcore/row"
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo"
    25  	"github.com/dolthub/dolt/go/store/types"
    26  )
    27  
    28  const (
    29  	nameTag uint64 = iota
    30  	ageTag
    31  	titleTag
    32  	greatTag
    33  )
    34  
    35  var fields = schema.NewColCollection(
    36  	schema.Column{Name: "name", Tag: nameTag, Kind: types.StringKind, IsPartOfPK: true, TypeInfo: typeinfo.StringDefaultType, Constraints: nil},
    37  	schema.Column{Name: "age", Tag: ageTag, Kind: types.UintKind, IsPartOfPK: true, TypeInfo: typeinfo.Uint64Type, Constraints: nil},
    38  	schema.Column{Name: "title", Tag: titleTag, Kind: types.StringKind, IsPartOfPK: true, TypeInfo: typeinfo.StringDefaultType, Constraints: nil},
    39  	schema.Column{Name: "is_great", Tag: greatTag, Kind: types.BoolKind, IsPartOfPK: true, TypeInfo: typeinfo.BoolType, Constraints: nil},
    40  )
    41  
    42  var rowSch = schema.MustSchemaFromCols(fields)
    43  
    44  func mustRow(r row.Row, err error) row.Row {
    45  	if err != nil {
    46  		panic(err)
    47  	}
    48  
    49  	return r
    50  }
    51  
    52  // These are in noms-key-sorted order, since InMemoryTable.AppendRow sorts its rows. This should probably be done
    53  // programatically instead of hard-coded.
    54  var rows = []row.Row{
    55  	mustRow(row.New(types.Format_Default, rowSch, row.TaggedValues{
    56  		nameTag:  types.String("Bill Billerson"),
    57  		ageTag:   types.Uint(32),
    58  		titleTag: types.String("Senior Dufus"),
    59  		greatTag: types.Bool(true),
    60  	})),
    61  	mustRow(row.New(types.Format_Default, rowSch, row.TaggedValues{
    62  		nameTag:  types.String("John Johnson"),
    63  		ageTag:   types.Uint(21),
    64  		titleTag: types.String("Intern Dufus"),
    65  		greatTag: types.Bool(true),
    66  	})),
    67  	mustRow(row.New(types.Format_Default, rowSch, row.TaggedValues{
    68  		nameTag:  types.String("Rob Robertson"),
    69  		ageTag:   types.Uint(25),
    70  		titleTag: types.String("Dufus"),
    71  		greatTag: types.Bool(false),
    72  	})),
    73  }
    74  
    75  func TestInMemTable(t *testing.T) {
    76  	vrw := types.NewMemoryValueStore()
    77  	ctx := context.Background()
    78  	imt := NewInMemTable(rowSch)
    79  
    80  	func() {
    81  		for _, r := range rows {
    82  			err := imt.AppendRow(ctx, vrw, r)
    83  
    84  			if err != nil {
    85  				t.Fatal("Failed to write row")
    86  			}
    87  		}
    88  	}()
    89  
    90  	func() {
    91  		var r ReadCloser
    92  		r = NewInMemTableReader(imt)
    93  		defer r.Close(context.Background())
    94  
    95  		for _, expectedRow := range rows {
    96  			actualRow, err := r.ReadRow(context.Background())
    97  
    98  			if err != nil {
    99  				t.Error("Unexpected read error")
   100  			} else if !row.AreEqual(expectedRow, actualRow, rowSch) {
   101  				t.Error("Unexpected row value")
   102  			}
   103  		}
   104  
   105  		_, err := r.ReadRow(context.Background())
   106  
   107  		if err != io.EOF {
   108  			t.Error("Should have reached the end.")
   109  		}
   110  	}()
   111  }