<commit_before>require 'test_helper'
require 'erb'
class SearchRenderTest < ActionDispatch::IntegrationTest

  def setup
    reindex
  end

  ##############################################################################
  ## Test each vertical to make sure that the correct results show up for
  ## each one.
  
  test "perform general searches" do 
    
    expected_results = [
      ['assignment', assignments(:two).id],
      ['dataset', datasets(:two).id],
      ['software', software(:two).id]
    ]
    
    
    get '/search/all', params: {q: "lion" }
    
    assert_template "search/show"
    assert_select ".search-result", count: 3
    
    expected_results.each_with_index do |x, i|
      assert_select ".search-result[data-rank=\"#{i+1}\"]", count: 1 do
        assert_select "div[data-#{x[0]}-id=\"#{x[1]}\"]", count: 1
      end
    end
    
    
    get '/search/software', params: {q: "lion" }
    
    assert_template "search/show"
    assert_select ".search-result", count: 1
    
    assert_select ".search-result[data-rank=\"1\"]", count: 1 do
      assert_select "div[data-software-id=\"#{software(:two).id}\"]", count: 1
    end
    
  end
  
  ##############################################################################
  
  
  ##############################################################################
  ## Test advanced search.
  
  test "perform an advanced search" do
    get '/search/software', params: {nq: "green", advanced: "true"}
    
    assert_template "search/show"
    assert_select ".search-result", count: 3
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Name", count: 1
    end
    assert_select "input[name=\"nq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"\"]", count: 1
    
    get '/search/software', params: {sq: "green", advanced: "true"}
    assert_template "search/show"
    assert_select ".search-result", count: 3
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Summary", count: 1
    end
      assert_select "input[name=\"nq\"][value=\"\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"\"]", count: 1
    
    get '/search/software', params: {dq: "green", advanced: "true"}
    assert_template "search/show"
    assert_select ".search-result", count: 3
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Description", count: 1
    end
    assert_select "input[name=\"nq\"][value=\"\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"green\"]", count: 1
    
    get '/search/software', params: {nq: "green", sq: "green", dq: "green", 
      advanced: "true"}
    assert_template "search/show"
    assert_select ".search-result", count: 9
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Name", count: 1
      assert_select "td", "Summary", count: 1
      assert_select "td", "Description", count: 1
    end
    assert_select "input[name=\"nq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"green\"]", count: 1
    
    get '/search/software', params: {nq: "green", sq: "green", dq: "green", 
      advanced: "true", all: "true"}
    
    assert_template "search/show"
    assert_select ".search-result", count: 0
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Name", count: 1
      assert_select "td", "Summary", count: 1
      assert_select "td", "Description", count: 1
    end
    assert_select "input[name=\"nq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"vertical\"][value=\"software\"]:checked", count: 1
  end
  ##############################################################################
  
  
  
  private
  
    def reindex
      Assignment.reindex<commit_msg>Fixed an error in the advanced search integration test.<commit_after>require 'test_helper'
require 'erb'
class SearchRenderTest < ActionDispatch::IntegrationTest

  def setup
    reindex
  end

  ##############################################################################
  ## Test each vertical to make sure that the correct results show up for
  ## each one.
  
  test "perform general searches" do 
    
    expected_results = [
      ['assignment', assignments(:two).id],
      ['dataset', datasets(:two).id],
      ['software', software(:two).id]
    ]
    
    
    get '/search/all', params: {q: "lion" }
    
    assert_template "search/show"
    assert_select ".search-result", count: 3
    
    expected_results.each_with_index do |x, i|
      assert_select ".search-result[data-rank=\"#{i+1}\"]", count: 1 do
        assert_select "div[data-#{x[0]}-id=\"#{x[1]}\"]", count: 1
      end
    end
    
    
    get '/search/software', params: {q: "lion" }
    
    assert_template "search/show"
    assert_select ".search-result", count: 1
    
    assert_select ".search-result[data-rank=\"1\"]", count: 1 do
      assert_select "div[data-software-id=\"#{software(:two).id}\"]", count: 1
    end
    
  end
  
  ##############################################################################
  
  
  ##############################################################################
  ## Test advanced search.
  
  test "perform an advanced search" do
    get '/search/software', params: {nq: "green", advanced: "true"}
    
    assert_template "search/show"
    assert_select ".search-result", count: 3
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Name", count: 1
    end
    assert_select "input[name=\"nq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"\"]", count: 1
    
    get '/search/software', params: {sq: "green", advanced: "true"}
    assert_template "search/show"
    assert_select ".search-result", count: 3
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Summary", count: 1
    end
      assert_select "input[name=\"nq\"][value=\"\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"\"]", count: 1
    
    get '/search/software', params: {dq: "green", advanced: "true"}
    assert_template "search/show"
    assert_select ".search-result", count: 3
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Description", count: 1
    end
    assert_select "input[name=\"nq\"][value=\"\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"green\"]", count: 1
    
    get '/search/software', params: {nq: "green", sq: "green", dq: "green", 
      advanced: "true"}
    assert_template "search/show"
    assert_select ".search-result", count: 9
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Name", count: 1
      assert_select "td", "Summary", count: 1
      assert_select "td", "Description", count: 1
    end
    assert_select "input[name=\"nq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"green\"]", count: 1
    
    get '/search/software', params: {nq: "green", sq: "green", dq: "green", 
      advanced: "true", all: "true"}
    
    assert_template "search/show"
    assert_select ".search-result", count: 0
    assert_select ".advanced-search-summary", count: 1 do 
      assert_select "td", "Name", count: 1
      assert_select "td", "Summary", count: 1
      assert_select "td", "Description", count: 1
    end
    assert_select "input[name=\"nq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"sq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"dq\"][value=\"green\"]", count: 1
    assert_select "input[name=\"vertical\"][value=\"software\"]" do |elm|
        assert elm.attr("checked").present?
    end
  end
  ##############################################################################
  
  
  
  private
  
    def reindex
      Assignment.reindex
{'fraction_non_alphanumeric': 0.23920461230733028, 'fraction_numerical': 0.01047182021414284, 'mean_word_length': 3.4155844155844157, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2257539', 'n_tokens_mistral': 2760, 'n_tokens_neox': 2558, 'n_words': 638}