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