relation_test.rb
1.66 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
require_relative "test_helper"
class RelationTest < Minitest::Test
def test_loaded
Product.search_index.refresh
products = Product.search("*")
refute products.loaded?
assert_equal 0, products.count
assert products.loaded?
error = assert_raises(Searchkick::Error) do
products.limit!(2)
end
assert_equal "Relation loaded", error.message
end
def test_clone
products = Product.search("*")
assert_equal 10, products.limit(10).limit_value
assert_equal 10000, products.limit_value
end
def test_only
assert_equal 10, Product.search("*").limit(10).only(:limit).limit_value
end
def test_except
assert_equal 10000, Product.search("*").limit(10).except(:limit).limit_value
end
# TODO call pluck on Active Record query
# currently uses pluck from Active Support enumerable
def test_pluck
store_names ["Product A", "Product B"]
assert_equal ["Product A", "Product B"], Product.search("product").pluck(:name).sort
end
def test_model
assert_equal Product, Product.search("product").model
assert_nil Searchkick.search("product").model
end
def test_klass
assert_equal Product, Product.search("product").klass
assert_nil Searchkick.search("product").klass
end
def test_respond_to
relation = Product.search("product")
assert relation.respond_to?(:page)
assert relation.respond_to?(:response)
assert relation.respond_to?(:size)
refute relation.respond_to?(:hello)
refute relation.loaded?
end
# TODO uncomment in 6.0
# def test_to_yaml
# store_names ["Product A", "Product B"]
# assert_equal Product.all.to_yaml, Product.search("product").to_yaml
# end
end