Skip to content

Commit 5b51da5

Browse files
committed
added tests for the parse_qualifier_strings helper
1 parent e3e7560 commit 5b51da5

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

test/helpers/search_helper_test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,43 @@ class SearchHelperTest < ActionView::TestCase
1313
end
1414
end
1515

16+
test 'parse_qualifier_strings should correctly parse qualifiers' do
17+
exclude_tag = tags(:bug)
18+
include_tag = tags(:support)
19+
20+
{
21+
'answers:>42' => { param: :answers, operator: '>', value: 42 },
22+
'category:1' => { param: :category, operator: '=', category_id: 1 },
23+
'downvotes:>10' => { param: :downvotes, operator: '>', value: 10 },
24+
'post_type:2' => { param: :post_type, operator: '=', post_type_id: 2 },
25+
'score:0.5' => { param: :score, operator: '=', value: 0.5 },
26+
'status:any' => { param: :status, value: 'any' },
27+
"tag:#{include_tag.name}" => { param: :include_tag, tag_id: include_tag },
28+
"-tag:#{exclude_tag.name}" => { param: :exclude_tag, tag_id: exclude_tag },
29+
'source:native' => { param: :source, value: 'native' },
30+
'upvotes:<3' => { param: :upvotes, operator: '<', value: 3 },
31+
'user:-1' => { param: :user, operator: '=', user_id: -1 },
32+
'votes:0' => { param: :net_votes, operator: '=', value: 0 }
33+
}.each do |input, expect|
34+
parsed = parse_qualifier_strings([input])
35+
assert_equal expect[:param], parsed[0][:param]
36+
37+
# TODO: make return types of parse_*_qualifier helpers consistent
38+
if [:category, :post_type, :user].include?(expect[:param])
39+
value_key = :"#{expect[:param]}_id"
40+
assert_equal expect[value_key], parsed[0][value_key]
41+
elsif [:include_tag, :exclude_tag].include?(expect[:param])
42+
assert_equal expect[:tag_id].id, parsed[0][:tag_id].first&.id
43+
else
44+
assert_equal expect[:value], parsed[0][:value]
45+
end
46+
47+
if expect[:operator].present?
48+
assert_equal expect[:operator], parsed[0][:operator]
49+
end
50+
end
51+
end
52+
1653
test 'qualifiers_to_sql should correctly narrow by :category qualifier' do
1754
main = categories(:main)
1855
admin_only = categories(:admin_only)

0 commit comments

Comments
 (0)