Skip to content

Commit 79eca40

Browse files
committed
specs for failed imports, validations
1 parent ed138f2 commit 79eca40

File tree

5 files changed

+39
-4
lines changed

5 files changed

+39
-4
lines changed

lib/active_admin_import/dsl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def active_admin_import(options = {}, &block)
8888
else
8989
instance_exec result, options, &DEFAULT_RESULT_PROC
9090
end
91-
rescue ActiveRecord::Import::MissingColumnError, NoMethodError => e
91+
rescue ActiveRecord::Import::MissingColumnError, NoMethodError, ActiveRecord::StatementInvalid => e
9292
flash[:error] = I18n.t('active_admin_import.file_error', message: e.message)
9393
end
9494
redirect_to options[:back]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Name,Last name,Birthday
2+
,Doe,1986-05-01
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name,Last name,Birthday
2+
John,Doe,1986-05-01
3+
John,Doe,1986-05-01

spec/import_spec.rb

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def upload_file!(name, ext='csv')
5656
end
5757

5858

59-
6059
end
6160

6261
context "with valid options" do
@@ -149,9 +148,39 @@ def upload_file!(name, ext='csv')
149148
expect(Author.count).to eq(0)
150149
end
151150
end
151+
end
152152

153+
context "with invalid data insert" do
154+
it "should render error" do
155+
upload_file!(:authors_invalid_db)
156+
expect(page).to have_content "Error:"
157+
expect(Author.count).to eq(0)
158+
end
153159
end
154160

161+
context "with invalid records" do
162+
163+
context "with validation" do
164+
it "should render error" do
165+
upload_file!(:author_invalid)
166+
expect(page).to have_content "Failed to import 1 author"
167+
expect(Author.count).to eq(0)
168+
end
169+
end
170+
171+
172+
context "without validation" do
173+
let(:options) { {validate: false} }
174+
it "should render error" do
175+
upload_file!(:author_invalid)
176+
expect(page).to have_content "Successfully imported 1 author"
177+
expect(Author.count).to eq(1)
178+
end
179+
180+
end
181+
182+
183+
end
155184

156185
context "when zipped" do
157186
context "when allowed" do

spec/support/rails_template.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Rails template to build the sample app for specs
22

3-
generate :model, 'author name:string last_name:string birthday:date'
4-
3+
generate :model, 'author name:string{10}:uniq last_name:string birthday:date'
54

5+
#Add validation
6+
inject_into_file "app/models/author.rb", " validates_presence_of :name\n", after: "Base\n"
67

78
# Configure default_url_options in test environment
89
inject_into_file "config/environments/test.rb", " config.action_mailer.default_url_options = { :host => 'example.com' }\n", after: "config.cache_classes = true\n"

0 commit comments

Comments
 (0)