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.

93 lines
2.3 KiB

  1. package db
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. // Empty iterator for empty db.
  7. func TestPrefixIteratorNoMatchNil(t *testing.T) {
  8. for backend, _ := range backends {
  9. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  10. db := newTempDB(t, backend)
  11. itr := IteratePrefix(db, []byte("2"))
  12. checkInvalid(t, itr)
  13. })
  14. }
  15. }
  16. // Empty iterator for db populated after iterator created.
  17. func TestPrefixIteratorNoMatch1(t *testing.T) {
  18. for backend, _ := range backends {
  19. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  20. db := newTempDB(t, backend)
  21. itr := IteratePrefix(db, []byte("2"))
  22. db.SetSync(bz("1"), bz("value_1"))
  23. checkInvalid(t, itr)
  24. })
  25. }
  26. }
  27. // Empty iterator for prefix starting after db entry.
  28. func TestPrefixIteratorNoMatch2(t *testing.T) {
  29. for backend, _ := range backends {
  30. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  31. db := newTempDB(t, backend)
  32. db.SetSync(bz("3"), bz("value_3"))
  33. itr := IteratePrefix(db, []byte("4"))
  34. checkInvalid(t, itr)
  35. })
  36. }
  37. }
  38. // Iterator with single val for db with single val, starting from that val.
  39. func TestPrefixIteratorMatch1(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("2"), bz("value_2"))
  44. itr := IteratePrefix(db, bz("2"))
  45. checkValid(t, itr, true)
  46. checkItem(t, itr, bz("2"), bz("value_2"))
  47. checkNext(t, itr, false)
  48. // Once invalid...
  49. checkInvalid(t, itr)
  50. })
  51. }
  52. }
  53. // Iterator with prefix iterates over everything with same prefix.
  54. func TestPrefixIteratorMatches1N(t *testing.T) {
  55. for backend, _ := range backends {
  56. t.Run(fmt.Sprintf("Prefix w/ backend %s", backend), func(t *testing.T) {
  57. db := newTempDB(t, backend)
  58. // prefixed
  59. db.SetSync(bz("a/1"), bz("value_1"))
  60. db.SetSync(bz("a/3"), bz("value_3"))
  61. // not
  62. db.SetSync(bz("b/3"), bz("value_3"))
  63. db.SetSync(bz("a-3"), bz("value_3"))
  64. db.SetSync(bz("a.3"), bz("value_3"))
  65. db.SetSync(bz("abcdefg"), bz("value_3"))
  66. itr := IteratePrefix(db, bz("a/"))
  67. checkValid(t, itr, true)
  68. checkItem(t, itr, bz("a/1"), bz("value_1"))
  69. checkNext(t, itr, true)
  70. checkItem(t, itr, bz("a/3"), bz("value_3"))
  71. // Bad!
  72. checkNext(t, itr, false)
  73. //Once invalid...
  74. checkInvalid(t, itr)
  75. })
  76. }
  77. }