From 67fbf37f19aa9fe470fc286fe048517e2d26ab44 Mon Sep 17 00:00:00 2001 From: Luke Baker Date: Thu, 11 Nov 2010 12:57:00 -0500 Subject: [PATCH] let FasterCSV do proper CSV escaping for strings --- app/models/question.rb | 2 +- spec/models/question_spec.rb | 74 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 1 deletion(-) diff --git a/app/models/question.rb b/app/models/question.rb index 0120a46..6ac6b79 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -556,7 +556,7 @@ class Question < ActiveRecord::Base num_skips = self.skips.count(:conditions => {:prompt_id => left_prompts_ids + right_prompts_ids}) - csv << [c.question_id, c.id, "'#{c.data.strip}'", c.wins, c.losses, num_skips, c.score, user_submitted , c.creator_id, c.created_at, c.updated_at, active, left_appearances, right_appearances] + csv << [c.question_id, c.id, c.data.strip, c.wins, c.losses, num_skips, c.score, user_submitted , c.creator_id, c.created_at, c.updated_at, active, left_appearances, right_appearances] end when 'non_votes' diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb index a2271b6..00d5117 100644 --- a/spec/models/question_spec.rb +++ b/spec/models/question_spec.rb @@ -423,4 +423,78 @@ describe Question do after(:all) { truncate_all } end + context "exporting data with odd characters" do + before(:all) do + @aoi_question = Factory.create(:question) + user = @aoi_question.site + + @aoi_question.it_should_autoactivate_ideas = true + @aoi_question.save! + + visitor = user.visitors.find_or_create_by_identifier('visitor identifier') + + user.create_choice(visitor.identifier, @aoi_question, + {:data => "foo\nbar", :local_identifier => "example creator"}) + user.create_choice(visitor.identifier, @aoi_question, + {:data => "foo,bar", :local_identifier => "example creator"}) + user.create_choice(visitor.identifier, @aoi_question, + {:data => "foo\"bar", :local_identifier => "example creator"}) + user.create_choice(visitor.identifier, @aoi_question, + {:data => "foo'bar", :local_identifier => "example creator"}) + + 40.times.each do |num| + @p = @aoi_question.picked_prompt + + @a = user.record_appearance(visitor, @p) + + vote_options = {:visitor_identifier => visitor.identifier, + :appearance_lookup => @a.lookup, + :prompt => @p, + :time_viewed => rand(1000), + :direction => (rand(2) == 0) ? "left" : "right" + } + + skip_options = {:visitor_identifier => visitor.identifier, + :appearance_lookup => @a.lookup, + :prompt => @p, + :time_viewed => rand(1000), + :skip_reason => "some reason" + } + + choice = rand(3) + case choice + when 0 + user.record_vote(vote_options) + when 1 + user.record_skip(skip_options) + when 2 + #this is an orphaned appearance, so do nothing + end + end + end + + it "should export idea data to a csv file" do + filename = @aoi_question.export('ideas') + + filename.should_not be nil + filename.should match /.*ideamarketplace_#{@aoi_question.id}_ideas[.]csv$/ + File.exists?(filename).should be_true + # Not specifying exact file syntax, it's likely to change frequently + # + rows = FasterCSV.read(filename) + rows.first.should include("Idea ID") + rows.first.should_not include("Skip ID") + + rows.shift + rows.each do |row| + row[2].should =~ /^foo.bar$/m + end + + #File.delete(filename).should_not be_nil + + end + + after(:all) { truncate_all } + end + end -- libgit2 0.21.2