search_cop 1.0.9 → 1.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/test.yml +50 -0
- data/.rubocop.yml +134 -0
- data/CHANGELOG.md +29 -7
- data/Gemfile +4 -17
- data/README.md +69 -3
- data/Rakefile +0 -1
- data/docker-compose.yml +18 -0
- data/gemfiles/rails5.gemfile +13 -0
- data/gemfiles/rails6.gemfile +13 -0
- data/gemfiles/rails7.gemfile +13 -0
- data/lib/search_cop/grammar_parser.rb +3 -4
- data/lib/search_cop/hash_parser.rb +20 -18
- data/lib/search_cop/helpers.rb +15 -0
- data/lib/search_cop/query_builder.rb +3 -5
- data/lib/search_cop/query_info.rb +0 -2
- data/lib/search_cop/search_scope.rb +2 -4
- data/lib/search_cop/version.rb +1 -1
- data/lib/search_cop/visitors/mysql.rb +4 -2
- data/lib/search_cop/visitors/postgres.rb +5 -3
- data/lib/search_cop/visitors/visitor.rb +7 -5
- data/lib/search_cop/visitors.rb +0 -2
- data/lib/search_cop.rb +15 -13
- data/lib/search_cop_grammar/attributes.rb +53 -34
- data/lib/search_cop_grammar/nodes.rb +0 -2
- data/lib/search_cop_grammar.rb +13 -4
- data/lib/search_cop_grammar.treetop +6 -4
- data/search_cop.gemspec +9 -11
- data/test/and_test.rb +6 -8
- data/test/boolean_test.rb +7 -9
- data/test/database.yml +4 -1
- data/test/date_test.rb +38 -12
- data/test/datetime_test.rb +45 -12
- data/test/default_operator_test.rb +51 -0
- data/test/error_test.rb +2 -4
- data/test/float_test.rb +16 -11
- data/test/fulltext_test.rb +6 -8
- data/test/hash_test.rb +32 -34
- data/test/integer_test.rb +9 -11
- data/test/not_test.rb +6 -8
- data/test/or_test.rb +8 -10
- data/test/scope_test.rb +11 -13
- data/test/search_cop_test.rb +42 -36
- data/test/string_test.rb +67 -19
- data/test/test_helper.rb +28 -18
- data/test/visitor_test.rb +4 -6
- metadata +25 -42
- data/.travis.yml +0 -45
- data/Appraisals +0 -21
- data/gemfiles/3.2.gemfile +0 -25
- data/gemfiles/4.0.gemfile +0 -25
- data/gemfiles/4.1.gemfile +0 -25
- data/gemfiles/4.2.gemfile +0 -25
- data/gemfiles/5.0.gemfile +0 -25
data/test/datetime_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 DatetimeTest < SearchCop::TestCase
|
5
4
|
def test_mapping
|
6
|
-
product = create(:product, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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 :
|
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, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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, :
|
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
|
-
|
data/test/fulltext_test.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
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, :
|
7
|
-
product2 = create(:product, :
|
8
|
-
product3 = create(:product, :
|
5
|
+
product1 = create(:product, title: "word1")
|
6
|
+
product2 = create(:product, title: "word2 word3")
|
7
|
+
product3 = create(:product, title: "word2")
|
9
8
|
|
10
9
|
results = Product.search("word1 OR (title:word2 -word3)")
|
11
10
|
|
@@ -15,8 +14,8 @@ class FulltextTest < SearchCop::TestCase
|
|
15
14
|
end
|
16
15
|
|
17
16
|
def test_mixed
|
18
|
-
expected = create(:product, :
|
19
|
-
rejected = create(:product, :
|
17
|
+
expected = create(:product, title: "Expected title", stock: 1)
|
18
|
+
rejected = create(:product, title: "Expected title", stock: 0)
|
20
19
|
|
21
20
|
results = Product.search("Expected title:Title stock > 0")
|
22
21
|
|
@@ -24,4 +23,3 @@ class FulltextTest < SearchCop::TestCase
|
|
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, :
|
7
|
-
product2 = create(:product, :
|
8
|
-
product3 = create(:product, :
|
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(:
|
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,93 +14,92 @@ class HashTest < SearchCop::TestCase
|
|
15
14
|
end
|
16
15
|
|
17
16
|
def test_matches
|
18
|
-
expected = create(:product, :
|
19
|
-
rejected = create(:product, :
|
17
|
+
expected = create(:product, title: "Expected")
|
18
|
+
rejected = create(:product, title: "Rejected")
|
20
19
|
|
21
|
-
results = Product.search(:
|
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, :
|
29
|
-
rejected = create(:product, :
|
27
|
+
expected = create(:product, title: "Expected")
|
28
|
+
rejected = create(:product, title: "Rejected")
|
30
29
|
|
31
|
-
results = Product.search(:
|
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, :
|
39
|
-
rejected = create(:product, :
|
37
|
+
expected = create(:product, title: "Expected")
|
38
|
+
rejected = create(:product, title: "Rejected")
|
40
39
|
|
41
|
-
results = Product.search(:
|
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, :
|
49
|
-
rejected = create(:product, :
|
47
|
+
expected = create(:product, title: "Expected")
|
48
|
+
rejected = create(:product, title: "Rejected")
|
50
49
|
|
51
|
-
results = Product.search(:
|
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, :
|
59
|
-
rejected = create(:product, :
|
57
|
+
expected = create(:product, stock: 1)
|
58
|
+
rejected = create(:product, stock: 0)
|
60
59
|
|
61
|
-
results = Product.search(:
|
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, :
|
69
|
-
rejected = create(:product, :
|
67
|
+
expected = create(:product, stock: 1)
|
68
|
+
rejected = create(:product, stock: 0)
|
70
69
|
|
71
|
-
results = Product.search(:
|
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, :
|
79
|
-
rejected = create(:product, :
|
77
|
+
expected = create(:product, stock: 0)
|
78
|
+
rejected = create(:product, stock: 1)
|
80
79
|
|
81
|
-
results = Product.search(:
|
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, :
|
89
|
-
rejected = create(:product, :
|
87
|
+
expected = create(:product, stock: 0)
|
88
|
+
rejected = create(:product, stock: 1)
|
90
89
|
|
91
|
-
results = Product.search(:
|
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
95
|
|
97
96
|
def test_custom_matcher
|
98
|
-
expected = create(:product, :
|
99
|
-
rejected = create(:product, :
|
97
|
+
expected = create(:product, title: "Expected")
|
98
|
+
rejected = create(:product, title: "Rejected")
|
100
99
|
|
101
|
-
results = Product.search(:
|
100
|
+
results = Product.search(title: { custom_eq: "Expected" })
|
102
101
|
|
103
102
|
assert_includes results, expected
|
104
103
|
refute_includes results, rejected
|
105
104
|
end
|
106
105
|
end
|
107
|
-
|
data/test/integer_test.rb
CHANGED
@@ -1,58 +1,57 @@
|
|
1
|
-
|
2
|
-
require File.expand_path("../test_helper", __FILE__)
|
1
|
+
require File.expand_path("test_helper", __dir__)
|
3
2
|
|
4
3
|
class IntegerTest < SearchCop::TestCase
|
5
4
|
def test_anywhere
|
6
|
-
product = create(:product, :
|
5
|
+
product = create(:product, stock: 1)
|
7
6
|
|
8
7
|
assert_includes Product.search("1"), product
|
9
8
|
refute_includes Product.search("0"), product
|
10
9
|
end
|
11
10
|
|
12
11
|
def test_includes
|
13
|
-
product = create(:product, :
|
12
|
+
product = create(:product, stock: 1)
|
14
13
|
|
15
14
|
assert_includes Product.search("stock: 1"), product
|
16
15
|
refute_includes Product.search("stock: 10"), product
|
17
16
|
end
|
18
17
|
|
19
18
|
def test_equals
|
20
|
-
product = create(:product, :
|
19
|
+
product = create(:product, stock: 1)
|
21
20
|
|
22
21
|
assert_includes Product.search("stock = 1"), product
|
23
22
|
refute_includes Product.search("stock = 0"), product
|
24
23
|
end
|
25
24
|
|
26
25
|
def test_equals_not
|
27
|
-
product = create(:product, :
|
26
|
+
product = create(:product, stock: 1)
|
28
27
|
|
29
28
|
assert_includes Product.search("stock != 0"), product
|
30
29
|
refute_includes Product.search("stock != 1"), product
|
31
30
|
end
|
32
31
|
|
33
32
|
def test_greater
|
34
|
-
product = create(:product, :
|
33
|
+
product = create(:product, stock: 1)
|
35
34
|
|
36
35
|
assert_includes Product.search("stock > 0"), product
|
37
36
|
refute_includes Product.search("stock < 1"), product
|
38
37
|
end
|
39
38
|
|
40
39
|
def test_greater_equals
|
41
|
-
product = create(:product, :
|
40
|
+
product = create(:product, stock: 1)
|
42
41
|
|
43
42
|
assert_includes Product.search("stock >= 1"), product
|
44
43
|
refute_includes Product.search("stock >= 2"), product
|
45
44
|
end
|
46
45
|
|
47
46
|
def test_less
|
48
|
-
product = create(:product, :
|
47
|
+
product = create(:product, stock: 1)
|
49
48
|
|
50
49
|
assert_includes Product.search("stock < 2"), product
|
51
50
|
refute_includes Product.search("stock < 1"), product
|
52
51
|
end
|
53
52
|
|
54
53
|
def test_less_equals
|
55
|
-
product = create(:product, :
|
54
|
+
product = create(:product, stock: 1)
|
56
55
|
|
57
56
|
assert_includes Product.search("stock <= 1"), product
|
58
57
|
refute_includes Product.search("stock <= 0"), product
|
@@ -64,4 +63,3 @@ class IntegerTest < SearchCop::TestCase
|
|
64
63
|
end
|
65
64
|
end
|
66
65
|
end
|
67
|
-
|
data/test/not_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 NotTest < SearchCop::TestCase
|
5
4
|
def test_not_string
|
6
|
-
expected = create(:product, :
|
7
|
-
rejected = create(:product, :
|
5
|
+
expected = create(:product, title: "Expected title")
|
6
|
+
rejected = create(:product, title: "Rejected title")
|
8
7
|
|
9
8
|
results = Product.search("title: Title NOT title: Rejected")
|
10
9
|
|
@@ -15,13 +14,12 @@ class NotTest < SearchCop::TestCase
|
|
15
14
|
end
|
16
15
|
|
17
16
|
def test_not_hash
|
18
|
-
expected = create(:product, :
|
19
|
-
rejected = create(:product, :
|
17
|
+
expected = create(:product, title: "Expected title")
|
18
|
+
rejected = create(:product, title: "Rejected title")
|
20
19
|
|
21
|
-
results = Product.search(:
|
20
|
+
results = Product.search(and: [{ title: "Title" }, { not: { title: "Rejected" } }])
|
22
21
|
|
23
22
|
assert_includes results, expected
|
24
23
|
refute_includes results, rejected
|
25
24
|
end
|
26
25
|
end
|
27
|
-
|
data/test/or_test.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
|
2
|
-
require File.expand_path("../test_helper", __FILE__)
|
1
|
+
require File.expand_path("test_helper", __dir__)
|
3
2
|
|
4
3
|
class OrTest < SearchCop::TestCase
|
5
4
|
def test_or_string
|
6
|
-
product1 = create(:product, :
|
7
|
-
product2 = create(:product, :
|
8
|
-
product3 = create(:product, :
|
5
|
+
product1 = create(:product, title: "Title1")
|
6
|
+
product2 = create(:product, title: "Title2")
|
7
|
+
product3 = create(:product, title: "Title3")
|
9
8
|
|
10
9
|
results = Product.search("title: Title1 OR title: Title2")
|
11
10
|
|
@@ -15,15 +14,14 @@ class OrTest < SearchCop::TestCase
|
|
15
14
|
end
|
16
15
|
|
17
16
|
def test_or_hash
|
18
|
-
product1 = create(:product, :
|
19
|
-
product2 = create(:product, :
|
20
|
-
product3 = create(:product, :
|
17
|
+
product1 = create(:product, title: "Title1")
|
18
|
+
product2 = create(:product, title: "Title2")
|
19
|
+
product3 = create(:product, title: "Title3")
|
21
20
|
|
22
|
-
results = Product.search(:
|
21
|
+
results = Product.search(or: [{ title: "Title1" }, { title: "Title2" }])
|
23
22
|
|
24
23
|
assert_includes results, product1
|
25
24
|
assert_includes results, product2
|
26
25
|
refute_includes results, product3
|
27
26
|
end
|
28
27
|
end
|
29
|
-
|
data/test/scope_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 ScopeTest < SearchCop::TestCase
|
5
4
|
def test_scope_name
|
6
|
-
expected = create(:product, :
|
7
|
-
rejected = create(:product, :
|
5
|
+
expected = create(:product, title: "Expected")
|
6
|
+
rejected = create(:product, notice: "Expected")
|
8
7
|
|
9
8
|
results = Product.user_search("Expected")
|
10
9
|
|
@@ -13,8 +12,8 @@ class ScopeTest < SearchCop::TestCase
|
|
13
12
|
end
|
14
13
|
|
15
14
|
def test_options
|
16
|
-
expected = create(:product, :
|
17
|
-
rejected = create(:product, :
|
15
|
+
expected = create(:product, title: "Expected")
|
16
|
+
rejected = create(:product, description: "Expected")
|
18
17
|
|
19
18
|
results = Product.user_search("Expected")
|
20
19
|
|
@@ -23,8 +22,8 @@ class ScopeTest < SearchCop::TestCase
|
|
23
22
|
end
|
24
23
|
|
25
24
|
def test_custom_scope
|
26
|
-
expected = create(:product, :
|
27
|
-
rejected = create(:product, :
|
25
|
+
expected = create(:product, user: create(:user, username: "Expected"))
|
26
|
+
rejected = create(:product, user: create(:user, username: "Rejected"))
|
28
27
|
|
29
28
|
results = Product.user_search("user: Expected")
|
30
29
|
|
@@ -33,8 +32,8 @@ class ScopeTest < SearchCop::TestCase
|
|
33
32
|
end
|
34
33
|
|
35
34
|
def test_aliases_with_association
|
36
|
-
expected = create(:product, :
|
37
|
-
rejected = create(:product, :
|
35
|
+
expected = create(:product, user: create(:user, username: "Expected"))
|
36
|
+
rejected = create(:product, user: create(:user, username: "Rejected"))
|
38
37
|
|
39
38
|
results = Product.search("user: Expected")
|
40
39
|
|
@@ -43,8 +42,8 @@ class ScopeTest < SearchCop::TestCase
|
|
43
42
|
end
|
44
43
|
|
45
44
|
def test_aliases_with_model
|
46
|
-
expected = create(:product, :
|
47
|
-
rejected = create(:product, :
|
45
|
+
expected = create(:product, user: create(:user, username: "Expected"))
|
46
|
+
rejected = create(:product, user: create(:user, username: "Rejected"))
|
48
47
|
|
49
48
|
results = Product.user_search("user: Expected")
|
50
49
|
|
@@ -52,4 +51,3 @@ class ScopeTest < SearchCop::TestCase
|
|
52
51
|
refute_includes results, rejected
|
53
52
|
end
|
54
53
|
end
|
55
|
-
|