You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package db
  2. import (
  3. "fmt"
  4. "os"
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. )
  11. func cleanupDBDir(dir, name string) {
  12. os.RemoveAll(filepath.Join(dir, name) + ".db")
  13. }
  14. func testBackendGetSetDelete(t *testing.T, backend string) {
  15. // Default
  16. dir, dirname := cmn.Tempdir(fmt.Sprintf("test_backend_%s_", backend))
  17. defer dir.Close()
  18. db := NewDB("testdb", backend, dirname)
  19. key := []byte("abc")
  20. require.Nil(t, db.Get(key))
  21. // Set empty ("")
  22. db.Set(key, []byte(""))
  23. require.NotNil(t, db.Get(key))
  24. require.Empty(t, db.Get(key))
  25. // Set empty (nil)
  26. db.Set(key, nil)
  27. require.NotNil(t, db.Get(key))
  28. require.Empty(t, db.Get(key))
  29. // Delete
  30. db.Delete(key)
  31. require.Nil(t, db.Get(key))
  32. }
  33. func TestBackendsGetSetDelete(t *testing.T) {
  34. for dbType, _ := range backends {
  35. testBackendGetSetDelete(t, dbType)
  36. }
  37. }
  38. func TestBackendsNilKeys(t *testing.T) {
  39. // test all backends
  40. for dbType, creator := range backends {
  41. name := cmn.Fmt("test_%x", cmn.RandStr(12))
  42. db, err := creator(name, "")
  43. defer cleanupDBDir("", name)
  44. assert.Nil(t, err)
  45. panicMsg := "expecting %s.%s to panic"
  46. assert.Panics(t, func() { db.Get(nil) }, panicMsg, dbType, "get")
  47. assert.Panics(t, func() { db.Has(nil) }, panicMsg, dbType, "has")
  48. assert.Panics(t, func() { db.Set(nil, []byte("abc")) }, panicMsg, dbType, "set")
  49. assert.Panics(t, func() { db.SetSync(nil, []byte("abc")) }, panicMsg, dbType, "setsync")
  50. assert.Panics(t, func() { db.Delete(nil) }, panicMsg, dbType, "delete")
  51. assert.Panics(t, func() { db.DeleteSync(nil) }, panicMsg, dbType, "deletesync")
  52. db.Close()
  53. }
  54. }
  55. func TestGoLevelDBBackendStr(t *testing.T) {
  56. name := cmn.Fmt("test_%x", cmn.RandStr(12))
  57. db := NewDB(name, LevelDBBackendStr, "")
  58. defer cleanupDBDir("", name)
  59. if _, ok := backends[CLevelDBBackendStr]; !ok {
  60. _, ok := db.(*GoLevelDB)
  61. assert.True(t, ok)
  62. }
  63. }