Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Switched to ActionDispatch upload

  • Loading branch information...
commit 584e587c59f60c4483dccd8bcd5833bb3ec98e8c 1 parent cd7f103
Matt Fordham authored
1  Gemfile.lock
@@ -2,6 +2,7 @@ PATH
2 2 remote: .
3 3 specs:
4 4 csv_validator (0.0.1)
  5 + actionpack
5 6 activemodel
6 7 mail
7 8
1  csv_file_validator.gemspec
@@ -14,6 +14,7 @@ Gem::Specification.new do |s|
14 14
15 15 s.require_paths = %w(lib)
16 16 s.add_dependency("activemodel", ">= 0")
  17 + s.add_dependency("actionpack", ">= 0")
17 18 s.add_dependency "mail"
18 19 s.add_development_dependency("rake")
19 20 s.add_development_dependency("rspec", ">= 0")
2  lib/csv_validator.rb
@@ -14,7 +14,7 @@ def validate_each(record, attribute, value)
14 14 options = @@default_options.merge(self.options)
15 15
16 16 begin
17   - csv = CSV.read(value)
  17 + csv = CSV.read(value.path)
18 18 rescue CSV::MalformedCSVError
19 19 record.errors.add(attribute, options[:message] || "is not a valid CSV file")
20 20 return
67 spec/csv_validator_spec.rb
... ... @@ -1,7 +1,17 @@
1 1 require 'spec_helper'
2 2
  3 +module Helpers
  4 + def upload(file)
  5 + csv_file = File.open(File.join(File.dirname(__FILE__), file))
  6 + return ActionDispatch::Http::UploadedFile.new(:tempfile => csv_file, :filename => File.basename(csv_file))
  7 + end
  8 +end
  9 +
  10 +
3 11 describe CsvValidator do
4 12
  13 + include Helpers
  14 +
5 15 describe "general validation" do
6 16
7 17 class TestUser1 < TestModel
@@ -9,13 +19,11 @@ class TestUser1 < TestModel
9 19 end
10 20
11 21 it "should be valid" do
12   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
13   - TestUser1.new(:csv => csv_file).should be_valid
  22 + TestUser1.new(:csv => upload('support/3x6.csv')).should be_valid
14 23 end
15 24
16 25 it "should be invalid due to maformed CSV" do
17   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/not_csv.png'))
18   - testUser = TestUser1.new(:csv => csv_file)
  26 + testUser = TestUser1.new(:csv => upload('support/not_csv.png'))
19 27 testUser.should have(1).error_on(:csv)
20 28 testUser.error_on(:csv)[0].should eq("is not a valid CSV file")
21 29 end
@@ -41,39 +49,33 @@ class TestUser5 < TestModel
41 49 end
42 50
43 51 it "should be invalid due to exact column count" do
44   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/2x6.csv'))
45   - testUser = TestUser2.new(:csv => csv_file)
  52 + testUser = TestUser2.new(:csv => upload('support/2x6.csv'))
46 53 testUser.should have(1).error_on(:csv)
47 54 testUser.error_on(:csv)[0].should eq("should have 3 columns")
48 55 end
49 56
50 57 it "should be valid with exact column count" do
51   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
52   - TestUser2.new(:csv => csv_file).should be_valid
  58 + TestUser2.new(:csv => upload('support/3x6.csv')).should be_valid
53 59 end
54 60
55 61 it "should be invalid due to max column count" do
56   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
57   - testUser = TestUser3.new(:csv => csv_file)
  62 + testUser = TestUser3.new(:csv => upload('support/3x6.csv'))
58 63 testUser.should have(1).error_on(:csv)
59 64 testUser.error_on(:csv)[0].should eq("should have no more than 2 columns")
60 65 end
61 66
62 67 it "should be valid with max column count" do
63   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/2x6.csv'))
64   - TestUser3.new(:csv => csv_file).should be_valid
  68 + TestUser3.new(:csv => upload('support/2x6.csv')).should be_valid
65 69 end
66 70
67 71 it "should be invalid due to min column count" do
68   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
69   - testUser = TestUser4.new(:csv => csv_file)
  72 + testUser = TestUser4.new(:csv => upload('support/3x6.csv'))
70 73 testUser.should have(1).error_on(:csv)
71 74 testUser.error_on(:csv)[0].should eq("should have at least 15 columns")
72 75 end
73 76
74 77 it "should be valid with min column count" do
75   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
76   - TestUser5.new(:csv => csv_file).should be_valid
  78 + TestUser5.new(:csv => upload('support/3x6.csv')).should be_valid
77 79 end
78 80
79 81 end
@@ -97,39 +99,33 @@ class TestUser9 < TestModel
97 99 end
98 100
99 101 it "should be valid with exact row count" do
100   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
101   - TestUser6.new(:csv => csv_file).should be_valid
  102 + TestUser6.new(:csv => upload('support/3x6.csv')).should be_valid
102 103 end
103 104
104 105 it "should be invalid due to exact row count" do
105   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x7.csv'))
106   - testUser = TestUser6.new(:csv => csv_file)
  106 + testUser = TestUser6.new(:csv => upload('support/3x7.csv'))
107 107 testUser.should have(1).error_on(:csv)
108 108 testUser.error_on(:csv)[0].should eq("should have 6 rows")
109 109 end
110 110
111 111 it "should be invalid due to min row count" do
112   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
113   - testUser = TestUser7.new(:csv => csv_file)
  112 + testUser = TestUser7.new(:csv => upload('support/3x6.csv'))
114 113 testUser.should have(1).error_on(:csv)
115 114 testUser.error_on(:csv)[0].should eq("should have at least 10 rows")
116 115 end
117 116
118 117 it "should be valid with min row count" do
119   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
120   - TestUser8.new(:csv => csv_file).should be_valid
  118 + TestUser8.new(:csv => upload('support/3x6.csv')).should be_valid
121 119 end
122 120
123 121 it "should be invalid due to max row count" do
124   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x7.csv'))
125   - testUser = TestUser9.new(:csv => csv_file)
  122 + testUser = TestUser9.new(:csv => upload('support/3x7.csv'))
126 123 testUser.should have(1).error_on(:csv)
127 124 testUser.error_on(:csv)[0].should eq("should have no more than 6 rows")
128 125 end
129 126
130 127 it "should be valid with max row count" do
131   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
132   - TestUser9.new(:csv => csv_file).should be_valid
  128 + TestUser9.new(:csv => upload('support/3x6.csv')).should be_valid
133 129 end
134 130
135 131 end
@@ -153,29 +149,26 @@ class TestUser13 < TestModel
153 149 end
154 150
155 151 it "should be invalid with a column specified as containing only emails" do
156   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
157   - testUser = TestUser10.new(:csv => csv_file)
  152 + testUser = TestUser10.new(:csv => upload('support/3x6.csv'))
158 153 testUser.should have(1).error_on(:csv)
159 154 testUser.error_on(:csv)[0].should include("contains invalid emails")
160 155 end
161 156
162 157 it "should be valid with a column specified as containing only emails" do
163   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
164   - TestUser11.new(:csv => csv_file).should be_valid
  158 + TestUser11.new(:csv => upload('support/3x6.csv')).should be_valid
165 159 end
166 160
167 161 it "should be valid with a column specified as containing only numbers" do
168   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
169   - TestUser12.new(:csv => csv_file).should be_valid
  162 + TestUser12.new(:csv => upload('support/3x6.csv')).should be_valid
170 163 end
171 164
172 165 it "should be invalid with a column specified as containing only numbers" do
173   - csv_file = File.open(File.join(File.dirname(__FILE__), 'support/3x6.csv'))
174   - testUser = TestUser13.new(:csv => csv_file)
  166 + testUser = TestUser13.new(:csv => upload('support/3x6.csv'))
175 167 testUser.should have(1).error_on(:csv)
176 168 testUser.error_on(:csv)[0].should include("contains non-numeric content in column 0")
177 169 end
178 170
179 171 end
180 172
181   -end
  173 +end
  174 +
1  spec/spec_helper.rb
... ... @@ -1,6 +1,7 @@
1 1 require 'rubygems'
2 2 require 'rspec'
3 3 require 'active_model'
  4 +require 'action_dispatch'
4 5 require 'rspec/rails/extensions'
5 6
6 7 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

0 comments on commit 584e587

Please sign in to comment.
Something went wrong with that request. Please try again.