spec/resource_spec.rb in survey-gizmo-ruby-4.1.0 vs spec/resource_spec.rb in survey-gizmo-ruby-5.0.2

- old
+ new

@@ -66,10 +66,18 @@ it 'should parse the number of completed records correctly' do survey = described_class.new('statistics' => [['Partial', 2], ['Disqualified', 28], ['Complete', 15]]) expect(survey.number_of_completed_responses).to eq(15) end + + it 'should determine if there are new results' do + stub_request(:get, /#{@base}\/survey\/1\/surveyresponse/).to_return(json_response(true, [])) + + survey = described_class.new(id: 1) + expect(survey.server_has_new_results_since?(Time.now)).to be_false + a_request(:get, /#{@base}\/survey\/1\/surveyresponse/).should have_been_made + end end describe SurveyGizmo::API::Question do let(:base_params) { {survey_id: 1234, page_id: 1} } let(:create_attributes) { base_params.merge(title: 'Spec Question', type: 'radio', properties: { 'required' => true, 'option_sort' => false }) } @@ -93,31 +101,92 @@ it 'should handle the _subtype key' do described_class.new(:_subtype => 'radio').type.should == 'radio' end - it 'should have no subquestions' do - expect(described_class.new.sub_questions).to eq([]) - end - it 'should find the survey' do stub_request(:get, /#{@base}\/survey\/1234/).to_return(json_response(true, get_attributes)) described_class.new(base_params).survey a_request(:get, /#{@base}\/survey\/1234/).should have_been_made end - context 'with subquestions' do + context 'options' do + let(:survey_id) { 15 } + let(:question_id) { 23 } + let(:body_data) do + { + "id" => question_id, + "title" => {"English"=>"How likely are you to bang your head to Bohemian Rhapsody?"}, + "options"=> + [ + { + "id" => 10014, + "title" => {"English"=>"0 = Not at all likely"}, + "value" => "0 = Not at all likely" + }, + { + "id" => 10015, + "title" => {"English"=>"1"}, + "value" => "1" + } + ] + } + end + + context 'option parsing' do + before do + stub_request(:get, /#{@base}\/survey\/#{survey_id}\/surveyquestion\/#{question_id}/).to_return(json_response(true, body_data)) + end + + it 'correctly parses options out of question data' do + question = described_class.first(survey_id: survey_id, id: question_id) + expect(question.options.all? { |o| o.question_id == question_id && o.survey_id == survey_id }).to be_true + expect(question.options.map { |o| o.id }).to eq([10014, 10015]) + a_request(:get, /#{@base}\/.*surveyoption/).should_not have_been_made + end + + it 'correctly parses sub question options' do + question = described_class.new(survey_id: survey_id, id: question_id + 1, parent_question_id: question_id) + expect(question.parent_question.id).to eq(described_class.new(body_data).id) + expect(question.options.all? { |o| o.question_id == question.id && o.survey_id == survey_id }).to be_true + expect(question.options.map { |o| o.id }).to eq([10014, 10015]) + a_request(:get, /#{@base}\/survey\/#{survey_id}\/surveyquestion\/#{question_id}/).should have_been_made + end + end + end + + context 'subquestions' do let(:parent_id) { 33 } - let(:question_with_subquestions) { described_class.new(id: parent_id, survey_id: 1234, sub_question_skus: [1, 2]) } + let(:skus) { [544, 322] } + let(:question_with_subquestions) { described_class.new(id: parent_id, survey_id: 1234, sub_question_skus: skus) } + it 'should have no subquestions' do + expect(described_class.new.sub_questions).to eq([]) + end + it 'should have 2 subquestions and they should have the right parent question' do stub_request(:get, /#{@base}/).to_return(json_response(true, get_attributes)) expect(question_with_subquestions.sub_questions.size).to eq(2) question_with_subquestions.sub_questions.first.parent_question a_request(:get, /#{@base}\/survey\/1234\/surveyquestion\/#{parent_id}/).should have_been_made + skus.each { |sku| a_request(:get, /#{@base}\/survey\/1234\/surveyquestion\/#{sku}/).should have_been_made } end + + context 'and shortname' do + let(:sku) { 6 } + let(:question_with_subquestions) { described_class.new(id: parent_id, survey_id: 1234, sub_question_skus: [["0", sku], ["foo", 8]]) } + + it 'should have 2 subquestions and they should have the right parent question' do + stub_request(:get, /#{@base}/).to_return(json_response(true, get_attributes)) + expect(question_with_subquestions.sub_questions.size).to eq(2) + + question_with_subquestions.sub_questions.first.parent_question + a_request(:get, /#{@base}\/survey\/1234\/surveyquestion\/#{parent_id}/).should have_been_made + a_request(:get, /#{@base}\/survey\/1234\/surveyquestion\/#{sku}/).should have_been_made + end + end end end describe SurveyGizmo::API::Option do let(:survey_and_page) { {survey_id: 1234, page_id: 1} } @@ -163,9 +232,48 @@ h } it_should_behave_like 'an API object' it_should_behave_like 'an object with errors' + + context 'answers' do + let(:survey_id) { 6 } + let(:response_id) { 7 } + let(:timestamp) { '2015-01-02'.to_time(:utc) } + let(:answers) do + { + "[question(3), option(\"10021-other\")]" => "Some other text field answer", + "[question(3), option(10021)]" => "Other (required)", + "[question(5)]" => "VERY important", + "[question(6)]" => nil, + "[question(7), option(10001)]" => nil, + "[question(8)]" => false, + "[question(9), option(10002)]" => '16', + "[question(10), question_pipe(\"Que aplicaciĆ³n\")]" => "5 = Extremely important", + "[question(11), option(10001)]" => "" + } + end + + it 'should propagate time, survey_id, and response_id' do + response = described_class.new( + answers: answers.select { |k,v| k == "[question(5)]"}, + survey_id: survey_id, + id: response_id, + submitted_at: timestamp + ) + expect(response.parsed_answers.map { |a| a.to_hash }).to eq([ { survey_id: survey_id, response_id: response_id, question_id: 5, answer_text: "VERY important", submitted_at: timestamp }]) + end + + it 'should parse the answers and remove extraneous answers' do + expect(described_class.new(answers: answers, survey_id: 1).parsed_answers.map { |a| a.to_hash }).to eq([ + { survey_id: 1, question_id: 3, option_id: 10021, other_text: "Some other text field answer" }, + { survey_id: 1, question_id: 5, answer_text: "VERY important" }, + { survey_id: 1, question_id: 8, answer_text: 'false' }, + { survey_id: 1, question_id: 9, option_id: 10002 }, + { survey_id: 1, question_id: 10, question_pipe: "Que aplicaciĆ³n", answer_text: "5 = Extremely important" } + ]) + end + end end describe SurveyGizmo::API::AccountTeams do pending('Need an account with admin privileges to test this') let(:create_attributes) { { teamid: 1234, teamname: 'team' } }