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.

151 lines
3.4 KiB

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 DBBackendType) {
  15. // Default
  16. dir, dirname := cmn.Tempdir(fmt.Sprintf("test_backend_%s_", backend))
  17. defer dir.Close()
  18. db := NewDB("testdb", backend, dirname)
  19. // A nonexistent key should return nil, even if the key is empty
  20. require.Nil(t, db.Get([]byte("")))
  21. // A nonexistent key should return nil, even if the key is nil
  22. require.Nil(t, db.Get(nil))
  23. // A nonexistent key should return nil.
  24. key := []byte("abc")
  25. require.Nil(t, db.Get(key))
  26. // Set empty value.
  27. db.Set(key, []byte(""))
  28. require.NotNil(t, db.Get(key))
  29. require.Empty(t, db.Get(key))
  30. // Set nil value.
  31. db.Set(key, nil)
  32. require.NotNil(t, db.Get(key))
  33. require.Empty(t, db.Get(key))
  34. // Delete.
  35. db.Delete(key)
  36. require.Nil(t, db.Get(key))
  37. }
  38. func TestBackendsGetSetDelete(t *testing.T) {
  39. for dbType := range backends {
  40. testBackendGetSetDelete(t, dbType)
  41. }
  42. }
  43. func withDB(t *testing.T, creator dbCreator, fn func(DB)) {
  44. name := cmn.Fmt("test_%x", cmn.RandStr(12))
  45. db, err := creator(name, "")
  46. defer cleanupDBDir("", name)
  47. assert.Nil(t, err)
  48. fn(db)
  49. db.Close()
  50. }
  51. func TestBackendsNilKeys(t *testing.T) {
  52. // Test all backends.
  53. for dbType, creator := range backends {
  54. withDB(t, creator, func(db DB) {
  55. t.Run(fmt.Sprintf("Testing %s", dbType), func(t *testing.T) {
  56. // Nil keys are treated as the empty key for most operations.
  57. expect := func(key, value []byte) {
  58. if len(key) == 0 { // nil or empty
  59. assert.Equal(t, db.Get(nil), db.Get([]byte("")))
  60. assert.Equal(t, db.Has(nil), db.Has([]byte("")))
  61. }
  62. assert.Equal(t, db.Get(key), value)
  63. assert.Equal(t, db.Has(key), value != nil)
  64. }
  65. // Not set
  66. expect(nil, nil)
  67. // Set nil value
  68. db.Set(nil, nil)
  69. expect(nil, []byte(""))
  70. // Set empty value
  71. db.Set(nil, []byte(""))
  72. expect(nil, []byte(""))
  73. // Set nil, Delete nil
  74. db.Set(nil, []byte("abc"))
  75. expect(nil, []byte("abc"))
  76. db.Delete(nil)
  77. expect(nil, nil)
  78. // Set nil, Delete empty
  79. db.Set(nil, []byte("abc"))
  80. expect(nil, []byte("abc"))
  81. db.Delete([]byte(""))
  82. expect(nil, nil)
  83. // Set empty, Delete nil
  84. db.Set([]byte(""), []byte("abc"))
  85. expect(nil, []byte("abc"))
  86. db.Delete(nil)
  87. expect(nil, nil)
  88. // Set empty, Delete empty
  89. db.Set([]byte(""), []byte("abc"))
  90. expect(nil, []byte("abc"))
  91. db.Delete([]byte(""))
  92. expect(nil, nil)
  93. // SetSync nil, DeleteSync nil
  94. db.SetSync(nil, []byte("abc"))
  95. expect(nil, []byte("abc"))
  96. db.DeleteSync(nil)
  97. expect(nil, nil)
  98. // SetSync nil, DeleteSync empty
  99. db.SetSync(nil, []byte("abc"))
  100. expect(nil, []byte("abc"))
  101. db.DeleteSync([]byte(""))
  102. expect(nil, nil)
  103. // SetSync empty, DeleteSync nil
  104. db.SetSync([]byte(""), []byte("abc"))
  105. expect(nil, []byte("abc"))
  106. db.DeleteSync(nil)
  107. expect(nil, nil)
  108. // SetSync empty, DeleteSync empty
  109. db.SetSync([]byte(""), []byte("abc"))
  110. expect(nil, []byte("abc"))
  111. db.DeleteSync([]byte(""))
  112. expect(nil, nil)
  113. })
  114. })
  115. }
  116. }
  117. func TestGoLevelDBBackend(t *testing.T) {
  118. name := cmn.Fmt("test_%x", cmn.RandStr(12))
  119. db := NewDB(name, GoLevelDBBackend, "")
  120. defer cleanupDBDir("", name)
  121. _, ok := db.(*GoLevelDB)
  122. assert.True(t, ok)
  123. }