github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/dtestutils/environment.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/stretchr/testify/require" 22 23 "github.com/dolthub/dolt/go/libraries/doltcore/doltdb" 24 "github.com/dolthub/dolt/go/libraries/doltcore/env" 25 "github.com/dolthub/dolt/go/libraries/doltcore/schema/encoding" 26 "github.com/dolthub/dolt/go/libraries/doltcore/table" 27 "github.com/dolthub/dolt/go/libraries/doltcore/table/editor" 28 "github.com/dolthub/dolt/go/libraries/doltcore/table/typed/noms" 29 "github.com/dolthub/dolt/go/libraries/utils/filesys" 30 "github.com/dolthub/dolt/go/store/types" 31 ) 32 33 const ( 34 TestHomeDir = "/user/bheni" 35 WorkingDir = "/user/bheni/datasets/states" 36 ) 37 38 func testHomeDirFunc() (string, error) { 39 return TestHomeDir, nil 40 } 41 42 func CreateTestEnv() *env.DoltEnv { 43 const name = "billy bob" 44 const email = "bigbillieb@fake.horse" 45 initialDirs := []string{TestHomeDir, WorkingDir} 46 fs := filesys.NewInMemFS(initialDirs, nil, WorkingDir) 47 dEnv := env.Load(context.Background(), testHomeDirFunc, fs, doltdb.InMemDoltDB, "test") 48 cfg, _ := dEnv.Config.GetConfig(env.GlobalConfig) 49 cfg.SetStrings(map[string]string{ 50 env.UserNameKey: name, 51 env.UserEmailKey: email, 52 }) 53 err := dEnv.InitRepo(context.Background(), types.Format_Default, name, email) 54 55 if err != nil { 56 panic("Failed to initialize environment:" + err.Error()) 57 } 58 59 return dEnv 60 } 61 62 func CreateEnvWithSeedData(t *testing.T) *env.DoltEnv { 63 dEnv := CreateTestEnv() 64 imt, sch := CreateTestDataTable(true) 65 66 ctx := context.Background() 67 vrw := dEnv.DoltDB.ValueReadWriter() 68 rd := table.NewInMemTableReader(imt) 69 wr := noms.NewNomsMapCreator(ctx, vrw, sch) 70 71 _, _, err := table.PipeRows(ctx, rd, wr, false) 72 require.NoError(t, err) 73 err = rd.Close(ctx) 74 require.NoError(t, err) 75 err = wr.Close(ctx) 76 require.NoError(t, err) 77 78 ai := sch.Indexes().AllIndexes() 79 sch = wr.GetSchema() 80 sch.Indexes().Merge(ai...) 81 82 schVal, err := encoding.MarshalSchemaAsNomsValue(ctx, vrw, sch) 83 require.NoError(t, err) 84 empty, err := types.NewMap(ctx, vrw) 85 require.NoError(t, err) 86 tbl, err := doltdb.NewTable(ctx, vrw, schVal, wr.GetMap(), empty, nil) 87 require.NoError(t, err) 88 tbl, err = editor.RebuildAllIndexes(ctx, tbl) 89 require.NoError(t, err) 90 91 sch, err = tbl.GetSchema(ctx) 92 require.NoError(t, err) 93 rows, err := tbl.GetRowData(ctx) 94 require.NoError(t, err) 95 indexes, err := tbl.GetIndexData(ctx) 96 require.NoError(t, err) 97 err = dEnv.PutTableToWorking(ctx, sch, rows, indexes, TableName, nil) 98 require.NoError(t, err) 99 100 return dEnv 101 }