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.

129 lines
3.1 KiB

  1. package db
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestPrefixIteratorNoMatchNil(t *testing.T) {
  7. for backend, _ := range backends {
  8. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  9. db := newTempDB(t, backend)
  10. itr := IteratePrefix(db, []byte("2"))
  11. checkInvalid(t, itr)
  12. })
  13. }
  14. }
  15. func TestPrefixIteratorNoMatch1(t *testing.T) {
  16. for backend, _ := range backends {
  17. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  18. db := newTempDB(t, backend)
  19. itr := IteratePrefix(db, []byte("2"))
  20. db.SetSync(bz("1"), bz("value_1"))
  21. checkInvalid(t, itr)
  22. })
  23. }
  24. }
  25. func TestPrefixIteratorMatch2(t *testing.T) {
  26. for backend, _ := range backends {
  27. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  28. db := newTempDB(t, backend)
  29. db.SetSync(bz("2"), bz("value_2"))
  30. itr := IteratePrefix(db, []byte("2"))
  31. checkValid(t, itr, true)
  32. checkItem(t, itr, bz("2"), bz("value_2"))
  33. checkNext(t, itr, false)
  34. // Once invalid...
  35. checkInvalid(t, itr)
  36. })
  37. }
  38. }
  39. func TestPrefixIteratorMatch3(t *testing.T) {
  40. for backend, _ := range backends {
  41. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  42. db := newTempDB(t, backend)
  43. db.SetSync(bz("3"), bz("value_3"))
  44. itr := IteratePrefix(db, []byte("2"))
  45. // Once invalid...
  46. checkInvalid(t, itr)
  47. })
  48. }
  49. }
  50. // Search for a/1, fail by too much Next()
  51. func TestPrefixIteratorMatches1N(t *testing.T) {
  52. for backend, _ := range backends {
  53. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  54. db := newTempDB(t, backend)
  55. db.SetSync(bz("a/1"), bz("value_1"))
  56. db.SetSync(bz("a/3"), bz("value_3"))
  57. itr := IteratePrefix(db, []byte("a/"))
  58. checkValid(t, itr, true)
  59. checkItem(t, itr, bz("a/1"), bz("value_1"))
  60. checkNext(t, itr, true)
  61. checkItem(t, itr, bz("a/3"), bz("value_3"))
  62. // Bad!
  63. checkNext(t, itr, false)
  64. // Once invalid...
  65. checkInvalid(t, itr)
  66. })
  67. }
  68. }
  69. // Search for a/2, fail by too much Next()
  70. func TestPrefixIteratorMatches2N(t *testing.T) {
  71. for backend, _ := range backends {
  72. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  73. db := newTempDB(t, backend)
  74. db.SetSync(bz("a/1"), bz("value_1"))
  75. db.SetSync(bz("a/3"), bz("value_3"))
  76. itr := IteratePrefix(db, []byte("a/"))
  77. checkValid(t, itr, true)
  78. checkItem(t, itr, bz("a/1"), bz("value_1"))
  79. checkNext(t, itr, true)
  80. checkValid(t, itr, true)
  81. checkItem(t, itr, bz("a/3"), bz("value_3"))
  82. // Bad!
  83. checkNext(t, itr, false)
  84. // Once invalid...
  85. checkInvalid(t, itr)
  86. })
  87. }
  88. }
  89. // Search for a/3, fail by too much Next()
  90. func TestPrefixIteratorMatches3N(t *testing.T) {
  91. for backend, _ := range backends {
  92. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  93. db := newTempDB(t, backend)
  94. db.SetSync(bz("a/1"), bz("value_1"))
  95. db.SetSync(bz("a/3"), bz("value_3"))
  96. itr := IteratePrefix(db, []byte("a/"))
  97. checkValid(t, itr, true)
  98. checkItem(t, itr, bz("a/1"), bz("value_1"))
  99. checkNext(t, itr, true)
  100. checkItem(t, itr, bz("a/3"), bz("value_3"))
  101. // Bad!
  102. checkNext(t, itr, false)
  103. // Once invalid...
  104. checkInvalid(t, itr)
  105. })
  106. }
  107. }