Plan stuff. Log stuff.
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.

215 lines
5.4 KiB

5 years ago
  1. package bolt
  2. import (
  3. "fmt"
  4. "github.com/stretchr/testify/assert"
  5. "go.etcd.io/bbolt"
  6. "testing"
  7. "time"
  8. )
  9. func TestIndex(t *testing.T) {
  10. ns := time.Now().Nanosecond()
  11. path := fmt.Sprintf("/tmp/stufflog_idx_test_%s_%d.db", time.Now().Format("20060102150405"), ns)
  12. db, err := bbolt.Open(path, 0744, nil)
  13. if err != nil {
  14. t.Fatal("Failed to open db:", err)
  15. }
  16. idx, err := newIndex(db, "idx", "stuff", "things")
  17. idx2, err := newIndex(db, "idx", "stuff2", "things")
  18. testId := []byte("Test")
  19. test2Id := []byte("Test2")
  20. test3Id := []byte("Test3")
  21. test4Id := []byte("Test4")
  22. t.Run("Insert Initial", func(t *testing.T) {
  23. _ = db.Update(func(tx *bbolt.Tx) error {
  24. itx := idx.WithTx(tx)
  25. itx2 := idx2.WithTx(tx)
  26. assert.NoError(t, itx.Set(testId, "A", "B", "C", "D", "E"))
  27. assert.NoError(t, itx.Set(test2Id, "B", "C"))
  28. assert.NoError(t, itx.Set(test3Id, "A", "C", "D"))
  29. assert.NoError(t, itx.Set(test4Id, "A", "C", "D"))
  30. assert.NoError(t, itx2.Set(testId, "user_0"))
  31. assert.NoError(t, itx2.Set(test2Id, "user_1"))
  32. assert.NoError(t, itx2.Set(test3Id, "user_2"))
  33. assert.NoError(t, itx.Set(testId, "A", "B", "C"))
  34. assert.NoError(t, itx.Set(testId, "A", "C"))
  35. assert.NoError(t, itx.Set(test4Id))
  36. return nil
  37. })
  38. })
  39. if t.Failed() {
  40. return
  41. }
  42. t.Run("Check Initial", func(t *testing.T) {
  43. _ = db.View(func(tx *bbolt.Tx) error {
  44. itx := idx.WithTx(tx)
  45. itx2 := idx2.WithTx(tx)
  46. ids, err := itx.Get("A")
  47. assert.NoError(t, err)
  48. assert.Equal(t, [][]byte{testId, test3Id}, ids)
  49. ids, err = itx.Get("B")
  50. assert.NoError(t, err)
  51. assert.Equal(t, [][]byte{test2Id}, ids)
  52. ids, err = itx.Get("C")
  53. assert.NoError(t, err)
  54. assert.Equal(t, [][]byte{testId, test2Id, test3Id}, ids)
  55. ids, err = itx.Get("D")
  56. assert.NoError(t, err)
  57. assert.Equal(t, [][]byte{test3Id}, ids)
  58. ids, err = itx2.Get("user_0")
  59. assert.NoError(t, err)
  60. assert.Equal(t, [][]byte{testId}, ids)
  61. ids, err = itx2.Get("user_1")
  62. assert.NoError(t, err)
  63. assert.Equal(t, [][]byte{test2Id}, ids)
  64. ids, err = itx2.Get("user_2")
  65. assert.NoError(t, err)
  66. assert.Equal(t, [][]byte{test3Id}, ids)
  67. return nil
  68. })
  69. })
  70. if t.Failed() {
  71. return
  72. }
  73. t.Run("Check Initial Reverse", func(t *testing.T) {
  74. _ = db.View(func(tx *bbolt.Tx) error {
  75. itx := idx.WithTx(tx)
  76. itx2 := idx2.WithTx(tx)
  77. values, err := itx.Reverse(testId)
  78. assert.NoError(t, err)
  79. assert.Equal(t, []string{"A", "C"}, values)
  80. values, err = itx.Reverse(test2Id)
  81. assert.NoError(t, err)
  82. assert.Equal(t, []string{"B", "C"}, values)
  83. values, err = itx.Reverse(test3Id)
  84. assert.NoError(t, err)
  85. assert.Equal(t, []string{"A", "C", "D"}, values)
  86. values, err = itx.Reverse(test4Id)
  87. assert.NoError(t, err)
  88. assert.Empty(t, values)
  89. values, err = itx2.Reverse(testId)
  90. assert.NoError(t, err)
  91. assert.Equal(t, []string{"user_0"}, values)
  92. values, err = itx2.Reverse(test2Id)
  93. assert.NoError(t, err)
  94. assert.Equal(t, []string{"user_1"}, values)
  95. values, err = itx2.Reverse(test3Id)
  96. assert.NoError(t, err)
  97. assert.Equal(t, []string{"user_2"}, values)
  98. return nil
  99. })
  100. })
  101. if t.Failed() {
  102. return
  103. }
  104. t.Run("Nuke 'n Pave", func(t *testing.T) {
  105. _ = db.Update(func(tx *bbolt.Tx) error {
  106. assert.NoError(t, idx.Reset(tx))
  107. assert.NoError(t, idx2.Reset(tx))
  108. itx := idx.WithTx(tx)
  109. itx2 := idx2.WithTx(tx)
  110. assert.NoError(t, itx.Set(testId, "pave"))
  111. assert.NoError(t, itx.Set(test2Id, "pave"))
  112. assert.NoError(t, itx.Set(test3Id, "pave"))
  113. assert.NoError(t, itx2.Set(testId, "pave2"))
  114. assert.NoError(t, itx2.Set(test3Id, "pave2"))
  115. return nil
  116. })
  117. })
  118. if t.Failed() {
  119. return
  120. }
  121. t.Run("Check Nuke 'n Pave", func(t *testing.T) {
  122. _ = db.View(func(tx *bbolt.Tx) error {
  123. itx := idx.WithTx(tx)
  124. itx2 := idx2.WithTx(tx)
  125. ids, err := itx.Get("A")
  126. assert.NoError(t, err)
  127. assert.Empty(t, ids)
  128. ids, err = itx.Get("B")
  129. assert.NoError(t, err)
  130. assert.Empty(t, ids)
  131. ids, err = itx.Get("C")
  132. assert.NoError(t, err)
  133. assert.Empty(t, ids)
  134. ids, err = itx.Get("D")
  135. assert.NoError(t, err)
  136. assert.Empty(t, ids)
  137. ids, err = itx2.Get("user_0")
  138. assert.NoError(t, err)
  139. assert.Empty(t, ids)
  140. ids, err = itx2.Get("user_1")
  141. assert.NoError(t, err)
  142. assert.Empty(t, ids)
  143. ids, err = itx2.Get("user_2")
  144. assert.NoError(t, err)
  145. assert.Empty(t, ids)
  146. ids, err = itx.Get("pave")
  147. assert.NoError(t, err)
  148. assert.Equal(t, [][]byte{testId, test2Id, test3Id}, ids)
  149. ids, err = itx2.Get("pave2")
  150. assert.NoError(t, err)
  151. assert.Equal(t, [][]byte{testId, test3Id}, ids)
  152. return nil
  153. })
  154. })
  155. if t.Failed() {
  156. return
  157. }
  158. t.Run("Check Nuke 'n Pave Reverse", func(t *testing.T) {
  159. _ = db.View(func(tx *bbolt.Tx) error {
  160. itx := idx.WithTx(tx)
  161. itx2 := idx2.WithTx(tx)
  162. values, err := itx.Reverse(testId)
  163. assert.NoError(t, err)
  164. assert.Equal(t, []string{"pave"}, values)
  165. values, err = itx.Reverse(test2Id)
  166. assert.NoError(t, err)
  167. assert.Equal(t, []string{"pave"}, values)
  168. values, err = itx.Reverse(test3Id)
  169. assert.NoError(t, err)
  170. assert.Equal(t, []string{"pave"}, values)
  171. values, err = itx2.Reverse(testId)
  172. assert.NoError(t, err)
  173. assert.Equal(t, []string{"pave2"}, values)
  174. values, err = itx2.Reverse(test2Id)
  175. assert.NoError(t, err)
  176. assert.Empty(t, values)
  177. values, err = itx2.Reverse(test3Id)
  178. assert.NoError(t, err)
  179. assert.Equal(t, []string{"pave2"}, values)
  180. return nil
  181. })
  182. })
  183. if t.Failed() {
  184. return
  185. }
  186. }