github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/chunks/chunk_store_common_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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  package chunks
    23  
    24  import (
    25  	"context"
    26  
    27  	"github.com/stretchr/testify/suite"
    28  
    29  	"github.com/dolthub/dolt/go/store/constants"
    30  	"github.com/dolthub/dolt/go/store/hash"
    31  )
    32  
    33  type ChunkStoreTestSuite struct {
    34  	suite.Suite
    35  	Factory *memoryStoreFactory
    36  }
    37  
    38  func (suite *ChunkStoreTestSuite) TestChunkStorePut() {
    39  	store := suite.Factory.CreateStore(context.Background(), "ns")
    40  	input := "abc"
    41  	c := NewChunk([]byte(input))
    42  	err := store.Put(context.Background(), c)
    43  	suite.NoError(err)
    44  	h := c.Hash()
    45  
    46  	// Reading it via the API should work.
    47  	assertInputInStore(input, h, store, suite.Assert())
    48  }
    49  
    50  func (suite *ChunkStoreTestSuite) TestChunkStoreRoot() {
    51  	store := suite.Factory.CreateStore(context.Background(), "ns")
    52  	oldRoot, err := store.Root(context.Background())
    53  	suite.NoError(err)
    54  	suite.True(oldRoot.IsEmpty())
    55  
    56  	bogusRoot := hash.Parse("8habda5skfek1265pc5d5l1orptn5dr0")
    57  	newRoot := hash.Parse("8la6qjbh81v85r6q67lqbfrkmpds14lg")
    58  
    59  	// Try to update root with bogus oldRoot
    60  	result, err := store.Commit(context.Background(), newRoot, bogusRoot)
    61  	suite.NoError(err)
    62  	suite.False(result)
    63  
    64  	// Now do a valid root update
    65  	result, err = store.Commit(context.Background(), newRoot, oldRoot)
    66  	suite.NoError(err)
    67  	suite.True(result)
    68  }
    69  
    70  func (suite *ChunkStoreTestSuite) TestChunkStoreCommitPut() {
    71  	name := "ns"
    72  	store := suite.Factory.CreateStore(context.Background(), name)
    73  	input := "abc"
    74  	c := NewChunk([]byte(input))
    75  	err := store.Put(context.Background(), c)
    76  	suite.NoError(err)
    77  	h := c.Hash()
    78  
    79  	// Reading it via the API should work...
    80  	assertInputInStore(input, h, store, suite.Assert())
    81  	// ...but it shouldn't be persisted yet
    82  	assertInputNotInStore(input, h, suite.Factory.CreateStore(context.Background(), name), suite.Assert())
    83  
    84  	r, err := store.Root(context.Background())
    85  	suite.NoError(err)
    86  	_, err = store.Commit(context.Background(), h, r) // Commit persists Chunks
    87  	suite.NoError(err)
    88  	assertInputInStore(input, h, store, suite.Assert())
    89  	assertInputInStore(input, h, suite.Factory.CreateStore(context.Background(), name), suite.Assert())
    90  }
    91  
    92  func (suite *ChunkStoreTestSuite) TestChunkStoreGetNonExisting() {
    93  	store := suite.Factory.CreateStore(context.Background(), "ns")
    94  	h := hash.Parse("11111111111111111111111111111111")
    95  	c, err := store.Get(context.Background(), h)
    96  	suite.NoError(err)
    97  	suite.True(c.IsEmpty())
    98  }
    99  
   100  func (suite *ChunkStoreTestSuite) TestChunkStoreVersion() {
   101  	store := suite.Factory.CreateStore(context.Background(), "ns")
   102  	oldRoot, err := store.Root(context.Background())
   103  	suite.NoError(err)
   104  	suite.True(oldRoot.IsEmpty())
   105  	newRoot := hash.Parse("11111222223333344444555556666677")
   106  	success, err := store.Commit(context.Background(), newRoot, oldRoot)
   107  	suite.NoError(err)
   108  	suite.True(success)
   109  
   110  	suite.Equal(constants.NomsVersion, store.Version())
   111  }
   112  
   113  func (suite *ChunkStoreTestSuite) TestChunkStoreCommitUnchangedRoot() {
   114  	store1, store2 := suite.Factory.CreateStore(context.Background(), "ns"), suite.Factory.CreateStore(context.Background(), "ns")
   115  	input := "abc"
   116  	c := NewChunk([]byte(input))
   117  	err := store1.Put(context.Background(), c)
   118  	suite.NoError(err)
   119  	h := c.Hash()
   120  
   121  	// Reading c from store1 via the API should work...
   122  	assertInputInStore(input, h, store1, suite.Assert())
   123  	// ...but not store2.
   124  	assertInputNotInStore(input, h, store2, suite.Assert())
   125  
   126  	newRoot, err := store1.Root(context.Background())
   127  	suite.NoError(err)
   128  	oldRoot, err := store1.Root(context.Background())
   129  	suite.NoError(err)
   130  	_, err = store1.Commit(context.Background(), newRoot, oldRoot)
   131  	suite.NoError(err)
   132  
   133  	err = store2.Rebase(context.Background())
   134  	suite.NoError(err)
   135  
   136  	// Now, reading c from store2 via the API should work...
   137  	assertInputInStore(input, h, store2, suite.Assert())
   138  }