Skip to content

Commit

Permalink
テストを修正した
Browse files Browse the repository at this point in the history
テストの意図、結果を変えずに新しい検索(Searcher.search)の実装に変更した
  • Loading branch information
nakamu-kazu222 committed Feb 1, 2025
1 parent b750820 commit 13d7d09
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions test/models/searcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ def current_user
users(:kimura)
end

# キーワード検索結果の強調されているHTMLタグを除去するヘルパーメソッド
def strip_html(text)
ActionController::Base.helpers.strip_tags(text)
end

test "returns all types when document_type argument isn't specified" do
results = Searcher.search('テスト', current_user: current_user)
actual_classes = results.map(&:model_name).uniq
Expand Down Expand Up @@ -179,16 +184,15 @@ def current_user

test 'returns only answers of questions having all keywords' do
result = Searcher.search('です', document_type: :questions, current_user: current_user)
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [answers(:answer1).formatted_summary('です'), answers(:answer1).user.login_name])
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [answers(:answer5).formatted_summary('です'), answers(:answer5).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [answers(:answer1).description, answers(:answer1).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [answers(:answer5).description, answers(:answer5).user.login_name])

result = Searcher.search('です atom', document_type: :questions, current_user: current_user)
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [answers(:answer1).formatted_summary('です atom'), answers(:answer1).user.login_name])
assert_not_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [answers(:answer5).formatted_summary('です atom'), answers(:answer5).user.login_name])

assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [answers(:answer1).description, answers(:answer1).user.login_name])
assert_not_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [answers(:answer5).description, answers(:answer5).user.login_name])
result = Searcher.search('です atom', document_type: :questions, current_user: current_user)
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [answers(:answer1).formatted_summary('です atom'), answers(:answer1).user.login_name])
assert_not_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [answers(:answer5).formatted_summary('です atom'), answers(:answer5).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [answers(:answer1).description, answers(:answer1).user.login_name])
assert_not_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [answers(:answer5).description, answers(:answer5).user.login_name])
end

test 'returns only announcements having all keywords' do
Expand All @@ -207,32 +211,33 @@ def current_user

test 'returns only comments having all keywords' do
result = Searcher.search('report_id', document_type: :reports, current_user: current_user)
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment6).formatted_summary('report_id'), comments(:comment6).user.login_name])
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment5).formatted_summary('report_id'), comments(:comment5).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment6).description, comments(:comment6).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment5).description, comments(:comment5).user.login_name])

result = Searcher.search('report_id typo', document_type: :reports, current_user: current_user)
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment6).formatted_summary('report_id typo'), comments(:comment6).user.login_name])
assert_not_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment5).formatted_summary('report_id typo'), comments(:comment5).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment6).description, comments(:comment6).user.login_name])
assert_not_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment5).description, comments(:comment5).user.login_name])

result = Searcher.search('report_id typo', document_type: :reports, current_user: current_user)
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment6).formatted_summary('report_id typo'), comments(:comment6).user.login_name])
assert_not_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment5).formatted_summary('report_id typo'), comments(:comment5).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment6).description, comments(:comment6).user.login_name])
assert_not_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment5).description, comments(:comment5).user.login_name])
end

test 'returns only comments associated to specified document_type' do
result = Searcher.search('コメント', document_type: :reports, current_user: current_user)
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment11).formatted_summary('コメント'), comments(:comment11).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment11).description, comments(:comment11).user.login_name])
end

test 'returns all comments when document_type is not specified' do
current_user = users(:komagata)
result = Searcher.search('コメント', current_user: current_user)
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment8).formatted_summary('コメント'), comments(:comment8).user.login_name])
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment10).formatted_summary('コメント'), comments(:comment10).user.login_name])
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment11).formatted_summary('コメント'), comments(:comment11).user.login_name])
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment12).formatted_summary('コメント'), comments(:comment12).user.login_name])
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment13).formatted_summary('コメント'), comments(:comment13).user.login_name])
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment14).formatted_summary('コメント'), comments(:comment14).user.login_name])
assert_includes(result.map { |r| [r.formatted_summary, r.login_name] }, [comments(:comment16).formatted_summary('コメント'), comments(:comment16).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment8).description, comments(:comment8).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment10).description, comments(:comment10).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment11).description, comments(:comment11).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment12).description, comments(:comment12).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment13).description, comments(:comment13).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment14).description, comments(:comment14).user.login_name])
assert_includes(result.map { |r| [strip_html(r.formatted_summary), r.login_name] }, [comments(:comment16).description, comments(:comment16).user.login_name])
assert_equal(30, result.size)
end

Expand Down

0 comments on commit 13d7d09

Please sign in to comment.