github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/doltdb/gc_test.go (about) 1 // Copyright 2020 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 doltdb_test 16 17 import ( 18 "context" 19 "testing" 20 21 "github.com/dolthub/go-mysql-server/sql" 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/require" 24 25 "github.com/dolthub/dolt/go/cmd/dolt/commands" 26 "github.com/dolthub/dolt/go/libraries/doltcore/dtestutils" 27 "github.com/dolthub/dolt/go/libraries/doltcore/sqle" 28 "github.com/dolthub/dolt/go/store/types" 29 ) 30 31 type gcTest struct { 32 name string 33 setup []testCommand 34 garbage types.Value 35 query string 36 expected []sql.Row 37 } 38 39 var gcTests = []gcTest{ 40 { 41 name: "gc test", 42 setup: []testCommand{ 43 {commands.SqlCmd{}, []string{"-q", "INSERT INTO test VALUES (0),(1),(2);"}}, 44 }, 45 garbage: types.String("supercalifragilisticexpialidocious"), 46 }, 47 } 48 49 var gcSetupCommon = []testCommand{ 50 {commands.SqlCmd{}, []string{"-q", "CREATE TABLE test (pk int PRIMARY KEY)"}}, 51 } 52 53 func TestGarbageCollection(t *testing.T) { 54 require.True(t, true) 55 assert.True(t, true) 56 57 for _, gct := range gcTests { 58 t.Run(gct.name, func(t *testing.T) { 59 testGarbageCollection(t, gct) 60 }) 61 } 62 63 } 64 65 func testGarbageCollection(t *testing.T, test gcTest) { 66 ctx := context.Background() 67 dEnv := dtestutils.CreateTestEnv() 68 69 for _, c := range gcSetupCommon { 70 exitCode := c.cmd.Exec(ctx, c.cmd.Name(), c.args, dEnv) 71 require.Equal(t, 0, exitCode) 72 } 73 for _, c := range test.setup { 74 exitCode := c.cmd.Exec(ctx, c.cmd.Name(), c.args, dEnv) 75 require.Equal(t, 0, exitCode) 76 } 77 78 garbageRef, err := dEnv.DoltDB.ValueReadWriter().WriteValue(ctx, test.garbage) 79 require.NoError(t, err) 80 val, err := dEnv.DoltDB.ValueReadWriter().ReadValue(ctx, garbageRef.TargetHash()) 81 require.NoError(t, err) 82 assert.NotNil(t, val) 83 84 working, err := dEnv.WorkingRoot(ctx) 85 require.NoError(t, err) 86 h, err := working.HashOf() 87 require.NoError(t, err) 88 // save working root during GC 89 err = dEnv.DoltDB.GC(ctx, h) 90 require.NoError(t, err) 91 92 working, err = dEnv.WorkingRoot(ctx) 93 require.NoError(t, err) 94 // assert all out rows are present after gc 95 actual, err := sqle.ExecuteSelect(dEnv, dEnv.DoltDB, working, test.query) 96 require.NoError(t, err) 97 assert.Equal(t, test.expected, actual) 98 99 // assert that garbage was collected 100 val, err = dEnv.DoltDB.ValueReadWriter().ReadValue(ctx, garbageRef.TargetHash()) 101 require.NoError(t, err) 102 assert.Nil(t, val) 103 }