Skip to content

Commit

Permalink
Switched to ActionDispatch upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mattfordham committed Jun 5, 2012
1 parent cd7f103 commit 584e587
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 38 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Expand Up @@ -2,6 +2,7 @@ PATH
remote: .
specs:
csv_validator (0.0.1)
actionpack
activemodel
mail

Expand Down
1 change: 1 addition & 0 deletions csv_file_validator.gemspec
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion lib/csv_validator.rb
Expand Up @@ -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
Expand Down
67 changes: 30 additions & 37 deletions 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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

1 change: 1 addition & 0 deletions 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'))
Expand Down

0 comments on commit 584e587

Please sign in to comment.