facets_test.rb
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require_relative "test_helper"
class TestFacets < Minitest::Unit::TestCase
def setup
super
store [
{name: "Product Show", latitude: 37.7833, longitude: 12.4167, store_id: 1, in_stock: true, color: "blue", price: 21},
{name: "Product Hide", latitude: 29.4167, longitude: -98.5000, store_id: 2, in_stock: false, color: "green", price: 25},
{name: "Product B", latitude: 43.9333, longitude: -122.4667, store_id: 2, in_stock: false, color: "red", price: 5},
{name: "Foo", latitude: 43.9333, longitude: 12.4667, store_id: 3, in_stock: false, color: "yellow", price: 15}
]
end
def test_basic
assert_equal ({1 => 1, 2 => 2}), store_facet(facets: [:store_id])
end
def test_where
assert_equal ({1 => 1}), store_facet(facets: {store_id: {where: {in_stock: true}}})
end
def test_limit
facet = Product.search("Product", facets: {store_id: {limit: 1}}).facets["store_id"]
assert_equal 1, facet["terms"].size
assert_equal 3, facet["total"]
assert_equal 1, facet["other"]
end
def test_ranges
price_ranges = [{to: 10}, {from: 10, to: 20}, {from: 20}]
facet = Product.search("Product", facets: {price: {ranges: price_ranges}}).facets["price"]
assert_equal 3, facet["ranges"].size
assert_equal 10.0, facet["ranges"][0]["to"]
assert_equal 20.0, facet["ranges"][2]["from"]
assert_equal 1, facet["ranges"][0]["count"]
assert_equal 0, facet["ranges"][1]["count"]
assert_equal 2, facet["ranges"][2]["count"]
end
def test_where_no_smart_facets
assert_equal ({2 => 2}), store_facet(where: {color: "red"}, facets: {store_id: {where: {in_stock: false}}})
end
def test_smart_facets
assert_equal ({1 => 1}), store_facet(where: {in_stock: true}, facets: [:store_id], smart_facets: true)
end
def test_smart_facets_where
assert_equal ({2 => 1}), store_facet(where: {color: "red"}, facets: {store_id: {where: {in_stock: false}}}, smart_facets: true)
end
def test_smart_facets_skip_facet
assert_equal ({1 => 1, 2 => 2}), store_facet(where: {store_id: 2}, facets: [:store_id], smart_facets: true)
end
def test_smart_facets_skip_facet_complex
assert_equal ({1 => 1, 2 => 1}), store_facet(where: {store_id: 2, price: {gt: 5}}, facets: [:store_id], smart_facets: true)
end
protected
def store_facet(options)
Hash[ Product.search("Product", options).facets["store_id"]["terms"].map{|v| [v["term"], v["count"]] } ]
end
end