Created
July 11, 2013 11:45
-
-
Save snoblenet/5974751 to your computer and use it in GitHub Desktop.
survey_spec.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "spec_helper" | |
describe "survey" do | |
before :each do | |
@element = Element.create! :name => "meta", :meta => true | |
@standard = Standard.create! :name => "meta", :meta => true, :description => "meta" | |
@search = Product.create! :name => "Retail Site Search" | |
@mispellings = @search.questions.create! :description => "Cope with mispellings?", :instructions => "On the homepage, there is:", :element_id => @element.id, :standard_id => @standard.id, :active => true | |
@f1 = @mispellings.fields.create! :name => "Best practice", :points => 3.0 | |
@f2 = @mispellings.fields.create! :name => "Category practice", :points => 2.0 | |
@f3 = @mispellings.fields.create! :name => "Standard practice", :points => 1.0 | |
@f4 = @mispellings.fields.create! :name => "Bad practice", :points => 0.0 | |
@review = @search.reviews.create! :client_name => "Review", :url => "http://review.com" | |
@competitor = @review.competitors.first | |
@anothercomp = @review.competitors.create! :name => "QBE", :url => "http://qbe.com.au" | |
@survey = @competitor.surveys.find_by_question_id(@mispellings.id) | |
end | |
describe "as customer" do | |
it "cannot list surveys" do | |
login_as "customer" | |
visit competitor_surveys_path(@competitor) | |
page.current_path.should == root_path | |
end | |
end | |
describe "as tester" do | |
before :each do | |
login_as "tester" | |
end | |
it "can see a survey" do | |
visit competitor_survey_path(@competitor, @survey) | |
page.current_path.should == competitor_survey_path(@competitor, @survey) | |
end | |
it "can edit a survey" do | |
visit edit_competitor_survey_path(@competitor, @survey) | |
page.current_path.should == edit_competitor_survey_path(@competitor, @survey) | |
end | |
it "can list surveys" do | |
visit competitor_surveys_path(@competitor) | |
page.current_path.should == competitor_surveys_path(@competitor) | |
end | |
end | |
describe "as admin" do | |
before :each do | |
login_as "admin" | |
@survey = @competitor.surveys.find_by_question_id(@mispellings.id) | |
end | |
it "can show" do | |
visit competitor_survey_path(@competitor, @survey) | |
page.current_path.should == competitor_survey_path(@competitor, @survey) | |
page.should have_content "Cope with mispellings?" | |
page.should have_content "On the homepage, there is:" | |
page.should have_content "Best practice (select)" | |
page.should have_content "Category practice (select)" | |
page.should have_content "Standard practice (select)" | |
page.should have_content "Bad practice (select)" | |
@survey.selected_results.blank?.should be_true | |
page.should have_content "Score: 0.0 out of 3.0 available points" | |
end | |
it "can list" do | |
visit competitor_surveys_path(@competitor) | |
page.current_path.should == competitor_surveys_path(@competitor) | |
page.should have_content @competitor.name | |
page.should have_content @competitor.review.client_name | |
page.should have_content @competitor.surveys.first.question.name | |
page.should have_content @competitor.surveys.last.question.name | |
end | |
it "can add a question to a product and then find the corresponding survey this creates" do | |
visit new_product_question_path(@search) | |
fill_in "question_description", :with => "Is it fab?" | |
fill_in "question_instructions", :with => "Look for evidence of fabulousness" | |
select @element.name, :from => "question[element_id]" | |
select @standard.name, :from => "question[standard_id]" | |
select "Yes", :from => "question[additive]" | |
check "question_active" | |
click_on "submit_question" | |
@survey = @competitor.surveys.find_by_question_id(Question.find_by_description("Is it fab?").id) | |
visit competitor_survey_path(@competitor, @survey) | |
page.current_path.should == competitor_survey_path(@competitor, @survey) | |
page.should have_content "Is it fab?" | |
visit competitor_surveys_path(@competitor) | |
page.current_path.should == competitor_surveys_path(@competitor) | |
page.should have_content "Is it fab?" | |
end | |
it "can complete and edit a first-past-the-post survey" do | |
visit competitor_survey_path(@competitor, @survey) | |
click_on "select_category_practice" | |
click_on "select_standard_practice" | |
@survey.results.select{|r| r.name == "Best practice"}.first.selected.should be_false | |
@survey.results.select{|r| r.name == "Category practice"}.first.selected.should be_false | |
@survey.results.select{|r| r.name == "Standard practice"}.first.selected.should be_true | |
@survey.results.select{|r| r.name == "Bad practice"}.first.selected.should be_false | |
visit competitor_survey_path(@competitor, @survey) | |
page.should have_content "Cope with mispellings?" | |
page.should have_content "On the homepage, there is:" | |
page.should have_content "Best practice (select)" | |
page.should have_content "Category practice (select)" | |
page.should have_content "Standard practice (deselect)" | |
page.should have_content "Bad practice (select)" | |
page.should have_content "Score: 1.0 out of 3.0 available points" | |
click_on "select_category_practice" | |
page.should have_content "Best practice (select)" | |
page.should have_content "Category practice (deselect)" | |
page.should have_content "Standard practice (select)" | |
page.should have_content "Bad practice (select)" | |
page.should have_content "Score: 2.0 out of 3.0 available points" | |
click_on "deselect_category_practice" | |
page.should have_content "Best practice (select)" | |
page.should have_content "Category practice (select)" | |
page.should have_content "Standard practice (select)" | |
page.should have_content "Bad practice (select)" | |
page.should have_content "Score: 0.0 out of 3.0 available points" | |
end | |
it "can complete and edit an additive survey" do | |
@mispellings.update_attributes :additive => true | |
visit competitor_survey_path(@competitor, @survey) | |
click_on "select_category_practice" | |
click_on "select_standard_practice" | |
@survey.results.select{|r| r.name == "Best practice"}.first.selected.should be_false | |
@survey.results.select{|r| r.name == "Category practice"}.first.selected.should be_true | |
@survey.results.select{|r| r.name == "Standard practice"}.first.selected.should be_true | |
@survey.results.select{|r| r.name == "Bad practice"}.first.selected.should be_false | |
visit competitor_survey_path(@competitor, @survey) | |
page.should have_content "Cope with mispellings?" | |
page.should have_content "On the homepage, there is:" | |
page.should have_content "Best practice (select)" | |
page.should have_content "Category practice (deselect)" | |
page.should have_content "Standard practice (deselect)" | |
page.should have_content "Bad practice (select)" | |
page.should have_content "Score: 3.0 out of 6.0 available points" | |
click_on "select_best_practice" | |
page.should have_content "Best practice (deselect)" | |
page.should have_content "Category practice (deselect)" | |
page.should have_content "Standard practice (deselect)" | |
page.should have_content "Bad practice (select)" | |
page.should have_content "Score: 6.0 out of 6.0 available points" | |
click_on "deselect_standard_practice" | |
page.should have_content "Best practice (deselect)" | |
page.should have_content "Category practice (deselect)" | |
page.should have_content "Standard practice (select)" | |
page.should have_content "Bad practice (select)" | |
page.should have_content "Score: 5.0 out of 6.0 available points" | |
end | |
end | |
describe "methods" do | |
describe "set_state" do | |
it "sets surveys to inactive if their questions are not active" do | |
@survey.question.update_attribute :active, false | |
@survey.reload | |
@survey.state_name.should == :inactive | |
end | |
it "sets surveys to inactive if their questions are not active, even if they have been answered" do | |
@survey.question.update_attributes :active => false | |
@survey.reload | |
@survey.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.postbacks.last.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"select_1"=>"option_2", "select_2"=>"option_2", "Sensible"=>"", "Bugs"=>""}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@survey.postbacks.last) | |
@survey.state_name.should == :inactive | |
end | |
it "sets surveys to active if their questions are active" do | |
@survey.state_name.should == :active | |
end | |
it "sets surveys to asked if their questions are active and the request has been sent to Houdini" do | |
@survey.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.state_name.should == :asked | |
end | |
it "sets surveys to answered if Houdini has sent a response" do | |
@survey.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.postbacks.last.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"select_1"=>"option_2", "select_2"=>"option_2", "Sensible"=>"", "Bugs"=>""}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@survey.postbacks.last) | |
@survey.state_name.should == :answered | |
end | |
it "sets surveys to dormant if they are waiting for products to be defined" do | |
@newquestion = @search.questions.create! :description => "something {{input-name}} something", :instructions => "foo", :element_id => @element.id, :standard_id => @standard.id, :active => true, :requires_data_from_other_question => true | |
@survey = @competitor.surveys.select{|s| s.question == @newquestion}.first | |
@survey.state_name.should == :dormant | |
end | |
it "sets surveys to dormant if they are waiting for products to be defined, even if their question has been asked" do | |
@newquestion = @search.questions.create! :description => "something {{input-name}} something", :instructions => "foo", :element_id => @element.id, :standard_id => @standard.id, :active => true, :requires_data_from_other_question => true | |
@f1 = @newquestion.fields.create! :name => "Best practice", :points => 3.0 | |
@f2 = @newquestion.fields.create! :name => "Category practice", :points => 2.0 | |
@survey = @competitor.surveys.select{|s| s.question == @newquestion}.first | |
@survey.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.state_name.should == :dormant | |
end | |
it "sets surveys to premature if they are waiting for products to be defined but they have been asked and answered" do | |
@newquestion = @search.questions.create! :description => "something {{input-name}} something", :instructions => "foo", :element_id => @element.id, :standard_id => @standard.id, :active => true, :requires_data_from_other_question => true | |
@f1 = @newquestion.fields.create! :name => "Best practice", :points => 3.0 | |
@f2 = @newquestion.fields.create! :name => "Category practice", :points => 2.0 | |
@survey = @competitor.surveys.select{|s| s.question == @newquestion}.first | |
@survey.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.postbacks.last.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"select_1"=>"option_2", "select_2"=>"option_2", "Sensible"=>"", "Bugs"=>""}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@survey.postbacks.last) | |
@survey.state_name.should == :premature | |
end | |
it "sets surveys to dormant if they are waiting for products to be found" do | |
@input = Input.create! :name => "Lord Of The Flies", :mispelling => "Lord Of The Flys", :sku => "123", :brand => "brand" | |
@newquestion = @search.questions.create! :description => "something {{input-name}} something", :instructions => "foo", :element_id => @element.id, :standard_id => @standard.id, :active => true, :requires_data_from_other_question => true | |
@category = Category.create! :name => "Books" | |
@input.categories << @category | |
@competitor.categories << @category | |
@survey = @competitor.surveys.select{|s| s.question == @newquestion}.first | |
@survey.state_name.should == :dormant | |
end | |
it "sets surveys to active if their products have been found and they are otherwise active" do | |
@input = Input.create! :name => "Lord Of The Flies", :mispelling => "Lord Of The Flys", :sku => "123", :brand => "brand" | |
@newquestion = @search.questions.create! :description => "something {{input-name}} something", :instructions => "foo", :element_id => @element.id, :standard_id => @standard.id, :active => true, :requires_data_from_other_question => true | |
@survey = @competitor.surveys.select{|s| s.question == @newquestion}.first | |
@category = Category.create! :name => "Books" | |
@competitor.categories << @category | |
@input.categories << @category | |
@competitor.surveys.select{|s| s.input_id == @input.id}.first.results.select{|r| r.name.include?("sells this product and it is in stock")}.first.update_attribute :selected, true | |
@survey = @competitor.surveys.select{|s| s.question == @newquestion}.first | |
@survey.reload | |
@survey.state_name.should == :active | |
end | |
it "sets surveys to asked if their products have been found, they are otherwise active, and they have been asked" do | |
@input = Input.create! :name => "Lord Of The Flies", :mispelling => "Lord Of The Flys", :sku => "123", :brand => "brand" | |
@newquestion = @search.questions.create! :description => "something {{input-name}} something", :instructions => "foo", :element_id => @element.id, :standard_id => @standard.id, :active => true, :requires_data_from_other_question => true | |
@f1 = @newquestion.fields.create! :name => "Best practice", :points => 3.0 | |
@f2 = @newquestion.fields.create! :name => "Category practice", :points => 2.0 | |
@survey = @competitor.surveys.select{|s| s.question == @newquestion}.first | |
@category = Category.create! :name => "Books" | |
@input.categories << @category | |
@competitor.categories << @category | |
@finder = @competitor.surveys.select{|s| s.input_id == @input.id}.first | |
@finder.description.should == "Does the site stock Lord Of The Flies?" | |
@result = @finder.results.select{|r| r.name.include?("sells this product and it is in stock")}.first | |
@result.update_attributes :selected => true | |
@result.save | |
@survey.reload | |
@survey.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.reload | |
@survey.state_name.should == :asked | |
end | |
describe "when the question is based on an input" do | |
before :each do | |
@input = Input.create! :name => "Lord Of The Flies", :mispelling => "Lord Of The Flys", :sku => "123", :brand => "brand" | |
@category = Category.create! :name => "Books" | |
@question = @input.questions.first | |
@survey = @question.surveys.select{|s| s.competitor == @competitor}.first | |
end | |
it "sets a survey to inactive if it's based on a product that does not match this competitor" do | |
@survey.state_name.should == :inactive | |
end | |
it "sets a survey to active if it's based on a product that does match this competitor" do | |
@competitor.categories << @category | |
@input.categories << @category | |
@survey.reload.state_name.should == :active | |
end | |
it "sets a survey to asked if it's based on a product that does match this competitor and the question has been asked" do | |
@competitor.categories << @category | |
@input.categories << @category | |
@survey.reload.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.state_name.should == :asked | |
end | |
it "sets a survey to answered if it's based on a product that does match this competitor and the question has been asked and answered" do | |
@competitor.categories << @category | |
@input.categories << @category | |
@survey.reload.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.postbacks.last.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"select_1"=>"option_2", "select_2"=>"option_2", "Sensible"=>"", "Bugs"=>""}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@survey.postbacks.last) | |
@survey.state_name.should == :answered | |
end | |
it "sets a survey to inactive if it's based on a product that does not match this competitor but the question has been asked - v1" do | |
@survey.reload.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.state_name.should == :inactive | |
end | |
it "sets a survey to inactive if it's based on a product that does not match this competitor but the question has been asked - v2" do | |
@competitor.categories << @category | |
@input.categories << @category | |
@category.destroy | |
@survey.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.reload.state_name.should == :inactive | |
end | |
it "sets a survey to inactive if it's based on a product that does not match this competitor but the question has been asked and answered - v1" do | |
@survey.reload.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.postbacks.last.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"select_1"=>"option_2", "select_2"=>"option_2", "Sensible"=>"", "Bugs"=>""}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@survey.postbacks.last) | |
@survey.state_name.should == :inactive | |
end | |
it "sets a survey to inactive if it's based on a product that does not match this competitor but the question has been asked and answered - v2" do | |
@competitor.categories << @category | |
@input.categories << @category | |
@category.destroy | |
@survey.reload.postbacks.create! | |
@survey.use_the_right_blueprint | |
@survey.postbacks.last.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"select_1"=>"option_2", "select_2"=>"option_2", "Sensible"=>"", "Bugs"=>""}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@survey.postbacks.last) | |
@category.delete | |
@survey.state_name.should == :inactive | |
end | |
end | |
end | |
describe "use_the_right_blueprint" do | |
it "should pick two_radio_buttons_simple for a two-button survey" do | |
@f3.destroy | |
@f4.destroy | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@survey.use_the_right_blueprint.should == "two_radio_buttons_simple" | |
end | |
it "should pick three_radio_buttons_simple for a three-button survey" do | |
@f4.destroy | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@survey.use_the_right_blueprint.should == "three_radio_buttons_simple" | |
end | |
it "should pick two_checkboxes_simple for a two-checkbox survey" do | |
@f3.destroy | |
@f4.destroy | |
@mispellings.update_attributes :additive => true | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@survey.use_the_right_blueprint.should == "two_select_menus_simple" | |
end | |
it "should pick three_checkboxes_simple for a three-checkbox survey" do | |
@f4.destroy | |
@mispellings.update_attributes :additive => true | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@survey.use_the_right_blueprint.should == "three_select_menus_simple" | |
end | |
end | |
describe "update_survey" do | |
it "should update survey with a first-past-the-post postback with the first item selected" do | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@postback = @survey.postbacks.create! | |
@postback.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"Pick_one"=>"option_1", "Sensible"=>"clear", "Bugs"=>"buggy"}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@postback) | |
@survey.bugs.should == "buggy" | |
@survey.oddities.should == "clear" | |
@survey.results.select{|r| r.field == @f1}.first.selected.should be_true | |
@survey.results.select{|r| r.field == @f2}.first.selected.should be_false | |
@survey.results.select{|r| r.field == @f3}.first.selected.should be_false | |
@survey.results.select{|r| r.field == @f4}.first.selected.should be_false | |
end | |
it "should update survey with a first-past-the-post postback with the second item selected" do | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@postback = @survey.postbacks.create! | |
@postback.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"Pick_one"=>"option_2", "Sensible"=>"clear", "Bugs"=>"buggy"}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@postback) | |
@survey.bugs.should == "buggy" | |
@survey.oddities.should == "clear" | |
@survey.results.select{|r| r.field == @f1}.first.selected.should be_false | |
@survey.results.select{|r| r.field == @f2}.first.selected.should be_true | |
@survey.results.select{|r| r.field == @f3}.first.selected.should be_false | |
@survey.results.select{|r| r.field == @f4}.first.selected.should be_false | |
end | |
it "should update survey with a first-past-the-post postback with the third item selected" do | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@postback = @survey.postbacks.create! | |
@postback.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"Pick_one"=>"option_3", "Sensible"=>"clear", "Bugs"=>"buggy"}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@postback) | |
@survey.bugs.should == "buggy" | |
@survey.oddities.should == "clear" | |
@survey.results.select{|r| r.field == @f1}.first.selected.should be_false | |
@survey.results.select{|r| r.field == @f2}.first.selected.should be_false | |
@survey.results.select{|r| r.field == @f3}.first.selected.should be_true | |
@survey.results.select{|r| r.field == @f4}.first.selected.should be_false | |
end | |
it "should update survey with an additive postback with the first item selected" do | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@postback = @survey.postbacks.create! | |
@postback.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"select_1"=>"option_1", "select_2"=>"option_2", "select_3"=>"option_2", "Sensible"=>"", "Bugs"=>""}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@postback) | |
@survey.results.select{|r| r.field == @f1}.first.selected.should be_false | |
@survey.results.select{|r| r.field == @f2}.first.selected.should be_true | |
@survey.results.select{|r| r.field == @f3}.first.selected.should be_true | |
@survey.results.select{|r| r.field == @f4}.first.selected.should be_false | |
end | |
it "should update survey with an additive postback with the second item selected -- different test data" do | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@postback = @survey.postbacks.create! | |
@postback.update_attributes :content => {"api_key"=>"key", "id"=>"#{@survey.id}", "verbose_output"=>{"select_1"=>"option_2", "select_2"=>"option_2", "select_3"=>"option_2", "Sensible"=>"", "Bugs"=>""}, "controller"=>"surveys", "action"=>"receive"} | |
@survey.update_from_postback(@postback) | |
@survey.results.select{|r| r.field == @f1}.first.selected.should be_true | |
@survey.results.select{|r| r.field == @f2}.first.selected.should be_true | |
@survey.results.select{|r| r.field == @f3}.first.selected.should be_true | |
@survey.results.select{|r| r.field == @f4}.first.selected.should be_false | |
end | |
end | |
describe "create_matching_results" do | |
it "should create a result for each field when it creates a survey" do | |
@survey1 = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@survey1.results.select{|result| result.name == @f1.name}.count.should == 1 | |
@survey2 = @anothercomp.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@survey2.results.select{|result| result.name == @f1.name}.count.should == 1 | |
end | |
end | |
describe "answered" do | |
it "should return false for unanswered questions" do | |
@mispellings = @search.questions.create! :description => "Cope with mispellings?", :instructions => "On the homepage, there is:", :element_id => @element.id, :standard_id => @standard.id, :active => true | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@survey.answered.should be_false | |
end | |
it "should return true for answered questions" do | |
@survey = @competitor.surveys.select{|survey| survey.name == "Cope with mispellings?"}.first | |
@survey.results.first.update_attributes :selected => true | |
@survey.answered.should be_true | |
end | |
end | |
describe "create_matching_condition_and_precondition and level" do | |
before :each do | |
@q0 = @search.questions.create! :description => "q0", :element_id => @element.id, :standard_id => @standard.id, :active => true | |
@f0 = @q0.fields.create! :name => "f0" | |
@q1 = @search.questions.create! :description => "q1", :element_id => @element.id, :standard_id => @standard.id, :active => true, :precondition_id => @q0.id, :condition_id => @f0.id | |
@f1 = @q1.fields.create! :name => "f1" | |
@q2 = @search.questions.create! :description => "q2", :element_id => @element.id, :standard_id => @standard.id, :active => true, :precondition_id => @q1.id, :condition_id => @f1.id | |
@f2 = @q2.fields.create! :name => "f2" | |
@q3 = @search.questions.create! :description => "q3", :element_id => @element.id, :standard_id => @standard.id, :active => true, :precondition_id => @q2.id, :condition_id => @f2.id | |
@f3 = @q3.fields.create! :name => "f3" | |
@q4 = @search.questions.create! :description => "q4", :element_id => @element.id, :standard_id => @standard.id, :active => true, :precondition_id => @q3.id, :condition_id => @f3.id | |
@s0 = @q0.surveys.select{|survey| survey.competitor == @competitor}.first | |
@s1 = @q1.surveys.select{|survey| survey.competitor == @competitor}.first | |
@s2 = @q2.surveys.select{|survey| survey.competitor == @competitor}.first | |
@s3 = @q3.surveys.select{|survey| survey.competitor == @competitor}.first | |
@s4 = @q4.surveys.select{|survey| survey.competitor == @competitor}.first | |
end | |
describe "create_matching_condition_and_precondition" do | |
it "should correctly set dependences, conditions and preconditions" do | |
@s0.dependencies.should == [@s1] | |
@s1.dependencies.should == [@s2] | |
@s2.dependencies.should == [@s3] | |
@s3.dependencies.should == [@s4] | |
@s0.precondition.should == nil | |
@s1.precondition.should == @s0 | |
@s2.precondition.should == @s1 | |
@s3.precondition.should == @s2 | |
@s4.precondition.should == @s3 | |
end | |
end | |
describe "level" do | |
it "should correctly set levels 0, 1, 2, 3 and 4" do | |
@s0.level.should == 0 | |
@s1.level.should == 1 | |
@s2.level.should == 2 | |
@s3.level.should == 3 | |
@s4.level.should == 4 | |
end | |
end | |
end | |
describe "active" do | |
it "survey is inactive when question is inactive" do | |
@inactive = @anothercomp.review.product.questions.create! :description => "inactive question", :element_id => @element.id, :standard_id => @standard.id | |
@question = @anothercomp.review.product.questions.find_by_description("inactive question") | |
@question.present?.should be_true | |
@anothercomp.surveys.find_by_question_id(@inactive.id).description.should == "inactive question" | |
@anothercomp.surveys.find_by_question_id(@inactive.id).inactive?.should be_true | |
end | |
it "survey is active when question is active" do | |
@anothercomp.review.product.questions.find_by_description("Cope with mispellings?").present?.should be_true | |
@anothercomp.surveys.find_by_question_id(Question.find_by_description("Cope with mispellings?").id).present?.should be_true | |
@anothercomp.surveys.find_by_question_id(@mispellings.id).description.should == "Cope with mispellings?" | |
@anothercomp.surveys.find_by_question_id(@mispellings.id).active?.should be_true | |
end | |
it "survey is active when question is based on inputs" do | |
@finance = Category.create! :name => "finance" | |
@super = Input.create! :name => "Superannuation", :mispelling => "Super", :sku => "123", :brand => "brand" | |
@super.categories << @finance | |
@anothercomp.categories << @finance | |
@survey = @anothercomp.reload.surveys.select{|survey| survey.description == "Does the site stock Superannuation?"}.first | |
@survey.active?.should be_true | |
end | |
end | |
describe "description" do | |
it "swaps in the name of a product if one is present" do | |
@survey = @anothercomp.surveys.first | |
@survey.question.update_column :requires_data_from_other_question, true | |
@input = Input.create! :name => "Mona Lisa Overdrive", :mispelling => "mona", :sku => "123", :brand => "brand" | |
@survey.inputs << @input | |
@survey.question.stub(:description => "What is {{input-name}}?") | |
@survey.description.should == "What is Mona Lisa Overdrive?" | |
end | |
it "passes through the question's description if no product is present" do | |
@survey = @anothercomp.surveys.first | |
@survey.question.stub(:description => "What is {{input-name}}?") | |
@survey.description.should == "What is {{input-name}}?" | |
end | |
end | |
describe "create_matching_results" do | |
it "has a result for each field in the corresponding question" do | |
@question = @anothercomp.review.product.questions.find_by_description("Cope with mispellings?") | |
@survey = @anothercomp.surveys.find_by_question_id(@question.id) | |
@survey.results.find_by_field_id(@question.fields.first.id).present?.should be_true | |
@question.fields.count.should == @survey.results.count | |
end | |
it "gets a new result for each field added to the corresponding question" do | |
@survey = @anothercomp.surveys.find_by_question_id(Question.find_by_description("Cope with mispellings?").id) | |
@question = @survey.question | |
@newfield = @question.fields.create! :name => "new field" | |
@survey.results.find_by_field_id(@newfield.id).name.should == "new field" | |
end | |
end | |
describe ":dependent => :destroy" do | |
it "loses a result for each field deleted from the corresponding question" do | |
@question = @anothercomp.review.product.questions.find_by_description("Cope with mispellings?") | |
@survey = @anothercomp.surveys.find_by_question_id(Question.find_by_description("Cope with mispellings?").id) | |
@field_id = @question.fields.last.id | |
@question.fields.last.destroy | |
@survey.results.find_by_field_id(@field_id).present?.should be_false | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment