<commit_before>require File.dirname(__FILE__) + '/../test_helper'
require 'questionnaires_controller'

# Re-raise errors caught by the controller.
class QuestionnairesController; def rescue_action(e) raise e end; end

class QuestionnairesControllerTest < Test::Unit::TestCase

  def setup
    @controller = QuestionnairesController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @environment = create_environment(:is_default => true)

    @research = create_research
    @section1 = create_item(:type => "section", :research_id => @research.id, :page_id => 1)
    @question1 = create_item(:type => "question", :research_id => @research.id, :html_type => Question.html_types.invert["pure_text"], :page_id => 1, :is_optional => false)
    @question2 = create_item(:type => "question", :research_id => @research.id, :html_type => Question.html_types.invert["single_selection"], :page_id => 2, :is_optional => false)
    @ivalue1 = create_item_value(:item_id => @question2.id, :info => "info")
    @ivalue2 = create_item_value(:item_id => @question2.id, :info => "info2")
    @research.reload; @question2.reload
  end

#new

  def test_should_get_new
     post :new, :research_id => @research.id
    assert_response :success
    #page_id == 1, because it's the first page 
    assert_tag :tag => 'form', :attributes => {:action => "/researches/#{@research.id}/questionnaires/new?page_id=1", :method => 'post'}
    assert_tag :tag => "input", :attributes => {:type => "submit", :value => /Next/}


    post :new, :research_id => @research.id, :commit => "Next", :page_id => 1
    assert_response :success
    assert_tag :tag => 'form', :attributes => {:action => "/researches/#{@research.id}/questionnaires/new?page_id=2", :method => 'post'}
   
    post :new, :research_id => @research.id, :commit => "Back", :page_id => 2
    assert_response :success
    assert_tag :tag => 'form', :attributes => {:action => "/researches/#{@research.id}/questionnaires/new?page_id=1", :method => 'post'}
 
 end

#create
  
  def test_should_create_questionnaire
     count = Questionnaire.count

     require 'pp'
    # Some questions weren't answered.
     
     post :new, :public_id => @research.id, :commit => "Submit", :page_id => 2, :object_item_values => {}

     assert_equal count, Questionnaire.count

     # All questions were answered. 

     pp @research     
     post :new, :public_id => @research.id, :commit => "Submit",                                                                           :page_id => 2, :object_item_values => {@question1.id.to_s => {:info => "test"},                                                                                     @question2.id.to_s => @ivalue1.id.to_s}
    
     assert_equal count + 1, Questionnaire.count

  end

end
<commit_msg>Fixing tests in questionnaire controller<commit_after>require File.dirname(__FILE__) + '/../test_helper'
require 'questionnaires_controller'

# Re-raise errors caught by the controller.
class QuestionnairesController; def rescue_action(e) raise e end; end

class QuestionnairesControllerTest < Test::Unit::TestCase

  def setup
    @controller = QuestionnairesController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
    @environment = create_environment(:is_default => true)

    @research = create_research
    @section1 = create_item(:type => "section", :research_id => @research.id, :page_id => 1)
    @question1 = create_item(:type => "question", :research_id => @research.id, :html_type => Question.html_types.invert["pure_text"], :page_id => 1, :is_optional => false)
    @question2 = create_item(:type => "question", :research_id => @research.id, :html_type => Question.html_types.invert["single_selection"], :page_id => 2, :is_optional => false)
    @ivalue1 = create_item_value(:item_id => @question2.id, :info => "info")
    @ivalue2 = create_item_value(:item_id => @question2.id, :info => "info2")
    @research.reload; @question2.reload
  end

#new

  def test_should_get_new
     post :new, :research_id => @research.id
    assert_response :success
    #page_id == 1, because it's the first page 
    assert_tag :tag => 'form', :attributes => {:action => "/questionnaires/new?page_id=1", :method => 'post'}
    assert_tag :tag => "input", :attributes => {:type => "submit", :value => /Next/}


    post :new, :research_id => @research.id, :commit => "Next", :page_id => 1
    assert_response :success
    assert_tag :tag => 'form', :attributes => {:action => "/questionnaires/new?page_id=2", :method => 'post'}
   
    post :new, :research_id => @research.id, :commit => "Back", :page_id => 2
    assert_response :success
    assert_tag :tag => 'form', :attributes => {:action => "/questionnaires/new?page_id=1", :method => 'post'}
 
 end

#create
  
  def test_should_create_questionnaire
     count = Questionnaire.count

    # Some questions weren't answered.
     
     post :new, :public_id => @research.id, :commit => "Submit", :page_id => 2, :object_item_values => {}

     assert_equal count, Questionnaire.count

     # All questions were answered. 

     post :new, :public_id => @research.id, :commit => "Submit",                                                                           :page_id => 2, :object_item_values => {@question1.id.to_s => {:info => "test"},                                                                                     @question2.id.to_s => @ivalue1.id.to_s}
    
     assert_equal count + 1, Questionnaire.count

  end

end

{'fraction_non_alphanumeric': 0.14712725971004117, 'fraction_numerical': 0.008591372829783426, 'mean_word_length': 3.2494296577946766, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5389381', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1655, 'n_words': 390}