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.

48 lines
1.1 KiB

  1. package db
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. "github.com/stretchr/testify/require"
  6. )
  7. func TestMemDbIterator(t *testing.T) {
  8. db := NewMemDB()
  9. keys := make([][]byte, 100)
  10. for i := 0; i < 100; i++ {
  11. keys[i] = []byte{byte(i)}
  12. }
  13. value := []byte{5}
  14. for _, k := range keys {
  15. db.Set(k, value)
  16. }
  17. iter := db.Iterator()
  18. i := 0
  19. for iter.Next() {
  20. assert.Equal(t, db.Get(iter.Key()), iter.Value(), "values dont match for key")
  21. i += 1
  22. }
  23. assert.Equal(t, i, len(db.db), "iterator didnt cover whole db")
  24. }
  25. func TestMemDBClose(t *testing.T) {
  26. db := NewMemDB()
  27. copyDB := func(orig map[string][]byte) map[string][]byte {
  28. copy := make(map[string][]byte)
  29. for k, v := range orig {
  30. copy[k] = v
  31. }
  32. return copy
  33. }
  34. k, v := []byte("foo"), []byte("bar")
  35. db.Set(k, v)
  36. require.Equal(t, db.Get(k), v, "expecting a successful get")
  37. copyBefore := copyDB(db.db)
  38. db.Close()
  39. require.Equal(t, db.Get(k), v, "Close is a noop, expecting a successful get")
  40. copyAfter := copyDB(db.db)
  41. require.Equal(t, copyBefore, copyAfter, "Close is a noop and shouldn't modify any internal data")
  42. }