search_cop 1.0.6 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/test.yml +42 -0
  3. data/.rubocop.yml +128 -0
  4. data/CHANGELOG.md +36 -5
  5. data/CONTRIBUTING.md +18 -0
  6. data/Gemfile +4 -17
  7. data/README.md +143 -35
  8. data/Rakefile +0 -1
  9. data/docker-compose.yml +18 -0
  10. data/gemfiles/rails5.gemfile +13 -0
  11. data/gemfiles/rails6.gemfile +13 -0
  12. data/lib/search_cop.rb +15 -13
  13. data/lib/search_cop/grammar_parser.rb +3 -4
  14. data/lib/search_cop/hash_parser.rb +23 -17
  15. data/lib/search_cop/helpers.rb +15 -0
  16. data/lib/search_cop/query_builder.rb +2 -4
  17. data/lib/search_cop/query_info.rb +0 -2
  18. data/lib/search_cop/search_scope.rb +8 -5
  19. data/lib/search_cop/version.rb +1 -1
  20. data/lib/search_cop/visitors.rb +0 -2
  21. data/lib/search_cop/visitors/mysql.rb +9 -7
  22. data/lib/search_cop/visitors/postgres.rb +18 -8
  23. data/lib/search_cop/visitors/visitor.rb +13 -6
  24. data/lib/search_cop_grammar.rb +18 -9
  25. data/lib/search_cop_grammar.treetop +6 -4
  26. data/lib/search_cop_grammar/attributes.rb +77 -34
  27. data/lib/search_cop_grammar/nodes.rb +7 -2
  28. data/search_cop.gemspec +9 -10
  29. data/test/and_test.rb +6 -8
  30. data/test/boolean_test.rb +7 -9
  31. data/test/database.yml +4 -1
  32. data/test/date_test.rb +38 -12
  33. data/test/datetime_test.rb +45 -12
  34. data/test/default_operator_test.rb +51 -0
  35. data/test/error_test.rb +2 -4
  36. data/test/float_test.rb +16 -11
  37. data/test/fulltext_test.rb +8 -10
  38. data/test/hash_test.rb +39 -31
  39. data/test/integer_test.rb +9 -11
  40. data/test/not_test.rb +6 -8
  41. data/test/or_test.rb +8 -10
  42. data/test/scope_test.rb +11 -13
  43. data/test/search_cop_test.rb +36 -34
  44. data/test/string_test.rb +67 -19
  45. data/test/test_helper.rb +24 -18
  46. data/test/visitor_test.rb +15 -8
  47. metadata +41 -55
  48. data/.travis.yml +0 -34
  49. data/Appraisals +0 -20
  50. data/gemfiles/3.2.gemfile +0 -26
  51. data/gemfiles/4.0.gemfile +0 -26
  52. data/gemfiles/4.1.gemfile +0 -26
  53. data/gemfiles/4.2.gemfile +0 -26
data/test/database.yml CHANGED
@@ -6,12 +6,15 @@ sqlite:
6
6
  mysql:
7
7
  adapter: mysql2
8
8
  database: search_cop
9
+ host: 127.0.0.1
9
10
  username: root
10
11
  encoding: utf8
11
12
 
12
13
  postgres:
14
+ host: 127.0.0.1
13
15
  adapter: postgresql
14
16
  database: search_cop
15
- username: postgres
17
+ username: search_cop
18
+ password: secret
16
19
  encoding: utf8
17
20
 
data/test/date_test.rb CHANGED
@@ -1,9 +1,8 @@
1
-
2
- require File.expand_path("../test_helper", __FILE__)
1
+ require File.expand_path("test_helper", __dir__)
3
2
 
4
3
  class DateTest < SearchCop::TestCase
5
4
  def test_mapping
6
- product = create(:product, :created_on => Date.parse("2014-05-01"))
5
+ product = create(:product, created_on: Date.parse("2014-05-01"))
7
6
 
8
7
  assert_includes Product.search("created_on: 2014"), product
9
8
  assert_includes Product.search("created_on: 2014-05"), product
@@ -11,61 +10,89 @@ class DateTest < SearchCop::TestCase
11
10
  end
12
11
 
13
12
  def test_anywhere
14
- product = create(:product, :created_on => Date.parse("2014-05-01"))
13
+ product = create(:product, created_on: Date.parse("2014-05-01"))
15
14
 
16
15
  assert_includes Product.search("2014-05-01"), product
17
16
  refute_includes Product.search("2014-05-02"), product
18
17
  end
19
18
 
20
19
  def test_includes
21
- product = create(:product, :created_on => Date.parse("2014-05-01"))
20
+ product = create(:product, created_on: Date.parse("2014-05-01"))
22
21
 
23
22
  assert_includes Product.search("created_on: 2014-05-01"), product
24
23
  refute_includes Product.search("created_on: 2014-05-02"), product
25
24
  end
26
25
 
27
26
  def test_equals
28
- product = create(:product, :created_on => Date.parse("2014-05-01"))
27
+ product = create(:product, created_on: Date.parse("2014-05-01"))
29
28
 
30
29
  assert_includes Product.search("created_on = 2014-05-01"), product
31
30
  refute_includes Product.search("created_on = 2014-05-02"), product
32
31
  end
33
32
 
34
33
  def test_equals_not
35
- product = create(:product, :created_on => Date.parse("2014-05-01"))
34
+ product = create(:product, created_on: Date.parse("2014-05-01"))
36
35
 
37
36
  assert_includes Product.search("created_on != 2014-05-02"), product
38
37
  refute_includes Product.search("created_on != 2014-05-01"), product
39
38
  end
40
39
 
41
40
  def test_greater
42
- product = create(:product, :created_on => Date.parse("2014-05-01"))
41
+ product = create(:product, created_on: Date.parse("2014-05-01"))
43
42
 
44
43
  assert_includes Product.search("created_on > 2014-04-01"), product
45
44
  refute_includes Product.search("created_on > 2014-05-01"), product
46
45
  end
47
46
 
48
47
  def test_greater_equals
49
- product = create(:product, :created_on => Date.parse("2014-05-01"))
48
+ product = create(:product, created_on: Date.parse("2014-05-01"))
50
49
 
51
50
  assert_includes Product.search("created_on >= 2014-05-01"), product
52
51
  refute_includes Product.search("created_on >= 2014-05-02"), product
53
52
  end
54
53
 
55
54
  def test_less
56
- product = create(:product, :created_on => Date.parse("2014-05-01"))
55
+ product = create(:product, created_on: Date.parse("2014-05-01"))
57
56
 
58
57
  assert_includes Product.search("created_on < 2014-05-02"), product
59
58
  refute_includes Product.search("created_on < 2014-05-01"), product
60
59
  end
61
60
 
62
61
  def test_less_equals
63
- product = create(:product, :created_on => Date.parse("2014-05-02"))
62
+ product = create(:product, created_on: Date.parse("2014-05-02"))
64
63
 
65
64
  assert_includes Product.search("created_on <= 2014-05-02"), product
66
65
  refute_includes Product.search("created_on <= 2014-05-01"), product
67
66
  end
68
67
 
68
+ def test_days_ago
69
+ product = create(:product, created_at: 2.days.ago.to_date)
70
+
71
+ assert_includes Product.search("created_at <= '1 day ago'"), product
72
+ refute_includes Product.search("created_at <= '3 days ago'"), product
73
+ end
74
+
75
+ def test_weeks_ago
76
+ product = create(:product, created_at: 2.weeks.ago.to_date)
77
+
78
+ assert_includes Product.search("created_at <= '1 weeks ago'"), product
79
+ refute_includes Product.search("created_at <= '3 weeks ago'"), product
80
+ end
81
+
82
+ def test_months_ago
83
+ product = create(:product, created_at: 2.months.ago.to_date)
84
+
85
+ assert_includes Product.search("created_at <= '1 months ago'"), product
86
+ refute_includes Product.search("created_at <= '3 months ago'"), product
87
+ end
88
+
89
+ def test_years_ago
90
+ product = create(:product, created_at: 2.years.ago.to_date)
91
+
92
+ assert_includes Product.search("created_at <= '1 years ago'"), product
93
+ refute_includes Product.search("created_at <= '3 years ago'"), product
94
+ end
95
+
69
96
  def test_no_overflow
70
97
  assert_nothing_raised do
71
98
  Product.search("created_on: 1000000").to_a
@@ -78,4 +105,3 @@ class DateTest < SearchCop::TestCase
78
105
  end
79
106
  end
80
107
  end
81
-
@@ -1,9 +1,8 @@
1
-
2
- require File.expand_path("../test_helper", __FILE__)
1
+ require File.expand_path("test_helper", __dir__)
3
2
 
4
3
  class DatetimeTest < SearchCop::TestCase
5
4
  def test_mapping
6
- product = create(:product, :created_at => Time.parse("2014-05-01 12:30:30"))
5
+ product = create(:product, created_at: Time.parse("2014-05-01 12:30:30"))
7
6
 
8
7
  assert_includes Product.search("created_at: 2014"), product
9
8
  assert_includes Product.search("created_at: 2014-05"), product
@@ -12,61 +11,96 @@ class DatetimeTest < SearchCop::TestCase
12
11
  end
13
12
 
14
13
  def test_anywhere
15
- product = create(:product, :created_at => Time.parse("2014-05-01"))
14
+ product = create(:product, created_at: Time.parse("2014-05-01"))
16
15
 
17
16
  assert_includes Product.search("2014-05-01"), product
18
17
  refute_includes Product.search("2014-05-02"), product
19
18
  end
20
19
 
21
20
  def test_includes
22
- product = create(:product, :created_at => Time.parse("2014-05-01"))
21
+ product = create(:product, created_at: Time.parse("2014-05-01"))
23
22
 
24
23
  assert_includes Product.search("created_at: 2014-05-01"), product
25
24
  refute_includes Product.search("created_at: 2014-05-02"), product
26
25
  end
27
26
 
28
27
  def test_equals
29
- product = create(:product, :created_at => Time.parse("2014-05-01"))
28
+ product = create(:product, created_at: Time.parse("2014-05-01"))
30
29
 
31
30
  assert_includes Product.search("created_at = 2014-05-01"), product
32
31
  refute_includes Product.search("created_at = 2014-05-02"), product
33
32
  end
34
33
 
35
34
  def test_equals_not
36
- product = create(:product, :created_at => Time.parse("2014-05-01"))
35
+ product = create(:product, created_at: Time.parse("2014-05-01"))
37
36
 
38
37
  assert_includes Product.search("created_at != 2014-05-02"), product
39
38
  refute_includes Product.search("created_at != 2014-05-01"), product
40
39
  end
41
40
 
42
41
  def test_greater
43
- product = create(:product, :created_at => Time.parse("2014-05-01"))
42
+ product = create(:product, created_at: Time.parse("2014-05-01"))
44
43
 
45
44
  assert_includes Product.search("created_at > 2014-04-01"), product
46
45
  refute_includes Product.search("created_at > 2014-05-01"), product
47
46
  end
48
47
 
49
48
  def test_greater_equals
50
- product = create(:product, :created_at => Time.parse("2014-05-01"))
49
+ product = create(:product, created_at: Time.parse("2014-05-01"))
51
50
 
52
51
  assert_includes Product.search("created_at >= 2014-05-01"), product
53
52
  refute_includes Product.search("created_at >= 2014-05-02"), product
54
53
  end
55
54
 
56
55
  def test_less
57
- product = create(:product, :created_at => Time.parse("2014-05-01"))
56
+ product = create(:product, created_at: Time.parse("2014-05-01"))
58
57
 
59
58
  assert_includes Product.search("created_at < 2014-05-02"), product
60
59
  refute_includes Product.search("created_at < 2014-05-01"), product
61
60
  end
62
61
 
63
62
  def test_less_equals
64
- product = create(:product, :created_at => Time.parse("2014-05-02"))
63
+ product = create(:product, created_at: Time.parse("2014-05-02"))
65
64
 
66
65
  assert_includes Product.search("created_at <= 2014-05-02"), product
67
66
  refute_includes Product.search("created_at <= 2014-05-01"), product
68
67
  end
69
68
 
69
+ def test_hours_ago
70
+ product = create(:product, created_at: 5.hours.ago)
71
+
72
+ assert_includes Product.search("created_at <= '4 hours ago'"), product
73
+ refute_includes Product.search("created_at <= '6 hours ago'"), product
74
+ end
75
+
76
+ def test_days_ago
77
+ product = create(:product, created_at: 2.days.ago)
78
+
79
+ assert_includes Product.search("created_at <= '1 day ago'"), product
80
+ refute_includes Product.search("created_at <= '3 days ago'"), product
81
+ end
82
+
83
+ def test_weeks_ago
84
+ product = create(:product, created_at: 2.weeks.ago)
85
+
86
+ assert_includes Product.search("created_at <= '1 weeks ago'"), product
87
+ refute_includes Product.search("created_at <= '3 weeks ago'"), product
88
+ end
89
+
90
+ def test_months_ago
91
+ product = create(:product, created_at: 2.months.ago)
92
+
93
+ assert_includes Product.search("created_at <= '1 months ago'"), product
94
+ refute_includes Product.search("created_at <= '3 months ago'"), product
95
+ end
96
+
97
+ def test_years_ago
98
+ product = create(:product, created_at: 2.years.ago)
99
+
100
+ assert_includes Product.search("created_at <= '1 years ago'"), product
101
+ refute_includes Product.search("created_at <= '3 years ago'"), product
102
+ end
103
+
70
104
  def test_no_overflow
71
105
  assert_nothing_raised do
72
106
  Product.search("created_at: 1000000").to_a
@@ -79,4 +113,3 @@ class DatetimeTest < SearchCop::TestCase
79
113
  end
80
114
  end
81
115
  end
82
-
@@ -0,0 +1,51 @@
1
+ require File.expand_path("test_helper", __dir__)
2
+
3
+ class DefaultOperatorTest < SearchCop::TestCase
4
+ def test_without_default_operator
5
+ avengers = create(:product, title: "Title Avengers", description: "2012")
6
+ inception = create(:product, title: "Title Inception", description: "2010")
7
+
8
+ results = Product.search_multi_columns("Title Avengers")
9
+ assert_includes results, avengers
10
+ refute_includes results, inception
11
+
12
+ results = Product.search_multi_columns("Title AND Avengers")
13
+ assert_includes results, avengers
14
+ refute_includes results, inception
15
+
16
+ results = Product.search_multi_columns("Title OR Avengers")
17
+ assert_includes results, avengers
18
+ assert_includes results, inception
19
+
20
+ results = Product.search(title: "Avengers", description: "2012")
21
+ assert_includes results, avengers
22
+ refute_includes results, inception
23
+ end
24
+
25
+ def test_with_specific_default_operator
26
+ matrix = create(:product, title: "Matrix", description: "1999 Fantasy Sci-fi 2h 30m")
27
+ star_wars = create(:product, title: "Star Wars", description: "2010 Sci-fi Thriller 2h 28m")
28
+
29
+ results = Product.search("Star Wars", default_operator: "AND")
30
+ refute_includes results, matrix
31
+ assert_includes results, star_wars
32
+
33
+ results = Product.search_multi_columns("Matrix movie", default_operator: "OR")
34
+ assert_includes results, matrix
35
+ refute_includes results, star_wars
36
+
37
+ results = Product.search({ title: "Matrix", description: "2000" }, default_operator: :or)
38
+ assert_includes results, matrix
39
+ refute_includes results, star_wars
40
+ end
41
+
42
+ def test_with_invalid_default_operator
43
+ assert_raises SearchCop::UnknownDefaultOperator do
44
+ Product.search_multi_columns("Matrix movie", default_operator: :xpto)
45
+ end
46
+
47
+ assert_raises SearchCop::UnknownDefaultOperator do
48
+ Product.search_multi_columns({ title: "Matrix movie" }, default_operator: :xpto)
49
+ end
50
+ end
51
+ end
data/test/error_test.rb CHANGED
@@ -1,10 +1,9 @@
1
-
2
- require File.expand_path("../test_helper", __FILE__)
1
+ require File.expand_path("test_helper", __dir__)
3
2
 
4
3
  class ErrorTest < SearchCop::TestCase
5
4
  def test_parse_error
6
5
  assert_raises SearchCop::ParseError do
7
- Product.unsafe_search :title => { :unknown_operator => "Value" }
6
+ Product.unsafe_search title: { unknown_operator: "Value" }
8
7
  end
9
8
  end
10
9
 
@@ -14,4 +13,3 @@ class ErrorTest < SearchCop::TestCase
14
13
  end
15
14
  end
16
15
  end
17
-
data/test/float_test.rb CHANGED
@@ -1,67 +1,72 @@
1
-
2
- require File.expand_path("../test_helper", __FILE__)
1
+ require File.expand_path("test_helper", __dir__)
3
2
 
4
3
  class FloatTest < SearchCop::TestCase
5
4
  def test_anywhere
6
- product = create(:product, :price => 10.5, :created_at => Time.now - 1.day)
5
+ product = create(:product, price: 10.5, created_at: Time.now - 1.day)
7
6
 
8
7
  assert_includes Product.search("10.5"), product
9
8
  refute_includes Product.search("11.5"), product
10
9
  end
11
10
 
12
11
  def test_includes
13
- product = create(:product, :price => 10.5)
12
+ product = create(:product, price: 10.5)
14
13
 
15
14
  assert_includes Product.search("price: 10.5"), product
16
15
  refute_includes Product.search("price: 11.5"), product
17
16
  end
18
17
 
19
18
  def test_equals
20
- product = create(:product, :price => 10.5)
19
+ product = create(:product, price: 10.5)
21
20
 
22
21
  assert_includes Product.search("price = 10.5"), product
23
22
  refute_includes Product.search("price = 11.5"), product
24
23
  end
25
24
 
26
25
  def test_equals_not
27
- product = create(:product, :price => 10.5)
26
+ product = create(:product, price: 10.5)
28
27
 
29
28
  assert_includes Product.search("price != 11.5"), product
30
29
  refute_includes Product.search("price != 10.5"), product
31
30
  end
32
31
 
33
32
  def test_greater
34
- product = create(:product, :price => 10.5)
33
+ product = create(:product, price: 10.5)
35
34
 
36
35
  assert_includes Product.search("price > 10.4"), product
37
36
  refute_includes Product.search("price < 10.5"), product
38
37
  end
39
38
 
40
39
  def test_greater_equals
41
- product = create(:product, :price => 10.5)
40
+ product = create(:product, price: 10.5)
42
41
 
43
42
  assert_includes Product.search("price >= 10.5"), product
44
43
  refute_includes Product.search("price >= 10.6"), product
45
44
  end
46
45
 
47
46
  def test_less
48
- product = create(:product, :price => 10.5)
47
+ product = create(:product, price: 10.5)
49
48
 
50
49
  assert_includes Product.search("price < 10.6"), product
51
50
  refute_includes Product.search("price < 10.5"), product
52
51
  end
53
52
 
54
53
  def test_less_equals
55
- product = create(:product, :price => 10.5)
54
+ product = create(:product, price: 10.5)
56
55
 
57
56
  assert_includes Product.search("price <= 10.5"), product
58
57
  refute_includes Product.search("price <= 10.4"), product
59
58
  end
60
59
 
60
+ def test_negative
61
+ product = create(:product, price: -10)
62
+
63
+ assert_includes Product.search("price = -10"), product
64
+ refute_includes Product.search("price = -11"), product
65
+ end
66
+
61
67
  def test_incompatible_datatype
62
68
  assert_raises SearchCop::IncompatibleDatatype do
63
69
  Product.unsafe_search "price: Value"
64
70
  end
65
71
  end
66
72
  end
67
-
@@ -1,13 +1,12 @@
1
-
2
- require File.expand_path("../test_helper", __FILE__)
1
+ require File.expand_path("test_helper", __dir__)
3
2
 
4
3
  class FulltextTest < SearchCop::TestCase
5
4
  def test_complex
6
- product1 = create(:product, :title => "word1")
7
- product2 = create(:product, :title => "word2 word3")
8
- product3 = create(:product, :title => "word2")
5
+ product1 = create(:product, title: "word1")
6
+ product2 = create(:product, title: "word2 word3")
7
+ product3 = create(:product, title: "word2")
9
8
 
10
- results = Product.search("title:word1 OR (title:word2 -title:word3)")
9
+ results = Product.search("word1 OR (title:word2 -word3)")
11
10
 
12
11
  assert_includes results, product1
13
12
  refute_includes results, product2
@@ -15,13 +14,12 @@ class FulltextTest < SearchCop::TestCase
15
14
  end
16
15
 
17
16
  def test_mixed
18
- expected = create(:product, :title => "Expected title", :stock => 1)
19
- rejected = create(:product, :title => "Expected title", :stock => 0)
17
+ expected = create(:product, title: "Expected title", stock: 1)
18
+ rejected = create(:product, title: "Expected title", stock: 0)
20
19
 
21
- results = Product.search("title:Expected title:Title stock > 0")
20
+ results = Product.search("Expected title:Title stock > 0")
22
21
 
23
22
  assert_includes results, expected
24
23
  refute_includes results, rejected
25
24
  end
26
25
  end
27
-
data/test/hash_test.rb CHANGED
@@ -1,13 +1,12 @@
1
-
2
- require File.expand_path("../test_helper", __FILE__)
1
+ require File.expand_path("test_helper", __dir__)
3
2
 
4
3
  class HashTest < SearchCop::TestCase
5
4
  def test_subquery
6
- product1 = create(:product, :title => "Title1", :description => "Description")
7
- product2 = create(:product, :title => "Title2", :description => "Description")
8
- product3 = create(:product, :title => "TItle3", :description => "Description")
5
+ product1 = create(:product, title: "Title1", description: "Description")
6
+ product2 = create(:product, title: "Title2", description: "Description")
7
+ product3 = create(:product, title: "TItle3", description: "Description")
9
8
 
10
- results = Product.search(:or => [{ :query => "Description Title1" }, { :query => "Description Title2" }])
9
+ results = Product.search(or: [{ query: "Description Title1" }, { query: "Description Title2" }])
11
10
 
12
11
  assert_includes results, product1
13
12
  assert_includes results, product2
@@ -15,83 +14,92 @@ class HashTest < SearchCop::TestCase
15
14
  end
16
15
 
17
16
  def test_matches
18
- expected = create(:product, :title => "Expected")
19
- rejected = create(:product, :title => "Rejected")
17
+ expected = create(:product, title: "Expected")
18
+ rejected = create(:product, title: "Rejected")
20
19
 
21
- results = Product.search(:title => { :matches => "Expected" })
20
+ results = Product.search(title: { matches: "Expected" })
22
21
 
23
22
  assert_includes results, expected
24
23
  refute_includes results, rejected
25
24
  end
26
25
 
27
26
  def test_matches_default
28
- expected = create(:product, :title => "Expected")
29
- rejected = create(:product, :title => "Rejected")
27
+ expected = create(:product, title: "Expected")
28
+ rejected = create(:product, title: "Rejected")
30
29
 
31
- results = Product.search(:title => "Expected")
30
+ results = Product.search(title: "Expected")
32
31
 
33
32
  assert_includes results, expected
34
33
  refute_includes results, rejected
35
34
  end
36
35
 
37
36
  def test_eq
38
- expected = create(:product, :title => "Expected")
39
- rejected = create(:product, :title => "Rejected")
37
+ expected = create(:product, title: "Expected")
38
+ rejected = create(:product, title: "Rejected")
40
39
 
41
- results = Product.search(:title => { :eq => "Expected" })
40
+ results = Product.search(title: { eq: "Expected" })
42
41
 
43
42
  assert_includes results, expected
44
43
  refute_includes results, rejected
45
44
  end
46
45
 
47
46
  def test_not_eq
48
- expected = create(:product, :title => "Expected")
49
- rejected = create(:product, :title => "Rejected")
47
+ expected = create(:product, title: "Expected")
48
+ rejected = create(:product, title: "Rejected")
50
49
 
51
- results = Product.search(:title => { :not_eq => "Rejected" })
50
+ results = Product.search(title: { not_eq: "Rejected" })
52
51
 
53
52
  assert_includes results, expected
54
53
  refute_includes results, rejected
55
54
  end
56
55
 
57
56
  def test_gt
58
- expected = create(:product, :stock => 1)
59
- rejected = create(:product, :stock => 0)
57
+ expected = create(:product, stock: 1)
58
+ rejected = create(:product, stock: 0)
60
59
 
61
- results = Product.search(:stock => { :gt => 0 })
60
+ results = Product.search(stock: { gt: 0 })
62
61
 
63
62
  assert_includes results, expected
64
63
  refute_includes results, rejected
65
64
  end
66
65
 
67
66
  def test_gteq
68
- expected = create(:product, :stock => 1)
69
- rejected = create(:product, :stock => 0)
67
+ expected = create(:product, stock: 1)
68
+ rejected = create(:product, stock: 0)
70
69
 
71
- results = Product.search(:stock => { :gteq => 1 })
70
+ results = Product.search(stock: { gteq: 1 })
72
71
 
73
72
  assert_includes results, expected
74
73
  refute_includes results, rejected
75
74
  end
76
75
 
77
76
  def test_lt
78
- expected = create(:product, :stock => 0)
79
- rejected = create(:product, :stock => 1)
77
+ expected = create(:product, stock: 0)
78
+ rejected = create(:product, stock: 1)
80
79
 
81
- results = Product.search(:stock => { :lt => 1 })
80
+ results = Product.search(stock: { lt: 1 })
82
81
 
83
82
  assert_includes results, expected
84
83
  refute_includes results, rejected
85
84
  end
86
85
 
87
86
  def test_lteq
88
- expected = create(:product, :stock => 0)
89
- rejected = create(:product, :stock => 1)
87
+ expected = create(:product, stock: 0)
88
+ rejected = create(:product, stock: 1)
90
89
 
91
- results = Product.search(:stock => { :lteq => 0 })
90
+ results = Product.search(stock: { lteq: 0 })
92
91
 
93
92
  assert_includes results, expected
94
93
  refute_includes results, rejected
95
94
  end
96
- end
97
95
 
96
+ def test_custom_matcher
97
+ expected = create(:product, title: "Expected")
98
+ rejected = create(:product, title: "Rejected")
99
+
100
+ results = Product.search(title: { custom_eq: "Expected" })
101
+
102
+ assert_includes results, expected
103
+ refute_includes results, rejected
104
+ end
105
+ end