Permalink
Browse files

Added test for presence of CSV file

  • Loading branch information...
1 parent 584e587 commit 40075f91c2587285dbf15f2599a1ec08715fb744 @mattfordham committed Jun 5, 2012
Showing with 12 additions and 1 deletion.
  1. +5 −0 lib/csv_validator.rb
  2. +7 −1 spec/csv_validator_spec.rb
@@ -13,6 +13,11 @@ def self.default_options
def validate_each(record, attribute, value)
options = @@default_options.merge(self.options)
+ unless value
+ record.errors.add(attribute, options[:message] || "must be present")
+ return
+ end
+
begin
csv = CSV.read(value.path)
rescue CSV::MalformedCSVError
@@ -22,6 +22,12 @@ class TestUser1 < TestModel
TestUser1.new(:csv => upload('support/3x6.csv')).should be_valid
end
+ it "should be invalid if no csv file given" do
+ testUser = TestUser1.new()
+ testUser.should have(1).error_on(:csv)
+ testUser.error_on(:csv)[0].should eq("must be present")
+ end
+
it "should be invalid due to maformed CSV" do
testUser = TestUser1.new(:csv => upload('support/not_csv.png'))
testUser.should have(1).error_on(:csv)
@@ -137,7 +143,7 @@ class TestUser10 < TestModel
end
class TestUser11 < TestModel
- validates :csv, :csv => {:email => 0}
+ validates :csv, :csv => {:email => 0}
end
class TestUser12 < TestModel

0 comments on commit 40075f9

Please sign in to comment.