facets_builder_test.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. // Copyright (c) 2014 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package search
  15. import (
  16. "reflect"
  17. "testing"
  18. )
  19. func TestTermFacetResultsMerge(t *testing.T) {
  20. fr1 := &FacetResult{
  21. Field: "type",
  22. Total: 100,
  23. Missing: 25,
  24. Other: 25,
  25. Terms: []*TermFacet{
  26. {
  27. Term: "blog",
  28. Count: 25,
  29. },
  30. {
  31. Term: "comment",
  32. Count: 24,
  33. },
  34. {
  35. Term: "feedback",
  36. Count: 1,
  37. },
  38. },
  39. }
  40. fr1Only := &FacetResult{
  41. Field: "category",
  42. Total: 97,
  43. Missing: 22,
  44. Other: 15,
  45. Terms: []*TermFacet{
  46. {
  47. Term: "clothing",
  48. Count: 35,
  49. },
  50. {
  51. Term: "electronics",
  52. Count: 25,
  53. },
  54. },
  55. }
  56. frs1 := FacetResults{
  57. "types": fr1,
  58. "categories": fr1Only,
  59. }
  60. fr2 := &FacetResult{
  61. Field: "type",
  62. Total: 100,
  63. Missing: 25,
  64. Other: 25,
  65. Terms: []*TermFacet{
  66. {
  67. Term: "blog",
  68. Count: 25,
  69. },
  70. {
  71. Term: "comment",
  72. Count: 22,
  73. },
  74. {
  75. Term: "flag",
  76. Count: 3,
  77. },
  78. },
  79. }
  80. frs2 := FacetResults{
  81. "types": fr2,
  82. }
  83. expectedFr := &FacetResult{
  84. Field: "type",
  85. Total: 200,
  86. Missing: 50,
  87. Other: 51,
  88. Terms: []*TermFacet{
  89. {
  90. Term: "blog",
  91. Count: 50,
  92. },
  93. {
  94. Term: "comment",
  95. Count: 46,
  96. },
  97. {
  98. Term: "flag",
  99. Count: 3,
  100. },
  101. },
  102. }
  103. expectedFrs := FacetResults{
  104. "types": expectedFr,
  105. "categories": fr1Only,
  106. }
  107. frs1.Merge(frs2)
  108. frs1.Fixup("types", 3)
  109. if !reflect.DeepEqual(frs1, expectedFrs) {
  110. t.Errorf("expected %v, got %v", expectedFrs, frs1)
  111. }
  112. }
  113. func TestNumericFacetResultsMerge(t *testing.T) {
  114. lowmed := 3.0
  115. medhi := 6.0
  116. hihigher := 9.0
  117. // why second copy? the pointers may be different, but values the same
  118. lowmed2 := 3.0
  119. medhi2 := 6.0
  120. hihigher2 := 9.0
  121. fr1 := &FacetResult{
  122. Field: "rating",
  123. Total: 100,
  124. Missing: 25,
  125. Other: 25,
  126. NumericRanges: []*NumericRangeFacet{
  127. {
  128. Name: "low",
  129. Max: &lowmed,
  130. Count: 25,
  131. },
  132. {
  133. Name: "med",
  134. Count: 24,
  135. Max: &lowmed,
  136. Min: &medhi,
  137. },
  138. {
  139. Name: "hi",
  140. Count: 1,
  141. Min: &medhi,
  142. Max: &hihigher,
  143. },
  144. },
  145. }
  146. frs1 := FacetResults{
  147. "ratings": fr1,
  148. }
  149. fr2 := &FacetResult{
  150. Field: "rating",
  151. Total: 100,
  152. Missing: 25,
  153. Other: 25,
  154. NumericRanges: []*NumericRangeFacet{
  155. {
  156. Name: "low",
  157. Max: &lowmed2,
  158. Count: 25,
  159. },
  160. {
  161. Name: "med",
  162. Max: &lowmed2,
  163. Min: &medhi2,
  164. Count: 22,
  165. },
  166. {
  167. Name: "highest",
  168. Min: &hihigher2,
  169. Count: 3,
  170. },
  171. },
  172. }
  173. frs2 := FacetResults{
  174. "ratings": fr2,
  175. }
  176. expectedFr := &FacetResult{
  177. Field: "rating",
  178. Total: 200,
  179. Missing: 50,
  180. Other: 51,
  181. NumericRanges: []*NumericRangeFacet{
  182. {
  183. Name: "low",
  184. Count: 50,
  185. Max: &lowmed,
  186. },
  187. {
  188. Name: "med",
  189. Max: &lowmed,
  190. Min: &medhi,
  191. Count: 46,
  192. },
  193. {
  194. Name: "highest",
  195. Min: &hihigher,
  196. Count: 3,
  197. },
  198. },
  199. }
  200. expectedFrs := FacetResults{
  201. "ratings": expectedFr,
  202. }
  203. frs1.Merge(frs2)
  204. frs1.Fixup("ratings", 3)
  205. if !reflect.DeepEqual(frs1, expectedFrs) {
  206. t.Errorf("expected %#v, got %#v", expectedFrs, frs1)
  207. }
  208. }
  209. func TestDateFacetResultsMerge(t *testing.T) {
  210. lowmed := "2010-01-01"
  211. medhi := "2011-01-01"
  212. hihigher := "2012-01-01"
  213. // why second copy? the pointer are to strings done by date time parsing
  214. // inside the facet generation, so comparing pointers will not work
  215. lowmed2 := "2010-01-01"
  216. medhi2 := "2011-01-01"
  217. hihigher2 := "2012-01-01"
  218. fr1 := &FacetResult{
  219. Field: "birthday",
  220. Total: 100,
  221. Missing: 25,
  222. Other: 25,
  223. DateRanges: []*DateRangeFacet{
  224. {
  225. Name: "low",
  226. End: &lowmed,
  227. Count: 25,
  228. },
  229. {
  230. Name: "med",
  231. Count: 24,
  232. Start: &lowmed,
  233. End: &medhi,
  234. },
  235. {
  236. Name: "hi",
  237. Count: 1,
  238. Start: &medhi,
  239. End: &hihigher,
  240. },
  241. },
  242. }
  243. frs1 := FacetResults{
  244. "birthdays": fr1,
  245. }
  246. fr2 := &FacetResult{
  247. Field: "birthday",
  248. Total: 100,
  249. Missing: 25,
  250. Other: 25,
  251. DateRanges: []*DateRangeFacet{
  252. {
  253. Name: "low",
  254. End: &lowmed2,
  255. Count: 25,
  256. },
  257. {
  258. Name: "med",
  259. Start: &lowmed2,
  260. End: &medhi2,
  261. Count: 22,
  262. },
  263. {
  264. Name: "highest",
  265. Start: &hihigher2,
  266. Count: 3,
  267. },
  268. },
  269. }
  270. frs2 := FacetResults{
  271. "birthdays": fr2,
  272. }
  273. expectedFr := &FacetResult{
  274. Field: "birthday",
  275. Total: 200,
  276. Missing: 50,
  277. Other: 51,
  278. DateRanges: []*DateRangeFacet{
  279. {
  280. Name: "low",
  281. Count: 50,
  282. End: &lowmed,
  283. },
  284. {
  285. Name: "med",
  286. Start: &lowmed,
  287. End: &medhi,
  288. Count: 46,
  289. },
  290. {
  291. Name: "highest",
  292. Start: &hihigher,
  293. Count: 3,
  294. },
  295. },
  296. }
  297. expectedFrs := FacetResults{
  298. "birthdays": expectedFr,
  299. }
  300. frs1.Merge(frs2)
  301. frs1.Fixup("birthdays", 3)
  302. if !reflect.DeepEqual(frs1, expectedFrs) {
  303. t.Errorf("expected %#v, got %#v", expectedFrs, frs1)
  304. }
  305. }