github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/row/row_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 row 16 17 import ( 18 "testing" 19 20 "github.com/stretchr/testify/assert" 21 "github.com/stretchr/testify/require" 22 23 "github.com/dolthub/dolt/go/libraries/doltcore/schema" 24 "github.com/dolthub/dolt/go/store/types" 25 ) 26 27 func TestGetFieldByName(t *testing.T) { 28 r, err := newTestRow() 29 require.NoError(t, err) 30 31 val, ok := GetFieldByName(lnColName, r, sch) 32 33 if !ok { 34 t.Error("Expected to find value") 35 } else if !val.Equals(lnVal) { 36 t.Error("Unexpected value") 37 } 38 39 val, ok = GetFieldByName(reservedColName, r, sch) 40 41 if ok { 42 t.Error("should not find missing key") 43 } else if val != nil { 44 t.Error("missing key should return null value") 45 } 46 } 47 48 func TestGetFieldByNameWithDefault(t *testing.T) { 49 r, err := newTestRow() 50 require.NoError(t, err) 51 defVal := types.String("default") 52 53 val := GetFieldByNameWithDefault(lnColName, defVal, r, sch) 54 55 if !val.Equals(lnVal) { 56 t.Error("expected:", lnVal, "actual", val) 57 } 58 59 val = GetFieldByNameWithDefault(reservedColName, defVal, r, sch) 60 61 if !val.Equals(defVal) { 62 t.Error("expected:", defVal, "actual", val) 63 } 64 } 65 66 func TestIsValid(t *testing.T) { 67 r, err := newTestRow() 68 require.NoError(t, err) 69 70 isv, err := IsValid(r, sch) 71 require.NoError(t, err) 72 assert.True(t, isv) 73 invCol, err := GetInvalidCol(r, sch) 74 require.NoError(t, err) 75 assert.Nil(t, invCol) 76 column, colConstraint, err := GetInvalidConstraint(r, sch) 77 require.NoError(t, err) 78 assert.Nil(t, column) 79 assert.Nil(t, colConstraint) 80 81 updatedRow, err := r.SetColVal(lnColTag, nil, sch) 82 require.NoError(t, err) 83 84 isv, err = IsValid(updatedRow, sch) 85 require.NoError(t, err) 86 assert.False(t, isv) 87 88 col, err := GetInvalidCol(updatedRow, sch) 89 require.NoError(t, err) 90 assert.NotNil(t, col) 91 assert.Equal(t, col.Tag, uint64(lnColTag)) 92 93 col, cnst, err := GetInvalidConstraint(updatedRow, sch) 94 require.NoError(t, err) 95 assert.NotNil(t, col) 96 assert.Equal(t, col.Tag, uint64(lnColTag)) 97 assert.Equal(t, cnst, schema.NotNullConstraint{}) 98 99 // Test getting a bad column without the constraint failure 100 t.Run("invalid type", func(t *testing.T) { 101 nonPkCols := []schema.Column{ 102 {Name: addrColName, Tag: addrColTag, Kind: types.BoolKind, IsPartOfPK: false, Constraints: nil}, 103 } 104 nonKeyColColl := schema.NewColCollection(nonPkCols...) 105 newSch, err := schema.SchemaFromPKAndNonPKCols(testKeyColColl, nonKeyColColl) 106 require.NoError(t, err) 107 108 isv, err := IsValid(r, newSch) 109 require.NoError(t, err) 110 assert.False(t, isv) 111 112 col, err = GetInvalidCol(r, newSch) 113 require.NoError(t, err) 114 require.NotNil(t, col) 115 assert.Equal(t, col.Tag, uint64(addrColTag)) 116 117 col, cnst, err = GetInvalidConstraint(r, newSch) 118 require.NoError(t, err) 119 assert.Nil(t, cnst) 120 assert.Equal(t, col.Tag, uint64(addrColTag)) 121 }) 122 } 123 124 func TestAreEqual(t *testing.T) { 125 r, err := newTestRow() 126 require.NoError(t, err) 127 128 updatedRow, err := r.SetColVal(lnColTag, types.String("new"), sch) 129 require.NoError(t, err) 130 131 assert.True(t, AreEqual(r, r, sch)) 132 assert.False(t, AreEqual(r, updatedRow, sch)) 133 }