github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/schema/col_coll_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 schema 16 17 import ( 18 "reflect" 19 "strconv" 20 "testing" 21 22 "github.com/stretchr/testify/assert" 23 24 "github.com/dolthub/dolt/go/libraries/doltcore/schema/typeinfo" 25 "github.com/dolthub/dolt/go/store/types" 26 ) 27 28 var firstNameCol = Column{"first", 0, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil} 29 var lastNameCol = Column{"last", 1, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil} 30 var firstNameCapsCol = Column{"FiRsT", 2, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil} 31 var lastNameCapsCol = Column{"LAST", 3, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil} 32 33 func TestGetByNameAndTag(t *testing.T) { 34 cols := []Column{firstNameCol, lastNameCol, firstNameCapsCol, lastNameCapsCol} 35 colColl := NewColCollection(cols...) 36 37 tests := []struct { 38 name string 39 tag uint64 40 expected Column 41 shouldBeOk bool 42 }{ 43 {firstNameCol.Name, firstNameCol.Tag, firstNameCol, true}, 44 {lastNameCol.Name, lastNameCol.Tag, lastNameCol, true}, 45 {firstNameCapsCol.Name, firstNameCapsCol.Tag, firstNameCapsCol, true}, 46 {lastNameCapsCol.Name, lastNameCapsCol.Tag, lastNameCapsCol, true}, 47 {"FIRST", InvalidTag, InvalidCol, false}, 48 {"missing", InvalidTag, InvalidCol, false}, 49 } 50 51 for _, test := range tests { 52 t.Run(test.name, func(t *testing.T) { 53 actual, ok := colColl.GetByName(test.name) 54 55 if ok != test.shouldBeOk { 56 t.Errorf("name - shouldBeOk: %v, ok: %v", test.shouldBeOk, ok) 57 } else if !reflect.DeepEqual(actual, test.expected) { 58 t.Errorf("name - %v != %v", actual, test.expected) 59 } 60 61 actual, ok = colColl.GetByTag(test.tag) 62 63 if ok != test.shouldBeOk { 64 t.Errorf("tag - shouldBeOk: %v, ok: %v", test.shouldBeOk, ok) 65 } else if !reflect.DeepEqual(actual, test.expected) { 66 t.Errorf("tag - %v != %v", actual, test.expected) 67 } 68 }) 69 } 70 } 71 72 func TestGetByNameCaseInsensitive(t *testing.T) { 73 cols := []Column{firstNameCol, lastNameCol, firstNameCapsCol, lastNameCapsCol} 74 colColl := NewColCollection(cols...) 75 76 tests := []struct { 77 name string 78 expected Column 79 shouldBeOk bool 80 }{ 81 {firstNameCol.Name, firstNameCol, true}, 82 {lastNameCol.Name, lastNameCol, true}, 83 {firstNameCapsCol.Name, firstNameCol, true}, 84 {lastNameCapsCol.Name, lastNameCol, true}, 85 {"missing", InvalidCol, false}, 86 } 87 88 for _, test := range tests { 89 t.Run(test.name, func(t *testing.T) { 90 91 actual, ok := colColl.GetByNameCaseInsensitive(test.name) 92 93 if ok != test.shouldBeOk { 94 t.Errorf("name - shouldBeOk: %v, ok: %v", test.shouldBeOk, ok) 95 } else if !reflect.DeepEqual(actual, test.expected) { 96 t.Errorf("name - %v != %v", actual, test.expected) 97 } 98 99 }) 100 } 101 } 102 103 func TestAppendAndItrInSortOrder(t *testing.T) { 104 cols := []Column{ 105 {"0", 0, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 106 {"2", 2, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 107 {"4", 4, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 108 {"3", 3, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 109 {"1", 1, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 110 } 111 cols2 := []Column{ 112 {"7", 7, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 113 {"9", 9, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 114 {"5", 5, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 115 {"8", 8, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 116 {"6", 6, types.StringKind, false, typeinfo.StringDefaultType, "", false, "", nil}, 117 } 118 119 colColl := NewColCollection(cols...) 120 validateIter(len(cols), colColl, t) 121 colColl2 := colColl.Append(cols2...) 122 validateIter(len(cols), colColl, t) //validate immutability 123 validateIter(len(cols)+len(cols2), colColl2, t) 124 } 125 126 func validateIter(numCols int, colColl *ColCollection, t *testing.T) { 127 if numCols != colColl.Size() { 128 t.Error("missing data") 129 } 130 131 err := colColl.Iter(func(tag uint64, col Column) (stop bool, err error) { 132 if col.Name != strconv.FormatUint(tag, 10) || col.Tag != tag { 133 t.Errorf("tag:%d - %v", tag, col) 134 } 135 136 return false, nil 137 }) 138 139 assert.NoError(t, err) 140 }