minimalist_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Minimalist test(s) to confirm that basic functionality works.
  2. package main
  3. import (
  4. "os"
  5. "testing"
  6. badger "github.com/dgraph-io/badger/v4"
  7. )
  8. func TestMinimalistSLKVDB(t *testing.T) {
  9. // Open the Badger database located in the /tmp/badger directory.
  10. // It will be created if it doesn't exist.
  11. // db, err := badger.Open(badger.DefaultOptions("/tmp/badger"))
  12. t.Log("Testing basic Badger functionality with a simple database read")
  13. db, err := badger.Open(badger.DefaultOptions("slkvdb/gosl-database.db"))
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. defer func() {
  18. db.Close() // First close database,
  19. // then remove all the directories and files
  20. // Using RemoveAll() function
  21. if err = os.RemoveAll("slkvdb"); err != nil {
  22. t.Fatal(err)
  23. } else {
  24. t.Log("cleaning up test database successful")
  25. }
  26. }()
  27. err = db.Update(func(txn *badger.Txn) error {
  28. err = txn.Set([]byte("answer"), []byte("42"))
  29. return err
  30. })
  31. handle(t, err)
  32. err = db.View(func(txn *badger.Txn) error {
  33. item, err := txn.Get([]byte("answer"))
  34. handle(t, err)
  35. var valCopy []byte
  36. err = item.Value(func(val []byte) error {
  37. // This func with val would only be called if item.Value encounters no error.
  38. // Accessing val here is valid.
  39. t.Logf("(Inside item.Value()) The answer is: %q\n", val)
  40. // Copying or parsing val is valid.
  41. valCopy = append([]byte{}, val...)
  42. return nil
  43. })
  44. handle(t, err)
  45. // You must copy it to use it outside item.Value(...).
  46. t.Logf("(inside db.View()) The answer is: %q\n", valCopy)
  47. // Alternatively, you could also use item.ValueCopy().
  48. valCopy, err = item.ValueCopy(nil)
  49. handle(t, err)
  50. t.Logf("(inside db.View() using ValueCopy) The answer is: %s\n", valCopy)
  51. return nil
  52. })
  53. handle(t, err)
  54. }
  55. func TestRealDataseSLKVDB(t *testing.T) { // Open the Badger database located in the /tmp/badger directory.
  56. t.Log("Testing Badger functionality reading actual database (first 20 entries only)")
  57. db, err := badger.Open(badger.DefaultOptions("../slkvdb/gosl-database.db"))
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. defer db.Close()
  62. err = db.View(func(txn *badger.Txn) error {
  63. opts := badger.DefaultIteratorOptions
  64. opts.PrefetchSize = 10
  65. it := txn.NewIterator(opts)
  66. defer it.Close()
  67. maxItems := 0 // to display only the first N entries (gwyneth 20211102)
  68. for it.Rewind(); it.Valid(); it.Next() {
  69. item := it.Item()
  70. k := item.Key()
  71. err := item.Value(func(v []byte) error {
  72. t.Logf("key=%s, value=%s\n", k, v)
  73. return nil
  74. })
  75. if err != nil {
  76. return err
  77. }
  78. if maxItems > 20 {
  79. break
  80. }
  81. maxItems++
  82. }
  83. t.Logf("%d item(s) read.\n", maxItems)
  84. return nil
  85. })
  86. handle(t, err)
  87. }
  88. // Handles errors for this very simple test.
  89. func handle(t *testing.T, err error) {
  90. if err != nil {
  91. t.Logf("error was: %q\n", err)
  92. }
  93. }