cosmos-sdk/db/version_manager_test.go
Robert Zaremba 284affb594
feat: use cosmos/rocksdb rather than replace trick (#10927)
## Description

A follow up to https://github.com/cosmos/gorocksdb/pull/4 (see description there).
Also:
* remove import aliasing in rocksdb package.

Closes: #10915

TODO:
- [x] merge https://github.com/cosmos/gorocksdb/pull/4
- [x] tag new release in https://github.com/cosmos/gorocksdb
- [x] Update this PR to include new release
- [ ] tag `db/v1.0.0` (or `db/v0.1.0)` 

---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
2022-01-11 22:29:53 +00:00

60 lines
1.4 KiB
Go

package db_test
import (
"sort"
"testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/db"
)
// Test that VersionManager satisfies the behavior of VersionSet
func TestVersionManager(t *testing.T) {
vm := db.NewVersionManager(nil)
require.Equal(t, uint64(0), vm.Last())
require.Equal(t, 0, vm.Count())
require.True(t, vm.Equal(vm))
require.False(t, vm.Exists(0))
id1, err := vm.Save(0)
require.NoError(t, err)
require.Equal(t, uint64(1), id1)
require.True(t, vm.Exists(id1))
id2, err := vm.Save(0)
require.NoError(t, err)
require.True(t, vm.Exists(id2))
id3, err := vm.Save(0)
require.NoError(t, err)
require.True(t, vm.Exists(id3))
_, err = vm.Save(id1) // can't save existing id
require.Error(t, err)
id4, err := vm.Save(0)
require.NoError(t, err)
require.True(t, vm.Exists(id4))
vm.Delete(id4)
require.False(t, vm.Exists(id4))
vm.Delete(id1)
require.False(t, vm.Exists(id1))
require.Equal(t, id2, vm.Initial())
require.Equal(t, id3, vm.Last())
var all []uint64
for it := vm.Iterator(); it.Next(); {
all = append(all, it.Value())
}
sort.Slice(all, func(i, j int) bool { return all[i] < all[j] })
require.Equal(t, []uint64{id2, id3}, all)
vmc := vm.Copy()
id5, err := vmc.Save(0)
require.NoError(t, err)
require.False(t, vm.Exists(id5)) // true copy is made
vm2 := db.NewVersionManager([]uint64{id2, id3})
require.True(t, vm.Equal(vm2))
}