github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/dtestutils/data.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 dtestutils
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"github.com/google/uuid"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/row"
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    26  	"github.com/dolthub/dolt/go/libraries/doltcore/table"
    27  	"github.com/dolthub/dolt/go/libraries/doltcore/table/untyped"
    28  	"github.com/dolthub/dolt/go/store/types"
    29  )
    30  
    31  var UUIDS = []uuid.UUID{
    32  	uuid.Must(uuid.Parse("00000000-0000-0000-0000-000000000000")),
    33  	uuid.Must(uuid.Parse("00000000-0000-0000-0000-000000000001")),
    34  	uuid.Must(uuid.Parse("00000000-0000-0000-0000-000000000002"))}
    35  var Names = []string{"Bill Billerson", "John Johnson", "Rob Robertson"}
    36  var Ages = []uint64{32, 25, 21}
    37  var Titles = []string{"Senior Dufus", "Dufus", ""}
    38  var MaritalStatus = []bool{true, false, false}
    39  
    40  const (
    41  	IdTag uint64 = iota
    42  	NameTag
    43  	AgeTag
    44  	IsMarriedTag
    45  	TitleTag
    46  	NextTag // leave last
    47  )
    48  
    49  const (
    50  	TableName = "people"
    51  	IndexName = "idx_name"
    52  )
    53  
    54  var typedColColl = schema.NewColCollection(
    55  	schema.NewColumn("id", IdTag, types.UUIDKind, true, schema.NotNullConstraint{}),
    56  	schema.NewColumn("name", NameTag, types.StringKind, false, schema.NotNullConstraint{}),
    57  	schema.NewColumn("age", AgeTag, types.UintKind, false, schema.NotNullConstraint{}),
    58  	schema.NewColumn("is_married", IsMarriedTag, types.BoolKind, false, schema.NotNullConstraint{}),
    59  	schema.NewColumn("title", TitleTag, types.StringKind, false),
    60  )
    61  
    62  // modified by init()
    63  var TypedSchema = schema.MustSchemaFromCols(typedColColl)
    64  
    65  // modified by init()
    66  var UntypedSchema, _ = untyped.UntypeSchema(TypedSchema)
    67  var TypedRows []row.Row
    68  var UntypedRows []row.Row
    69  
    70  func init() {
    71  	for i := 0; i < len(UUIDS); i++ {
    72  
    73  		taggedVals := row.TaggedValues{
    74  			IdTag:        types.UUID(UUIDS[i]),
    75  			NameTag:      types.String(Names[i]),
    76  			AgeTag:       types.Uint(Ages[i]),
    77  			TitleTag:     types.String(Titles[i]),
    78  			IsMarriedTag: types.Bool(MaritalStatus[i]),
    79  		}
    80  
    81  		r, err := row.New(types.Format_Default, TypedSchema, taggedVals)
    82  
    83  		if err != nil {
    84  			panic(err)
    85  		}
    86  
    87  		TypedRows = append(TypedRows, r)
    88  
    89  		taggedVals = row.TaggedValues{
    90  			IdTag:        types.UUID(uuid.MustParse(UUIDS[i].String())),
    91  			NameTag:      types.String(Names[i]),
    92  			AgeTag:       types.Uint(Ages[i]),
    93  			TitleTag:     types.String(Titles[i]),
    94  			IsMarriedTag: types.Bool(MaritalStatus[i]),
    95  		}
    96  
    97  		r, err = row.New(types.Format_Default, TypedSchema, taggedVals)
    98  
    99  		if err != nil {
   100  			panic(err)
   101  		}
   102  
   103  		UntypedRows = append(UntypedRows, r)
   104  	}
   105  
   106  	_, err := TypedSchema.Indexes().AddIndexByColTags(IndexName, []uint64{NameTag}, schema.IndexProperties{IsUnique: false, Comment: ""})
   107  	if err != nil {
   108  		panic(err)
   109  	}
   110  	_, err = UntypedSchema.Indexes().AddIndexByColTags(IndexName, []uint64{NameTag}, schema.IndexProperties{IsUnique: false, Comment: ""})
   111  	if err != nil {
   112  		panic(err)
   113  	}
   114  }
   115  
   116  func NewTypedRow(id uuid.UUID, name string, age uint, isMarried bool, title *string) row.Row {
   117  	var titleVal types.Value
   118  	if title != nil {
   119  		titleVal = types.String(*title)
   120  	}
   121  
   122  	taggedVals := row.TaggedValues{
   123  		IdTag:        types.UUID(id),
   124  		NameTag:      types.String(name),
   125  		AgeTag:       types.Uint(age),
   126  		IsMarriedTag: types.Bool(isMarried),
   127  		TitleTag:     titleVal,
   128  	}
   129  
   130  	r, err := row.New(types.Format_Default, TypedSchema, taggedVals)
   131  
   132  	if err != nil {
   133  		panic(err)
   134  	}
   135  
   136  	return r
   137  }
   138  
   139  func CreateTestDataTable(typed bool) (*table.InMemTable, schema.Schema) {
   140  	sch := TypedSchema
   141  	rows := TypedRows
   142  	if !typed {
   143  		sch = UntypedSchema
   144  		rows = UntypedRows
   145  	}
   146  
   147  	imt := table.NewInMemTable(sch)
   148  
   149  	for _, r := range rows {
   150  		err := imt.AppendRow(r)
   151  		if err != nil {
   152  			panic(err)
   153  		}
   154  	}
   155  
   156  	return imt, sch
   157  }
   158  
   159  // AddColToRows adds a column to all the rows given and returns it. This method relies on the fact that
   160  // noms_row.SetColVal doesn't need a full schema, just one that includes the column being set.
   161  func AddColToRows(t *testing.T, rs []row.Row, tag uint64, val types.Value) []row.Row {
   162  	if types.IsNull(val) {
   163  		return rs
   164  	}
   165  
   166  	colColl := schema.NewColCollection(schema.NewColumn("unused", tag, val.Kind(), false))
   167  	fakeSch := schema.UnkeyedSchemaFromCols(colColl)
   168  
   169  	newRows := make([]row.Row, len(rs))
   170  	var err error
   171  	for i, r := range rs {
   172  		newRows[i], err = r.SetColVal(tag, val, fakeSch)
   173  		require.NoError(t, err)
   174  	}
   175  	return newRows
   176  }
   177  
   178  // Coerces the rows given into the schema given. Only possible if the types are equivalent.
   179  func ConvertToSchema(sch schema.Schema, rs ...row.Row) []row.Row {
   180  	newRows := make([]row.Row, len(rs))
   181  	for i, r := range rs {
   182  		taggedVals := make(row.TaggedValues)
   183  		_, err := r.IterCols(func(tag uint64, val types.Value) (stop bool, err error) {
   184  			if _, ok := sch.GetAllCols().GetByTag(tag); ok {
   185  				taggedVals[tag] = val
   186  			}
   187  			return false, nil
   188  		})
   189  
   190  		if err != nil {
   191  			panic(err)
   192  		}
   193  
   194  		newRows[i], err = row.New(types.Format_Default, sch, taggedVals)
   195  
   196  		if err != nil {
   197  			panic(err)
   198  		}
   199  	}
   200  	return newRows
   201  }
   202  
   203  // MustRowData converts a slice of row.TaggedValues into a noms types.Map containing that data.
   204  func MustRowData(t *testing.T, ctx context.Context, vrw types.ValueReadWriter, sch schema.Schema, colVals []row.TaggedValues) *types.Map {
   205  	m, err := types.NewMap(ctx, vrw)
   206  	require.NoError(t, err)
   207  
   208  	me := m.Edit()
   209  	for _, taggedVals := range colVals {
   210  		r, err := row.New(types.Format_Default, sch, taggedVals)
   211  		require.NoError(t, err)
   212  
   213  		me = me.Set(r.NomsMapKey(sch), r.NomsMapValue(sch))
   214  	}
   215  
   216  	m, err = me.Map(ctx)
   217  	require.NoError(t, err)
   218  
   219  	return &m
   220  }
   221  
   222  // MustMap contructs a types.Map for a slice of alternating key, value types.Value.
   223  func MustMap(t *testing.T, vrw types.ValueReadWriter, kv ...types.Value) types.Map {
   224  	m, err := types.NewMap(context.Background(), vrw, kv...)
   225  	require.NoError(t, err)
   226  	return m
   227  }
   228  
   229  // MustMap contructs a types.Tuple for a slice of types.Values.
   230  func MustTuple(vals ...types.Value) types.Tuple {
   231  	tup, err := types.NewTuple(types.Format_Default, vals...)
   232  	if err != nil {
   233  		panic(err)
   234  	}
   235  	return tup
   236  }