tree_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package search
  2. import (
  3. "testing"
  4. "github.com/stretchr/testify/assert"
  5. )
  6. type mockedRoute struct {
  7. route string
  8. value int
  9. }
  10. func TestSearch(t *testing.T) {
  11. routes := []mockedRoute{
  12. {"/", 1},
  13. {"/api", 2},
  14. {"/img", 3},
  15. {"/:layer1", 4},
  16. {"/api/users", 5},
  17. {"/img/jpgs", 6},
  18. {"/img/jpgs", 7},
  19. {"/api/:layer2", 8},
  20. {"/:layer1/:layer2", 9},
  21. {"/:layer1/:layer2/users", 10},
  22. }
  23. tests := []struct {
  24. query string
  25. expect int
  26. params map[string]string
  27. contains bool
  28. }{
  29. {
  30. query: "",
  31. contains: false,
  32. },
  33. {
  34. query: "/",
  35. expect: 1,
  36. contains: true,
  37. },
  38. {
  39. query: "/wildcard",
  40. expect: 4,
  41. params: map[string]string{
  42. "layer1": "wildcard",
  43. },
  44. contains: true,
  45. },
  46. {
  47. query: "/wildcard/",
  48. expect: 4,
  49. params: map[string]string{
  50. "layer1": "wildcard",
  51. },
  52. contains: true,
  53. },
  54. {
  55. query: "/a/b/c",
  56. contains: false,
  57. },
  58. {
  59. query: "/a/b",
  60. expect: 9,
  61. params: map[string]string{
  62. "layer1": "a",
  63. "layer2": "b",
  64. },
  65. contains: true,
  66. },
  67. {
  68. query: "/a/b/",
  69. expect: 9,
  70. params: map[string]string{
  71. "layer1": "a",
  72. "layer2": "b",
  73. },
  74. contains: true,
  75. },
  76. {
  77. query: "/a/b/users",
  78. expect: 10,
  79. params: map[string]string{
  80. "layer1": "a",
  81. "layer2": "b",
  82. },
  83. contains: true,
  84. },
  85. }
  86. for _, test := range tests {
  87. t.Run(test.query, func(t *testing.T) {
  88. tree := NewTree()
  89. for _, r := range routes {
  90. tree.Add(r.route, r.value)
  91. }
  92. result, ok := tree.Search(test.query)
  93. assert.Equal(t, test.contains, ok)
  94. if ok {
  95. actual := result.Item.(int)
  96. assert.EqualValues(t, test.params, result.Params)
  97. assert.Equal(t, test.expect, actual)
  98. }
  99. })
  100. }
  101. }
  102. func TestStrictSearch(t *testing.T) {
  103. routes := []mockedRoute{
  104. {"/api/users", 1},
  105. {"/api/:layer", 2},
  106. }
  107. query := "/api/users"
  108. tree := NewTree()
  109. for _, r := range routes {
  110. tree.Add(r.route, r.value)
  111. }
  112. for i := 0; i < 1000; i++ {
  113. result, ok := tree.Search(query)
  114. assert.True(t, ok)
  115. assert.Equal(t, 1, result.Item.(int))
  116. }
  117. }
  118. func TestStrictSearchSibling(t *testing.T) {
  119. routes := []mockedRoute{
  120. {"/api/:user/profile/name", 1},
  121. {"/api/:user/profile", 2},
  122. {"/api/:user/name", 3},
  123. {"/api/:layer", 4},
  124. }
  125. query := "/api/123/name"
  126. tree := NewTree()
  127. for _, r := range routes {
  128. tree.Add(r.route, r.value)
  129. }
  130. for i := 0; i < 1000; i++ {
  131. result, ok := tree.Search(query)
  132. assert.True(t, ok)
  133. assert.Equal(t, 3, result.Item.(int))
  134. }
  135. }
  136. func TestAddDuplicate(t *testing.T) {
  137. tree := NewTree()
  138. err := tree.Add("/a/b", 1)
  139. assert.Nil(t, err)
  140. err = tree.Add("/a/b", 2)
  141. assert.Equal(t, ErrDupItem, err)
  142. err = tree.Add("/a/b/", 2)
  143. assert.Equal(t, ErrDupItem, err)
  144. }
  145. func TestPlain(t *testing.T) {
  146. tree := NewTree()
  147. err := tree.Add("/a/b", 1)
  148. assert.Nil(t, err)
  149. err = tree.Add("/a/c", 2)
  150. assert.Nil(t, err)
  151. _, ok := tree.Search("/a/d")
  152. assert.False(t, ok)
  153. }
  154. func TestSearchWithDoubleSlashes(t *testing.T) {
  155. tree := NewTree()
  156. err := tree.Add("//a", 1)
  157. assert.Error(t, ErrDupSlash, err)
  158. }
  159. func TestSearchInvalidRoute(t *testing.T) {
  160. tree := NewTree()
  161. err := tree.Add("", 1)
  162. assert.Equal(t, ErrNotFromRoot, err)
  163. err = tree.Add("bad", 1)
  164. assert.Equal(t, ErrNotFromRoot, err)
  165. }
  166. func TestSearchInvalidItem(t *testing.T) {
  167. tree := NewTree()
  168. err := tree.Add("/", nil)
  169. assert.Equal(t, ErrEmptyItem, err)
  170. }