Browse files

Switched to ActionDispatch upload

  • Loading branch information...
1 parent cd7f103 commit 584e587c59f60c4483dccd8bcd5833bb3ec98e8c @mattfordham committed Jun 5, 2012
Showing with 34 additions and 38 deletions.
  1. +1 −0 Gemfile.lock
  2. +1 −0 csv_file_validator.gemspec
  3. +1 −1 lib/csv_validator.rb
  4. +30 −37 spec/csv_validator_spec.rb
  5. +1 −0 spec/spec_helper.rb
View
1 Gemfile.lock
@@ -2,6 +2,7 @@ PATH
remote: .
specs:
csv_validator (0.0.1)
+ actionpack
activemodel
mail
View
1 csv_file_validator.gemspec
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
s.require_paths = %w(lib)
s.add_dependency("activemodel", ">= 0")
+ s.add_dependency("actionpack", ">= 0")
s.add_dependency "mail"
s.add_development_dependency("rake")
s.add_development_dependency("rspec", ">= 0")
View
2 lib/csv_validator.rb
@@ -14,7 +14,7 @@ def validate_each(record, attribute, value)
options = @@default_options.merge(self.options)
begin
- csv = CSV.read(value)
+ csv = CSV.read(value.path)
rescue CSV::MalformedCSVError
record.errors.add(attribute, options[:message] || "is not a valid CSV file")
return
View
67 spec/csv_validator_spec.rb
@@ -1,21 +1,29 @@
require 'spec_helper'
+module Helpers
+ def upload(file)
+ csv_file = File.open(File.join(File.dirname(__FILE__), file))
+ return ActionDispatch::Http::UploadedFile.new(:tempfile => csv_file, :filename => File.basename(csv_file))
+ end
+end
+
+
describe CsvValidator do
+ include Helpers
+
describe "general validation" do
class TestUser1 < TestModel
validates :csv, :csv => true
end
it "should be valid" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- TestUser1.new(:csv => csv_file).should be_valid
+ TestUser1.new(:csv => upload('support/3x6.csv')).should be_valid
end
it "should be invalid due to maformed CSV" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/not_csv.png'))
- testUser = TestUser1.new(:csv => csv_file)
+ testUser = TestUser1.new(:csv => upload('support/not_csv.png'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should eq("is not a valid CSV file")
end
@@ -41,39 +49,33 @@ class TestUser5 < TestModel
end
it "should be invalid due to exact column count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/2x6.csv'))
- testUser = TestUser2.new(:csv => csv_file)
+ testUser = TestUser2.new(:csv => upload('support/2x6.csv'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should eq("should have 3 columns")
end
it "should be valid with exact column count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- TestUser2.new(:csv => csv_file).should be_valid
+ TestUser2.new(:csv => upload('support/3x6.csv')).should be_valid
end
it "should be invalid due to max column count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- testUser = TestUser3.new(:csv => csv_file)
+ testUser = TestUser3.new(:csv => upload('support/3x6.csv'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should eq("should have no more than 2 columns")
end
it "should be valid with max column count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/2x6.csv'))
- TestUser3.new(:csv => csv_file).should be_valid
+ TestUser3.new(:csv => upload('support/2x6.csv')).should be_valid
end
it "should be invalid due to min column count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- testUser = TestUser4.new(:csv => csv_file)
+ testUser = TestUser4.new(:csv => upload('support/3x6.csv'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should eq("should have at least 15 columns")
end
it "should be valid with min column count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- TestUser5.new(:csv => csv_file).should be_valid
+ TestUser5.new(:csv => upload('support/3x6.csv')).should be_valid
end
end
@@ -97,39 +99,33 @@ class TestUser9 < TestModel
end
it "should be valid with exact row count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- TestUser6.new(:csv => csv_file).should be_valid
+ TestUser6.new(:csv => upload('support/3x6.csv')).should be_valid
end
it "should be invalid due to exact row count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x7.csv'))
- testUser = TestUser6.new(:csv => csv_file)
+ testUser = TestUser6.new(:csv => upload('support/3x7.csv'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should eq("should have 6 rows")
end
it "should be invalid due to min row count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- testUser = TestUser7.new(:csv => csv_file)
+ testUser = TestUser7.new(:csv => upload('support/3x6.csv'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should eq("should have at least 10 rows")
end
it "should be valid with min row count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- TestUser8.new(:csv => csv_file).should be_valid
+ TestUser8.new(:csv => upload('support/3x6.csv')).should be_valid
end
it "should be invalid due to max row count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x7.csv'))
- testUser = TestUser9.new(:csv => csv_file)
+ testUser = TestUser9.new(:csv => upload('support/3x7.csv'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should eq("should have no more than 6 rows")
end
it "should be valid with max row count" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- TestUser9.new(:csv => csv_file).should be_valid
+ TestUser9.new(:csv => upload('support/3x6.csv')).should be_valid
end
end
@@ -153,29 +149,26 @@ class TestUser13 < TestModel
end
it "should be invalid with a column specified as containing only emails" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- testUser = TestUser10.new(:csv => csv_file)
+ testUser = TestUser10.new(:csv => upload('support/3x6.csv'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should include("contains invalid emails")
end
it "should be valid with a column specified as containing only emails" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- TestUser11.new(:csv => csv_file).should be_valid
+ TestUser11.new(:csv => upload('support/3x6.csv')).should be_valid
end
it "should be valid with a column specified as containing only numbers" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- TestUser12.new(:csv => csv_file).should be_valid
+ TestUser12.new(:csv => upload('support/3x6.csv')).should be_valid
end
it "should be invalid with a column specified as containing only numbers" do
- csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
- testUser = TestUser13.new(:csv => csv_file)
+ testUser = TestUser13.new(:csv => upload('support/3x6.csv'))
testUser.should have(1).error_on(:csv)
testUser.error_on(:csv)[0].should include("contains non-numeric content in column 0")
end
end
-end
+end
+
View
1 spec/spec_helper.rb
@@ -1,6 +1,7 @@
require 'rubygems'
require 'rspec'
require 'active_model'
+require 'action_dispatch'
require 'rspec/rails/extensions'
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

0 comments on commit 584e587

Please sign in to comment.