search_cop 1.1.0 → 1.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/test.yml +50 -0
- data/.rubocop.yml +134 -0
- data/CHANGELOG.md +23 -9
- data/Gemfile +4 -5
- data/README.md +11 -1
- data/Rakefile +0 -1
- data/docker-compose.yml +8 -1
- data/gemfiles/{5.1.gemfile → rails5.gemfile} +2 -2
- data/gemfiles/rails6.gemfile +13 -0
- data/gemfiles/rails7.gemfile +13 -0
- data/lib/search_cop/grammar_parser.rb +1 -3
- data/lib/search_cop/hash_parser.rb +19 -19
- data/lib/search_cop/helpers.rb +15 -0
- data/lib/search_cop/query_builder.rb +1 -3
- data/lib/search_cop/query_info.rb +0 -2
- data/lib/search_cop/search_scope.rb +3 -5
- 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 +5 -3
- data/lib/search_cop/visitors.rb +0 -2
- data/lib/search_cop.rb +5 -23
- data/lib/search_cop_grammar/attributes.rb +45 -34
- data/lib/search_cop_grammar/nodes.rb +0 -2
- data/lib/search_cop_grammar.rb +1 -3
- data/search_cop.gemspec +8 -9
- data/test/and_test.rb +6 -8
- data/test/boolean_test.rb +7 -9
- data/test/database.yml +2 -1
- data/test/date_test.rb +14 -16
- data/test/datetime_test.rb +15 -17
- data/test/default_operator_test.rb +14 -10
- data/test/error_test.rb +2 -4
- data/test/float_test.rb +9 -11
- data/test/fulltext_test.rb +6 -8
- data/test/hash_test.rb +32 -34
- data/test/integer_test.rb +9 -11
- data/test/namespace_test.rb +23 -0
- 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 +41 -34
- data/test/string_test.rb +67 -19
- data/test/test_helper.rb +34 -15
- data/test/visitor_test.rb +4 -6
- metadata +34 -35
- data/.travis.yml +0 -34
- data/gemfiles/4.2.gemfile +0 -13
data/test/float_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 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
|
@@ -71,4 +70,3 @@ class FloatTest < SearchCop::TestCase
|
|
71
70
|
end
|
72
71
|
end
|
73
72
|
end
|
74
|
-
|
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
|
-
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.expand_path("test_helper", __dir__)
|
2
|
+
|
3
|
+
class NamespaceTest < SearchCop::TestCase
|
4
|
+
def test_model_namespace
|
5
|
+
expected = create(:product, title: "Expected")
|
6
|
+
rejected = create(:product, title: "Rejected")
|
7
|
+
|
8
|
+
results = SomeNamespace::Product.search("Expected")
|
9
|
+
|
10
|
+
assert_includes results.map(&:id), expected.id
|
11
|
+
refute_includes results.map(&:id), rejected.id
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_model_namespace_with_associations
|
15
|
+
expected = create(:product, user: create(:user, username: "Expected"))
|
16
|
+
rejected = create(:product, user: create(:user, username: "Rejected"))
|
17
|
+
|
18
|
+
results = SomeNamespace::Product.search("user:Expected")
|
19
|
+
|
20
|
+
assert_includes results.map(&:id), expected.id
|
21
|
+
refute_includes results.map(&:id), rejected.id
|
22
|
+
end
|
23
|
+
end
|
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
|
-
|
data/test/search_cop_test.rb
CHANGED
@@ -1,41 +1,40 @@
|
|
1
|
-
|
2
|
-
require File.expand_path("../test_helper", __FILE__)
|
1
|
+
require File.expand_path("test_helper", __dir__)
|
3
2
|
|
4
3
|
class SearchCopTest < SearchCop::TestCase
|
5
4
|
def test_scope_before
|
6
|
-
expected = create(:product, :
|
7
|
-
rejected = create(:product, :
|
5
|
+
expected = create(:product, stock: 1, title: "Title")
|
6
|
+
rejected = create(:product, stock: 0, title: "Title")
|
8
7
|
|
9
|
-
results = Product.where(:
|
8
|
+
results = Product.where(stock: 1).search("Title")
|
10
9
|
|
11
10
|
assert_includes results, expected
|
12
11
|
refute_includes results, rejected
|
13
12
|
end
|
14
13
|
|
15
14
|
def test_scope_after
|
16
|
-
expected = create(:product, :
|
17
|
-
rejected = create(:product, :
|
15
|
+
expected = create(:product, stock: 1, title: "Title")
|
16
|
+
rejected = create(:product, stock: 0, title: "Title")
|
18
17
|
|
19
|
-
results = Product.search("Title").where(:
|
18
|
+
results = Product.search("Title").where(stock: 1)
|
20
19
|
|
21
20
|
assert_includes results, expected
|
22
21
|
refute_includes results, rejected
|
23
22
|
end
|
24
23
|
|
25
24
|
def test_scope
|
26
|
-
expected = create(:product, :
|
27
|
-
rejected = create(:product, :
|
25
|
+
expected = create(:product, stock: 1, title: "Title")
|
26
|
+
rejected = create(:product, stock: 0, title: "Title")
|
28
27
|
|
29
|
-
results = with_scope(Product.search_scopes[:search],
|
28
|
+
results = with_scope(Product.search_scopes[:search], -> { where stock: 1 }) { Product.search("title: Title") }
|
30
29
|
|
31
30
|
assert_includes results, expected
|
32
31
|
refute_includes results, rejected
|
33
32
|
end
|
34
33
|
|
35
34
|
def test_multi_associations
|
36
|
-
product = create(:product, :
|
37
|
-
create(:comment, :
|
38
|
-
create(:comment, :
|
35
|
+
product = create(:product, comments: [
|
36
|
+
create(:comment, title: "Title1", message: "Message1"),
|
37
|
+
create(:comment, title: "Title2", message: "Message2")
|
39
38
|
])
|
40
39
|
|
41
40
|
assert_includes Product.search("comment: Title1 comment: Message1"), product
|
@@ -43,8 +42,8 @@ class SearchCopTest < SearchCop::TestCase
|
|
43
42
|
end
|
44
43
|
|
45
44
|
def test_single_association
|
46
|
-
expected = create(:comment, :
|
47
|
-
rejected = create(:comment, :
|
45
|
+
expected = create(:comment, user: create(:user, username: "Expected"))
|
46
|
+
rejected = create(:comment, user: create(:user, username: "Rejected"))
|
48
47
|
|
49
48
|
results = Comment.search("user: Expected")
|
50
49
|
|
@@ -53,8 +52,8 @@ class SearchCopTest < SearchCop::TestCase
|
|
53
52
|
end
|
54
53
|
|
55
54
|
def test_deep_associations
|
56
|
-
expected = create(:product, :
|
57
|
-
rejected = create(:product, :
|
55
|
+
expected = create(:product, comments: [create(:comment, user: create(:user, username: "Expected"))])
|
56
|
+
rejected = create(:product, comments: [create(:comment, user: create(:user, username: "Rejected"))])
|
58
57
|
|
59
58
|
results = Product.search("user: Expected")
|
60
59
|
|
@@ -62,16 +61,25 @@ class SearchCopTest < SearchCop::TestCase
|
|
62
61
|
refute_includes results, rejected
|
63
62
|
end
|
64
63
|
|
64
|
+
def test_inherited_model
|
65
|
+
expected = create(:available_product, comments: [create(:comment, user: create(:user, username: "Expected"))])
|
66
|
+
rejected = create(:available_product, comments: [create(:comment, user: create(:user, username: "Rejected"))])
|
67
|
+
|
68
|
+
results = AvailableProduct.search("user: Expected")
|
69
|
+
assert_includes results, expected
|
70
|
+
refute_includes results, rejected
|
71
|
+
end
|
72
|
+
|
65
73
|
def test_multiple
|
66
|
-
product = create(:product, :
|
74
|
+
product = create(:product, comments: [create(:comment, title: "Title", message: "Message")])
|
67
75
|
|
68
76
|
assert_includes Product.search("comment: Title"), product
|
69
77
|
assert_includes Product.search("comment: Message"), product
|
70
78
|
end
|
71
79
|
|
72
80
|
def test_default
|
73
|
-
product1 = create(:product, :
|
74
|
-
product2 = create(:product, :
|
81
|
+
product1 = create(:product, title: "Expected")
|
82
|
+
product2 = create(:product, description: "Expected")
|
75
83
|
|
76
84
|
results = Product.search("Expected")
|
77
85
|
|
@@ -80,11 +88,11 @@ class SearchCopTest < SearchCop::TestCase
|
|
80
88
|
end
|
81
89
|
|
82
90
|
def test_custom_default_enabled
|
83
|
-
product1 = create(:product, :
|
84
|
-
product2 = create(:product, :
|
85
|
-
product3 = create(:product, :
|
91
|
+
product1 = create(:product, title: "Expected")
|
92
|
+
product2 = create(:product, description: "Expected")
|
93
|
+
product3 = create(:product, brand: "Expected")
|
86
94
|
|
87
|
-
results = with_options(Product.search_scopes[:search], :primary, :
|
95
|
+
results = with_options(Product.search_scopes[:search], :primary, default: true) { Product.search "Expected" }
|
88
96
|
|
89
97
|
assert_includes results, product1
|
90
98
|
assert_includes results, product2
|
@@ -92,32 +100,32 @@ class SearchCopTest < SearchCop::TestCase
|
|
92
100
|
end
|
93
101
|
|
94
102
|
def test_custom_default_disabled
|
95
|
-
product1 = create(:product, :
|
96
|
-
product2 = create(:product, :
|
103
|
+
product1 = create(:product, brand: "Expected")
|
104
|
+
product2 = create(:product, notice: "Expected")
|
97
105
|
|
98
|
-
results = with_options(Product.search_scopes[:search], :notice, :
|
106
|
+
results = with_options(Product.search_scopes[:search], :notice, default: false) { Product.search "Expected" }
|
99
107
|
|
100
108
|
assert_includes results, product1
|
101
109
|
refute_includes results, product2
|
102
110
|
end
|
103
111
|
|
104
112
|
def test_count
|
105
|
-
create_list :product, 2, :
|
113
|
+
create_list :product, 2, title: "Expected"
|
106
114
|
|
107
115
|
assert_equal 2, Product.search("Expected").count
|
108
116
|
end
|
109
117
|
|
110
118
|
def test_default_attributes_true
|
111
|
-
with_options(Product.search_scopes[:search], :title, :
|
112
|
-
with_options(Product.search_scopes[:search], :description, :
|
119
|
+
with_options(Product.search_scopes[:search], :title, default: true) do
|
120
|
+
with_options(Product.search_scopes[:search], :description, default: true) do
|
113
121
|
assert_equal ["title", "description"], Product.search_scopes[:search].reflection.default_attributes.keys
|
114
122
|
end
|
115
123
|
end
|
116
124
|
end
|
117
125
|
|
118
126
|
def test_default_attributes_fales
|
119
|
-
with_options(Product.search_scopes[:search], :title, :
|
120
|
-
with_options(Product.search_scopes[:search], :description, :
|
127
|
+
with_options(Product.search_scopes[:search], :title, default: false) do
|
128
|
+
with_options(Product.search_scopes[:search], :description, default: false) do
|
121
129
|
assert_equal Product.search_scopes[:search].reflection.attributes.keys - ["title", "description"], Product.search_scopes[:search].reflection.default_attributes.keys
|
122
130
|
end
|
123
131
|
end
|
@@ -136,4 +144,3 @@ class SearchCopTest < SearchCop::TestCase
|
|
136
144
|
refute Object.respond_to?(:search)
|
137
145
|
end
|
138
146
|
end
|
139
|
-
|