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.

211 lines
5.3 KiB

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