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.

135 lines
3.1 KiB

  1. package db
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. cmn "github.com/tendermint/tmlibs/common"
  7. )
  8. func checkValid(t *testing.T, itr Iterator, expected bool) {
  9. valid := itr.Valid()
  10. assert.Equal(t, expected, valid)
  11. }
  12. func checkNext(t *testing.T, itr Iterator, expected bool) {
  13. itr.Next()
  14. valid := itr.Valid()
  15. assert.Equal(t, expected, valid)
  16. }
  17. func checkNextPanics(t *testing.T, itr Iterator) {
  18. assert.Panics(t, func() { itr.Next() }, "checkNextPanics expected panic but didn't")
  19. }
  20. func checkItem(t *testing.T, itr Iterator, key []byte, value []byte) {
  21. k, v := itr.Key(), itr.Value()
  22. assert.Exactly(t, key, k)
  23. assert.Exactly(t, value, v)
  24. }
  25. func checkInvalid(t *testing.T, itr Iterator) {
  26. checkValid(t, itr, false)
  27. checkKeyPanics(t, itr)
  28. checkValuePanics(t, itr)
  29. checkNextPanics(t, itr)
  30. }
  31. func checkKeyPanics(t *testing.T, itr Iterator) {
  32. assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't")
  33. }
  34. func checkValuePanics(t *testing.T, itr Iterator) {
  35. assert.Panics(t, func() { itr.Key() }, "checkValuePanics expected panic but didn't")
  36. }
  37. func newTempDB(t *testing.T, backend string) (db DB) {
  38. dir, dirname := cmn.Tempdir("test_go_iterator")
  39. db = NewDB("testdb", backend, dirname)
  40. dir.Close()
  41. return db
  42. }
  43. func TestDBIteratorSingleKey(t *testing.T) {
  44. for backend, _ := range backends {
  45. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  46. db := newTempDB(t, backend)
  47. db.SetSync(bz("1"), bz("value_1"))
  48. itr := db.Iterator(BeginningKey(), EndingKey())
  49. checkValid(t, itr, true)
  50. checkNext(t, itr, false)
  51. checkValid(t, itr, false)
  52. checkNextPanics(t, itr)
  53. // Once invalid...
  54. checkInvalid(t, itr)
  55. })
  56. }
  57. }
  58. func TestDBIteratorTwoKeys(t *testing.T) {
  59. for backend, _ := range backends {
  60. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  61. db := newTempDB(t, backend)
  62. db.SetSync(bz("1"), bz("value_1"))
  63. db.SetSync(bz("2"), bz("value_1"))
  64. { // Fail by calling Next too much
  65. itr := db.Iterator(BeginningKey(), EndingKey())
  66. checkValid(t, itr, true)
  67. for i := 0; i < 10; i++ {
  68. checkNext(t, itr, true)
  69. checkValid(t, itr, true)
  70. }
  71. checkNext(t, itr, true)
  72. checkValid(t, itr, true)
  73. checkNext(t, itr, false)
  74. checkValid(t, itr, false)
  75. checkNextPanics(t, itr)
  76. // Once invalid...
  77. checkInvalid(t, itr)
  78. }
  79. })
  80. }
  81. }
  82. func TestDBIteratorEmpty(t *testing.T) {
  83. for backend, _ := range backends {
  84. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  85. db := newTempDB(t, backend)
  86. itr := db.Iterator(BeginningKey(), EndingKey())
  87. checkInvalid(t, itr)
  88. })
  89. }
  90. }
  91. func TestDBIteratorEmptyBeginAfter(t *testing.T) {
  92. for backend, _ := range backends {
  93. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  94. db := newTempDB(t, backend)
  95. itr := db.Iterator(bz("1"), EndingKey())
  96. checkInvalid(t, itr)
  97. })
  98. }
  99. }
  100. func TestDBIteratorNonemptyBeginAfter(t *testing.T) {
  101. for backend, _ := range backends {
  102. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  103. db := newTempDB(t, backend)
  104. db.SetSync(bz("1"), bz("value_1"))
  105. itr := db.Iterator(bz("2"), EndingKey())
  106. checkInvalid(t, itr)
  107. })
  108. }
  109. }