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.

193 lines
4.7 KiB

  1. package test
  2. import (
  3. "testing"
  4. "github.com/iden3/go-merkletree/db"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. // TestReturnKnownErrIfNotExists checks that the implementation of the
  8. // db.Storage interface returns the expected error in the case that the value
  9. // is not found
  10. func TestReturnKnownErrIfNotExists(t *testing.T, sto db.Storage) {
  11. k := []byte("key")
  12. tx, err := sto.NewTx()
  13. assert.Nil(t, err)
  14. _, err = tx.Get(k)
  15. assert.EqualError(t, err, db.ErrNotFound.Error())
  16. }
  17. // TestStorageInsertGet checks that the implementation of the db.Storage
  18. // interface behaves as expected
  19. func TestStorageInsertGet(t *testing.T, sto db.Storage) {
  20. key := []byte("key")
  21. value := []byte("data")
  22. tx, err := sto.NewTx()
  23. assert.Nil(t, err)
  24. tx.Put(key, value)
  25. v, err := tx.Get(key)
  26. assert.Nil(t, err)
  27. assert.Equal(t, value, v)
  28. assert.Nil(t, tx.Commit())
  29. tx, err = sto.NewTx()
  30. assert.Nil(t, err)
  31. v, err = tx.Get(key)
  32. assert.Nil(t, err)
  33. assert.Equal(t, value, v)
  34. }
  35. // TestStorageWithPrefix checks that the implementation of the db.Storage
  36. // interface behaves as expected for the WithPrefix method
  37. func TestStorageWithPrefix(t *testing.T, sto db.Storage) {
  38. k := []byte{9}
  39. sto1 := sto.WithPrefix([]byte{1})
  40. sto2 := sto.WithPrefix([]byte{2})
  41. // check within tx
  42. sto1tx, err := sto1.NewTx()
  43. assert.Nil(t, err)
  44. sto1tx.Put(k, []byte{4, 5, 6})
  45. v1, err := sto1tx.Get(k)
  46. assert.Nil(t, err)
  47. assert.Equal(t, v1, []byte{4, 5, 6})
  48. assert.Nil(t, sto1tx.Commit())
  49. sto2tx, err := sto2.NewTx()
  50. assert.Nil(t, err)
  51. sto2tx.Put(k, []byte{8, 9})
  52. v2, err := sto2tx.Get(k)
  53. assert.Nil(t, err)
  54. assert.Equal(t, v2, []byte{8, 9})
  55. assert.Nil(t, sto2tx.Commit())
  56. // check outside tx
  57. v1, err = sto1.Get(k)
  58. assert.Nil(t, err)
  59. assert.Equal(t, v1, []byte{4, 5, 6})
  60. v2, err = sto2.Get(k)
  61. assert.Nil(t, err)
  62. assert.Equal(t, v2, []byte{8, 9})
  63. }
  64. // TestIterate checks that the implementation of the db.Storage interface
  65. // behaves as expected for the Iterate method
  66. func TestIterate(t *testing.T, sto db.Storage) {
  67. r := []db.KV{}
  68. lister := func(k []byte, v []byte) (bool, error) {
  69. r = append(r, db.KV{K: db.Clone(k), V: db.Clone(v)})
  70. return true, nil
  71. }
  72. sto1 := sto.WithPrefix([]byte{1})
  73. err := sto1.Iterate(lister)
  74. assert.Nil(t, err)
  75. assert.Equal(t, 0, len(r))
  76. sto1tx, _ := sto1.NewTx()
  77. sto1tx.Put([]byte{1}, []byte{4})
  78. sto1tx.Put([]byte{2}, []byte{5})
  79. sto1tx.Put([]byte{3}, []byte{6})
  80. assert.Nil(t, sto1tx.Commit())
  81. sto2 := sto.WithPrefix([]byte{2})
  82. sto2tx, _ := sto2.NewTx()
  83. sto2tx.Put([]byte{1}, []byte{7})
  84. sto2tx.Put([]byte{2}, []byte{8})
  85. sto2tx.Put([]byte{3}, []byte{9})
  86. assert.Nil(t, sto2tx.Commit())
  87. r = []db.KV{}
  88. err = sto1.Iterate(lister)
  89. assert.Nil(t, err)
  90. assert.Equal(t, 3, len(r))
  91. assert.Equal(t, db.KV{[]byte{1}, []byte{4}}, r[0])
  92. assert.Equal(t, db.KV{[]byte{2}, []byte{5}}, r[1])
  93. assert.Equal(t, db.KV{[]byte{3}, []byte{6}}, r[2])
  94. r = []db.KV{}
  95. err = sto2.Iterate(lister)
  96. assert.Nil(t, err)
  97. assert.Equal(t, 3, len(r))
  98. assert.Equal(t, db.KV{[]byte{1}, []byte{7}}, r[0])
  99. assert.Equal(t, db.KV{[]byte{2}, []byte{8}}, r[1])
  100. assert.Equal(t, db.KV{[]byte{3}, []byte{9}}, r[2])
  101. }
  102. // TestConcatTx checks that the implementation of the db.Storage interface
  103. // behaves as expected
  104. func TestConcatTx(t *testing.T, sto db.Storage) {
  105. k := []byte{9}
  106. sto1 := sto.WithPrefix([]byte{1})
  107. sto2 := sto.WithPrefix([]byte{2})
  108. // check within tx
  109. sto1tx, err := sto1.NewTx()
  110. if err != nil {
  111. panic(err)
  112. }
  113. sto1tx.Put(k, []byte{4, 5, 6})
  114. sto2tx, err := sto2.NewTx()
  115. if err != nil {
  116. panic(err)
  117. }
  118. sto2tx.Put(k, []byte{8, 9})
  119. sto1tx.Add(sto2tx)
  120. assert.Nil(t, sto1tx.Commit())
  121. // check outside tx
  122. v1, err := sto1.Get(k)
  123. assert.Nil(t, err)
  124. assert.Equal(t, v1, []byte{4, 5, 6})
  125. v2, err := sto2.Get(k)
  126. assert.Nil(t, err)
  127. assert.Equal(t, v2, []byte{8, 9})
  128. }
  129. // TestList checks that the implementation of the db.Storage interface behaves
  130. // as expected
  131. func TestList(t *testing.T, sto db.Storage) {
  132. sto1 := sto.WithPrefix([]byte{1})
  133. r1, err := sto1.List(100)
  134. assert.Nil(t, err)
  135. assert.Equal(t, 0, len(r1))
  136. sto1tx, _ := sto1.NewTx()
  137. sto1tx.Put([]byte{1}, []byte{4})
  138. sto1tx.Put([]byte{2}, []byte{5})
  139. sto1tx.Put([]byte{3}, []byte{6})
  140. assert.Nil(t, sto1tx.Commit())
  141. sto2 := sto.WithPrefix([]byte{2})
  142. sto2tx, _ := sto2.NewTx()
  143. sto2tx.Put([]byte{1}, []byte{7})
  144. sto2tx.Put([]byte{2}, []byte{8})
  145. sto2tx.Put([]byte{3}, []byte{9})
  146. assert.Nil(t, sto2tx.Commit())
  147. r, err := sto1.List(100)
  148. assert.Nil(t, err)
  149. assert.Equal(t, 3, len(r))
  150. assert.Equal(t, r[0], db.KV{[]byte{1}, []byte{4}})
  151. assert.Equal(t, r[1], db.KV{[]byte{2}, []byte{5}})
  152. assert.Equal(t, r[2], db.KV{[]byte{3}, []byte{6}})
  153. r, err = sto1.List(2)
  154. assert.Nil(t, err)
  155. assert.Equal(t, 2, len(r))
  156. assert.Equal(t, r[0], db.KV{[]byte{1}, []byte{4}})
  157. assert.Equal(t, r[1], db.KV{[]byte{2}, []byte{5}})
  158. }