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.

53 lines
979 B

  1. package db
  2. import (
  3. "io/ioutil"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. "github.com/syndtr/goleveldb/leveldb/errors"
  7. )
  8. func TestNew(t *testing.T) {
  9. dir, err := ioutil.TempDir("", "db")
  10. assert.Nil(t, err)
  11. _, err = New(dir)
  12. assert.Nil(t, err)
  13. }
  14. func TestPutAndGet(t *testing.T) {
  15. dir, err := ioutil.TempDir("", "db")
  16. assert.Nil(t, err)
  17. db, err := New(dir)
  18. assert.Nil(t, err)
  19. k := []byte("test key")
  20. v := []byte("test value")
  21. err = db.Put(k, v)
  22. assert.Nil(t, err)
  23. vg, err := db.Get(k)
  24. assert.Nil(t, err)
  25. assert.Equal(t, v, vg)
  26. }
  27. func TestPrefix(t *testing.T) {
  28. dir, err := ioutil.TempDir("", "db")
  29. assert.Nil(t, err)
  30. db, err := New(dir)
  31. assert.Nil(t, err)
  32. dbPrefix := db.WithPrefix([]byte("prefix1"))
  33. err = dbPrefix.Put([]byte("k1"), []byte("v1"))
  34. assert.Nil(t, err)
  35. _, err = db.Get([]byte("k1"))
  36. assert.Equal(t, err, errors.ErrNotFound)
  37. v1, err := dbPrefix.Get([]byte("k1"))
  38. assert.Nil(t, err)
  39. assert.Equal(t, []byte("v1"), v1)
  40. }