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.

172 lines
3.8 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 checkPrevPanics(t *testing.T, itr Iterator) {
  21. assert.Panics(t, func() { itr.Prev() }, "checkPrevPanics expected panic but didn't")
  22. }
  23. func checkPrev(t *testing.T, itr Iterator, expected bool) {
  24. itr.Prev()
  25. valid := itr.Valid()
  26. assert.Equal(t, expected, valid)
  27. }
  28. func checkItem(t *testing.T, itr Iterator, key []byte, value []byte) {
  29. k, v := itr.Key(), itr.Value()
  30. assert.Exactly(t, key, k)
  31. assert.Exactly(t, value, v)
  32. }
  33. func checkInvalid(t *testing.T, itr Iterator) {
  34. checkValid(t, itr, false)
  35. checkKeyPanics(t, itr)
  36. checkValuePanics(t, itr)
  37. checkNextPanics(t, itr)
  38. checkPrevPanics(t, itr)
  39. }
  40. func checkKeyPanics(t *testing.T, itr Iterator) {
  41. assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't")
  42. }
  43. func checkValuePanics(t *testing.T, itr Iterator) {
  44. assert.Panics(t, func() { itr.Key() }, "checkValuePanics expected panic but didn't")
  45. }
  46. func newTempDB(t *testing.T, backend string) (db DB) {
  47. dir, dirname := cmn.Tempdir("test_go_iterator")
  48. db = NewDB("testdb", backend, dirname)
  49. dir.Close()
  50. return db
  51. }
  52. func TestDBIteratorSingleKey(t *testing.T) {
  53. for backend, _ := range backends {
  54. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  55. db := newTempDB(t, backend)
  56. db.SetSync(bz("1"), bz("value_1"))
  57. itr := db.Iterator()
  58. checkValid(t, itr, true)
  59. checkNext(t, itr, false)
  60. checkValid(t, itr, false)
  61. checkNextPanics(t, itr)
  62. // Once invalid...
  63. checkInvalid(t, itr)
  64. })
  65. }
  66. }
  67. func TestDBIteratorTwoKeys(t *testing.T) {
  68. for backend, _ := range backends {
  69. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  70. db := newTempDB(t, backend)
  71. db.SetSync(bz("1"), bz("value_1"))
  72. db.SetSync(bz("2"), bz("value_1"))
  73. { // Fail by calling Next too much
  74. itr := db.Iterator()
  75. checkValid(t, itr, true)
  76. for i := 0; i < 10; i++ {
  77. checkNext(t, itr, true)
  78. checkValid(t, itr, true)
  79. checkPrev(t, itr, true)
  80. checkValid(t, itr, true)
  81. }
  82. checkNext(t, itr, true)
  83. checkValid(t, itr, true)
  84. checkNext(t, itr, false)
  85. checkValid(t, itr, false)
  86. checkNextPanics(t, itr)
  87. // Once invalid...
  88. checkInvalid(t, itr)
  89. }
  90. { // Fail by calling Prev too much
  91. itr := db.Iterator()
  92. checkValid(t, itr, true)
  93. for i := 0; i < 10; i++ {
  94. checkNext(t, itr, true)
  95. checkValid(t, itr, true)
  96. checkPrev(t, itr, true)
  97. checkValid(t, itr, true)
  98. }
  99. checkPrev(t, itr, false)
  100. checkValid(t, itr, false)
  101. checkPrevPanics(t, itr)
  102. // Once invalid...
  103. checkInvalid(t, itr)
  104. }
  105. })
  106. }
  107. }
  108. func TestDBIteratorEmpty(t *testing.T) {
  109. for backend, _ := range backends {
  110. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  111. db := newTempDB(t, backend)
  112. itr := db.Iterator()
  113. checkInvalid(t, itr)
  114. })
  115. }
  116. }
  117. func TestDBIteratorEmptySeek(t *testing.T) {
  118. for backend, _ := range backends {
  119. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  120. db := newTempDB(t, backend)
  121. itr := db.Iterator()
  122. itr.Seek(bz("1"))
  123. checkInvalid(t, itr)
  124. })
  125. }
  126. }
  127. func TestDBIteratorBadSeek(t *testing.T) {
  128. for backend, _ := range backends {
  129. t.Run(fmt.Sprintf("Backend %s", backend), func(t *testing.T) {
  130. db := newTempDB(t, backend)
  131. db.SetSync(bz("1"), bz("value_1"))
  132. itr := db.Iterator()
  133. itr.Seek(bz("2"))
  134. checkInvalid(t, itr)
  135. })
  136. }
  137. }