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.

136 lines
3.3 KiB

cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
cleanup: Reduce and normalize import path aliasing. (#6975) The code in the Tendermint repository makes heavy use of import aliasing. This is made necessary by our extensive reuse of common base package names, and by repetition of similar names across different subdirectories. Unfortunately we have not been very consistent about which packages we alias in various circumstances, and the aliases we use vary. In the spirit of the advice in the style guide and https://github.com/golang/go/wiki/CodeReviewComments#imports, his change makes an effort to clean up and normalize import aliasing. This change makes no API or behavioral changes. It is a pure cleanup intended o help make the code more readable to developers (including myself) trying to understand what is being imported where. Only unexported names have been modified, and the changes were generated and applied mechanically with gofmt -r and comby, respecting the lexical and syntactic rules of Go. Even so, I did not fix every inconsistency. Where the changes would be too disruptive, I left it alone. The principles I followed in this cleanup are: - Remove aliases that restate the package name. - Remove aliases where the base package name is unambiguous. - Move overly-terse abbreviations from the import to the usage site. - Fix lexical issues (remove underscores, remove capitalization). - Fix import groupings to more closely match the style guide. - Group blank (side-effecting) imports and ensure they are commented. - Add aliases to multiple imports with the same base package name.
3 years ago
  1. package kv_test
  2. import (
  3. "context"
  4. "fmt"
  5. "testing"
  6. "github.com/stretchr/testify/require"
  7. dbm "github.com/tendermint/tm-db"
  8. abci "github.com/tendermint/tendermint/abci/types"
  9. "github.com/tendermint/tendermint/internal/pubsub/query"
  10. blockidxkv "github.com/tendermint/tendermint/internal/state/indexer/block/kv"
  11. "github.com/tendermint/tendermint/types"
  12. )
  13. func TestBlockIndexer(t *testing.T) {
  14. store := dbm.NewPrefixDB(dbm.NewMemDB(), []byte("block_events"))
  15. indexer := blockidxkv.New(store)
  16. require.NoError(t, indexer.Index(types.EventDataNewBlockHeader{
  17. Header: types.Header{Height: 1},
  18. ResultFinalizeBlock: abci.ResponseFinalizeBlock{
  19. BlockEvents: []abci.Event{
  20. {
  21. Type: "finalize_event1",
  22. Attributes: []abci.EventAttribute{
  23. {
  24. Key: "proposer",
  25. Value: "FCAA001",
  26. Index: true,
  27. },
  28. },
  29. },
  30. {
  31. Type: "finalize_event2",
  32. Attributes: []abci.EventAttribute{
  33. {
  34. Key: "foo",
  35. Value: "100",
  36. Index: true,
  37. },
  38. },
  39. },
  40. },
  41. },
  42. }))
  43. for i := 2; i < 12; i++ {
  44. var index bool
  45. if i%2 == 0 {
  46. index = true
  47. }
  48. require.NoError(t, indexer.Index(types.EventDataNewBlockHeader{
  49. Header: types.Header{Height: int64(i)},
  50. ResultFinalizeBlock: abci.ResponseFinalizeBlock{
  51. BlockEvents: []abci.Event{
  52. {
  53. Type: "finalize_event1",
  54. Attributes: []abci.EventAttribute{
  55. {
  56. Key: "proposer",
  57. Value: "FCAA001",
  58. Index: true,
  59. },
  60. },
  61. },
  62. {
  63. Type: "finalize_event2",
  64. Attributes: []abci.EventAttribute{
  65. {
  66. Key: "foo",
  67. Value: fmt.Sprintf("%d", i),
  68. Index: index,
  69. },
  70. },
  71. },
  72. },
  73. },
  74. }))
  75. }
  76. testCases := map[string]struct {
  77. q *query.Query
  78. results []int64
  79. }{
  80. "block.height = 100": {
  81. q: query.MustCompile(`block.height = 100`),
  82. results: []int64{},
  83. },
  84. "block.height = 5": {
  85. q: query.MustCompile(`block.height = 5`),
  86. results: []int64{5},
  87. },
  88. "begin_event.key1 = 'value1'": {
  89. q: query.MustCompile(`finalize_event1.key1 = 'value1'`),
  90. results: []int64{},
  91. },
  92. "begin_event.proposer = 'FCAA001'": {
  93. q: query.MustCompile(`finalize_event1.proposer = 'FCAA001'`),
  94. results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
  95. },
  96. "end_event.foo <= 5": {
  97. q: query.MustCompile(`finalize_event2.foo <= 5`),
  98. results: []int64{2, 4},
  99. },
  100. "end_event.foo >= 100": {
  101. q: query.MustCompile(`finalize_event2.foo >= 100`),
  102. results: []int64{1},
  103. },
  104. "block.height > 2 AND finalize_event2.foo <= 8": {
  105. q: query.MustCompile(`block.height > 2 AND finalize_event2.foo <= 8`),
  106. results: []int64{4, 6, 8},
  107. },
  108. "begin_event.proposer CONTAINS 'FFFFFFF'": {
  109. q: query.MustCompile(`finalize_event1.proposer CONTAINS 'FFFFFFF'`),
  110. results: []int64{},
  111. },
  112. "begin_event.proposer CONTAINS 'FCAA001'": {
  113. q: query.MustCompile(`finalize_event1.proposer CONTAINS 'FCAA001'`),
  114. results: []int64{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11},
  115. },
  116. }
  117. for name, tc := range testCases {
  118. tc := tc
  119. t.Run(name, func(t *testing.T) {
  120. ctx, cancel := context.WithCancel(context.Background())
  121. defer cancel()
  122. results, err := indexer.Search(ctx, tc.q)
  123. require.NoError(t, err)
  124. require.Equal(t, tc.results, results)
  125. })
  126. }
  127. }