search_cop 1.2.3 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/test/date_test.rb DELETED
@@ -1,107 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class DateTest < SearchCop::TestCase
4
- def test_mapping
5
- product = create(:product, created_on: Date.parse("2014-05-01"))
6
-
7
- assert_includes Product.search("created_on: 2014"), product
8
- assert_includes Product.search("created_on: 2014-05"), product
9
- assert_includes Product.search("created_on: 2014-05-01"), product
10
- end
11
-
12
- def test_anywhere
13
- product = create(:product, created_on: Date.parse("2014-05-01"))
14
-
15
- assert_includes Product.search("2014-05-01"), product
16
- refute_includes Product.search("2014-05-02"), product
17
- end
18
-
19
- def test_includes
20
- product = create(:product, created_on: Date.parse("2014-05-01"))
21
-
22
- assert_includes Product.search("created_on: 2014-05-01"), product
23
- refute_includes Product.search("created_on: 2014-05-02"), product
24
- end
25
-
26
- def test_equals
27
- product = create(:product, created_on: Date.parse("2014-05-01"))
28
-
29
- assert_includes Product.search("created_on = 2014-05-01"), product
30
- refute_includes Product.search("created_on = 2014-05-02"), product
31
- end
32
-
33
- def test_equals_not
34
- product = create(:product, created_on: Date.parse("2014-05-01"))
35
-
36
- assert_includes Product.search("created_on != 2014-05-02"), product
37
- refute_includes Product.search("created_on != 2014-05-01"), product
38
- end
39
-
40
- def test_greater
41
- product = create(:product, created_on: Date.parse("2014-05-01"))
42
-
43
- assert_includes Product.search("created_on > 2014-04-01"), product
44
- refute_includes Product.search("created_on > 2014-05-01"), product
45
- end
46
-
47
- def test_greater_equals
48
- product = create(:product, created_on: Date.parse("2014-05-01"))
49
-
50
- assert_includes Product.search("created_on >= 2014-05-01"), product
51
- refute_includes Product.search("created_on >= 2014-05-02"), product
52
- end
53
-
54
- def test_less
55
- product = create(:product, created_on: Date.parse("2014-05-01"))
56
-
57
- assert_includes Product.search("created_on < 2014-05-02"), product
58
- refute_includes Product.search("created_on < 2014-05-01"), product
59
- end
60
-
61
- def test_less_equals
62
- product = create(:product, created_on: Date.parse("2014-05-02"))
63
-
64
- assert_includes Product.search("created_on <= 2014-05-02"), product
65
- refute_includes Product.search("created_on <= 2014-05-01"), product
66
- end
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
-
96
- def test_no_overflow
97
- assert_nothing_raised do
98
- Product.search("created_on: 1000000").to_a
99
- end
100
- end
101
-
102
- def test_incompatible_datatype
103
- assert_raises SearchCop::IncompatibleDatatype do
104
- Product.unsafe_search "created_on: Value"
105
- end
106
- end
107
- end
@@ -1,115 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class DatetimeTest < SearchCop::TestCase
4
- def test_mapping
5
- product = create(:product, created_at: Time.parse("2014-05-01 12:30:30"))
6
-
7
- assert_includes Product.search("created_at: 2014"), product
8
- assert_includes Product.search("created_at: 2014-05"), product
9
- assert_includes Product.search("created_at: 2014-05-01"), product
10
- assert_includes Product.search("created_at: '2014-05-01 12:30:30'"), product
11
- end
12
-
13
- def test_anywhere
14
- product = create(:product, created_at: Time.parse("2014-05-01"))
15
-
16
- assert_includes Product.search("2014-05-01"), product
17
- refute_includes Product.search("2014-05-02"), product
18
- end
19
-
20
- def test_includes
21
- product = create(:product, created_at: Time.parse("2014-05-01"))
22
-
23
- assert_includes Product.search("created_at: 2014-05-01"), product
24
- refute_includes Product.search("created_at: 2014-05-02"), product
25
- end
26
-
27
- def test_equals
28
- product = create(:product, created_at: Time.parse("2014-05-01"))
29
-
30
- assert_includes Product.search("created_at = 2014-05-01"), product
31
- refute_includes Product.search("created_at = 2014-05-02"), product
32
- end
33
-
34
- def test_equals_not
35
- product = create(:product, created_at: Time.parse("2014-05-01"))
36
-
37
- assert_includes Product.search("created_at != 2014-05-02"), product
38
- refute_includes Product.search("created_at != 2014-05-01"), product
39
- end
40
-
41
- def test_greater
42
- product = create(:product, created_at: Time.parse("2014-05-01"))
43
-
44
- assert_includes Product.search("created_at > 2014-04-01"), product
45
- refute_includes Product.search("created_at > 2014-05-01"), product
46
- end
47
-
48
- def test_greater_equals
49
- product = create(:product, created_at: Time.parse("2014-05-01"))
50
-
51
- assert_includes Product.search("created_at >= 2014-05-01"), product
52
- refute_includes Product.search("created_at >= 2014-05-02"), product
53
- end
54
-
55
- def test_less
56
- product = create(:product, created_at: Time.parse("2014-05-01"))
57
-
58
- assert_includes Product.search("created_at < 2014-05-02"), product
59
- refute_includes Product.search("created_at < 2014-05-01"), product
60
- end
61
-
62
- def test_less_equals
63
- product = create(:product, created_at: Time.parse("2014-05-02"))
64
-
65
- assert_includes Product.search("created_at <= 2014-05-02"), product
66
- refute_includes Product.search("created_at <= 2014-05-01"), product
67
- end
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
-
104
- def test_no_overflow
105
- assert_nothing_raised do
106
- Product.search("created_at: 1000000").to_a
107
- end
108
- end
109
-
110
- def test_incompatible_datatype
111
- assert_raises SearchCop::IncompatibleDatatype do
112
- Product.unsafe_search "created_at: Value"
113
- end
114
- end
115
- end
@@ -1,51 +0,0 @@
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 DELETED
@@ -1,15 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class ErrorTest < SearchCop::TestCase
4
- def test_parse_error
5
- assert_raises SearchCop::ParseError do
6
- Product.unsafe_search title: { unknown_operator: "Value" }
7
- end
8
- end
9
-
10
- def test_unknown_column
11
- assert_raises SearchCop::UnknownColumn do
12
- Product.unsafe_search "Unknown: Column"
13
- end
14
- end
15
- end
data/test/float_test.rb DELETED
@@ -1,72 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class FloatTest < SearchCop::TestCase
4
- def test_anywhere
5
- product = create(:product, price: 10.5, created_at: Time.now - 1.day)
6
-
7
- assert_includes Product.search("10.5"), product
8
- refute_includes Product.search("11.5"), product
9
- end
10
-
11
- def test_includes
12
- product = create(:product, price: 10.5)
13
-
14
- assert_includes Product.search("price: 10.5"), product
15
- refute_includes Product.search("price: 11.5"), product
16
- end
17
-
18
- def test_equals
19
- product = create(:product, price: 10.5)
20
-
21
- assert_includes Product.search("price = 10.5"), product
22
- refute_includes Product.search("price = 11.5"), product
23
- end
24
-
25
- def test_equals_not
26
- product = create(:product, price: 10.5)
27
-
28
- assert_includes Product.search("price != 11.5"), product
29
- refute_includes Product.search("price != 10.5"), product
30
- end
31
-
32
- def test_greater
33
- product = create(:product, price: 10.5)
34
-
35
- assert_includes Product.search("price > 10.4"), product
36
- refute_includes Product.search("price < 10.5"), product
37
- end
38
-
39
- def test_greater_equals
40
- product = create(:product, price: 10.5)
41
-
42
- assert_includes Product.search("price >= 10.5"), product
43
- refute_includes Product.search("price >= 10.6"), product
44
- end
45
-
46
- def test_less
47
- product = create(:product, price: 10.5)
48
-
49
- assert_includes Product.search("price < 10.6"), product
50
- refute_includes Product.search("price < 10.5"), product
51
- end
52
-
53
- def test_less_equals
54
- product = create(:product, price: 10.5)
55
-
56
- assert_includes Product.search("price <= 10.5"), product
57
- refute_includes Product.search("price <= 10.4"), product
58
- end
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
-
67
- def test_incompatible_datatype
68
- assert_raises SearchCop::IncompatibleDatatype do
69
- Product.unsafe_search "price: Value"
70
- end
71
- end
72
- end
@@ -1,25 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class FulltextTest < SearchCop::TestCase
4
- def test_complex
5
- product1 = create(:product, title: "word1")
6
- product2 = create(:product, title: "word2 word3")
7
- product3 = create(:product, title: "word2")
8
-
9
- results = Product.search("word1 OR (title:word2 -word3)")
10
-
11
- assert_includes results, product1
12
- refute_includes results, product2
13
- assert_includes results, product3
14
- end
15
-
16
- def test_mixed
17
- expected = create(:product, title: "Expected title", stock: 1)
18
- rejected = create(:product, title: "Expected title", stock: 0)
19
-
20
- results = Product.search("Expected title:Title stock > 0")
21
-
22
- assert_includes results, expected
23
- refute_includes results, rejected
24
- end
25
- end
data/test/hash_test.rb DELETED
@@ -1,105 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class HashTest < SearchCop::TestCase
4
- def test_subquery
5
- product1 = create(:product, title: "Title1", description: "Description")
6
- product2 = create(:product, title: "Title2", description: "Description")
7
- product3 = create(:product, title: "TItle3", description: "Description")
8
-
9
- results = Product.search(or: [{ query: "Description Title1" }, { query: "Description Title2" }])
10
-
11
- assert_includes results, product1
12
- assert_includes results, product2
13
- refute_includes results, product3
14
- end
15
-
16
- def test_matches
17
- expected = create(:product, title: "Expected")
18
- rejected = create(:product, title: "Rejected")
19
-
20
- results = Product.search(title: { matches: "Expected" })
21
-
22
- assert_includes results, expected
23
- refute_includes results, rejected
24
- end
25
-
26
- def test_matches_default
27
- expected = create(:product, title: "Expected")
28
- rejected = create(:product, title: "Rejected")
29
-
30
- results = Product.search(title: "Expected")
31
-
32
- assert_includes results, expected
33
- refute_includes results, rejected
34
- end
35
-
36
- def test_eq
37
- expected = create(:product, title: "Expected")
38
- rejected = create(:product, title: "Rejected")
39
-
40
- results = Product.search(title: { eq: "Expected" })
41
-
42
- assert_includes results, expected
43
- refute_includes results, rejected
44
- end
45
-
46
- def test_not_eq
47
- expected = create(:product, title: "Expected")
48
- rejected = create(:product, title: "Rejected")
49
-
50
- results = Product.search(title: { not_eq: "Rejected" })
51
-
52
- assert_includes results, expected
53
- refute_includes results, rejected
54
- end
55
-
56
- def test_gt
57
- expected = create(:product, stock: 1)
58
- rejected = create(:product, stock: 0)
59
-
60
- results = Product.search(stock: { gt: 0 })
61
-
62
- assert_includes results, expected
63
- refute_includes results, rejected
64
- end
65
-
66
- def test_gteq
67
- expected = create(:product, stock: 1)
68
- rejected = create(:product, stock: 0)
69
-
70
- results = Product.search(stock: { gteq: 1 })
71
-
72
- assert_includes results, expected
73
- refute_includes results, rejected
74
- end
75
-
76
- def test_lt
77
- expected = create(:product, stock: 0)
78
- rejected = create(:product, stock: 1)
79
-
80
- results = Product.search(stock: { lt: 1 })
81
-
82
- assert_includes results, expected
83
- refute_includes results, rejected
84
- end
85
-
86
- def test_lteq
87
- expected = create(:product, stock: 0)
88
- rejected = create(:product, stock: 1)
89
-
90
- results = Product.search(stock: { lteq: 0 })
91
-
92
- assert_includes results, expected
93
- refute_includes results, rejected
94
- end
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
data/test/integer_test.rb DELETED
@@ -1,65 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class IntegerTest < SearchCop::TestCase
4
- def test_anywhere
5
- product = create(:product, stock: 1)
6
-
7
- assert_includes Product.search("1"), product
8
- refute_includes Product.search("0"), product
9
- end
10
-
11
- def test_includes
12
- product = create(:product, stock: 1)
13
-
14
- assert_includes Product.search("stock: 1"), product
15
- refute_includes Product.search("stock: 10"), product
16
- end
17
-
18
- def test_equals
19
- product = create(:product, stock: 1)
20
-
21
- assert_includes Product.search("stock = 1"), product
22
- refute_includes Product.search("stock = 0"), product
23
- end
24
-
25
- def test_equals_not
26
- product = create(:product, stock: 1)
27
-
28
- assert_includes Product.search("stock != 0"), product
29
- refute_includes Product.search("stock != 1"), product
30
- end
31
-
32
- def test_greater
33
- product = create(:product, stock: 1)
34
-
35
- assert_includes Product.search("stock > 0"), product
36
- refute_includes Product.search("stock < 1"), product
37
- end
38
-
39
- def test_greater_equals
40
- product = create(:product, stock: 1)
41
-
42
- assert_includes Product.search("stock >= 1"), product
43
- refute_includes Product.search("stock >= 2"), product
44
- end
45
-
46
- def test_less
47
- product = create(:product, stock: 1)
48
-
49
- assert_includes Product.search("stock < 2"), product
50
- refute_includes Product.search("stock < 1"), product
51
- end
52
-
53
- def test_less_equals
54
- product = create(:product, stock: 1)
55
-
56
- assert_includes Product.search("stock <= 1"), product
57
- refute_includes Product.search("stock <= 0"), product
58
- end
59
-
60
- def test_incompatible_datatype
61
- assert_raises SearchCop::IncompatibleDatatype do
62
- Product.unsafe_search "stock: Value"
63
- end
64
- end
65
- end
data/test/not_test.rb DELETED
@@ -1,25 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class NotTest < SearchCop::TestCase
4
- def test_not_string
5
- expected = create(:product, title: "Expected title")
6
- rejected = create(:product, title: "Rejected title")
7
-
8
- results = Product.search("title: Title NOT title: Rejected")
9
-
10
- assert_includes results, expected
11
- refute_includes results, rejected
12
-
13
- assert_equal results, Product.search("title: Title -title: Rejected")
14
- end
15
-
16
- def test_not_hash
17
- expected = create(:product, title: "Expected title")
18
- rejected = create(:product, title: "Rejected title")
19
-
20
- results = Product.search(and: [{ title: "Title" }, { not: { title: "Rejected" } }])
21
-
22
- assert_includes results, expected
23
- refute_includes results, rejected
24
- end
25
- end
data/test/or_test.rb DELETED
@@ -1,27 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class OrTest < SearchCop::TestCase
4
- def test_or_string
5
- product1 = create(:product, title: "Title1")
6
- product2 = create(:product, title: "Title2")
7
- product3 = create(:product, title: "Title3")
8
-
9
- results = Product.search("title: Title1 OR title: Title2")
10
-
11
- assert_includes results, product1
12
- assert_includes results, product2
13
- refute_includes results, product3
14
- end
15
-
16
- def test_or_hash
17
- product1 = create(:product, title: "Title1")
18
- product2 = create(:product, title: "Title2")
19
- product3 = create(:product, title: "Title3")
20
-
21
- results = Product.search(or: [{ title: "Title1" }, { title: "Title2" }])
22
-
23
- assert_includes results, product1
24
- assert_includes results, product2
25
- refute_includes results, product3
26
- end
27
- end
data/test/scope_test.rb DELETED
@@ -1,53 +0,0 @@
1
- require File.expand_path("test_helper", __dir__)
2
-
3
- class ScopeTest < SearchCop::TestCase
4
- def test_scope_name
5
- expected = create(:product, title: "Expected")
6
- rejected = create(:product, notice: "Expected")
7
-
8
- results = Product.user_search("Expected")
9
-
10
- assert_includes results, expected
11
- refute_includes results, rejected
12
- end
13
-
14
- def test_options
15
- expected = create(:product, title: "Expected")
16
- rejected = create(:product, description: "Expected")
17
-
18
- results = Product.user_search("Expected")
19
-
20
- assert_includes results, expected
21
- refute_includes results, rejected
22
- end
23
-
24
- def test_custom_scope
25
- expected = create(:product, user: create(:user, username: "Expected"))
26
- rejected = create(:product, user: create(:user, username: "Rejected"))
27
-
28
- results = Product.user_search("user: Expected")
29
-
30
- assert_includes results, expected
31
- refute_includes results, rejected
32
- end
33
-
34
- def test_aliases_with_association
35
- expected = create(:product, user: create(:user, username: "Expected"))
36
- rejected = create(:product, user: create(:user, username: "Rejected"))
37
-
38
- results = Product.search("user: Expected")
39
-
40
- assert_includes results, expected
41
- refute_includes results, rejected
42
- end
43
-
44
- def test_aliases_with_model
45
- expected = create(:product, user: create(:user, username: "Expected"))
46
- rejected = create(:product, user: create(:user, username: "Rejected"))
47
-
48
- results = Product.user_search("user: Expected")
49
-
50
- assert_includes results, expected
51
- refute_includes results, rejected
52
- end
53
- end