Skip to content

Commit c2d9ed8

Browse files
committed
Update rails_template and add spec*
Add `validates_uniqueness_of :last_name` to *Author* model, because the original `validates_presence_of :name` barely works. ```ruby context "with invalid data insert" do it "should render error" do upload_file!(:authors_invalid_db) expect(page).to have_content "Error:" expect(Author.count).to eq(0) end end ``` The spec above catches an `Error: SQLite 3::ConstraintException: column name is not unique`, the really constraint is on DB level. ```ruby add_index "authors", ["name"], name: "index_authors_on_name", unique: true ```` The option `:batch_transaction` controls the transaction on model level, so I've add an rspec context "with invalid data insert on model validation".
1 parent d40df13 commit c2d9ed8

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Name,Last name,Birthday
2+
Jim,Doe,1986-05-01
3+
Jane,Roe,1986-05-01

spec/import_spec.rb

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,41 @@ def upload_file!(name, ext='csv')
292292
end
293293
end
294294

295-
context "with invalid data insert" do
295+
context "with invalid data insert on DB constraint" do
296+
# :name field has an uniq index
296297
it "should render error" do
297298
upload_file!(:authors_invalid_db)
298299
expect(page).to have_content "Error:"
299300
expect(Author.count).to eq(0)
300301
end
301302
end
302303

304+
context "with invalid data insert on model validation" do
305+
let(:options) { { validate: true } }
306+
307+
before do
308+
Author.create!(name: "John", last_name: "Doe")
309+
end
310+
311+
it "should render both successful and failed message" do
312+
upload_file!(:authors_invalid_model)
313+
expect(page).to have_content "Failed to import 1 author"
314+
expect(page).to have_content "Successfully imported 1 author"
315+
expect(Author.count).to eq(2)
316+
end
317+
318+
context "use batch_transaction to make transaction work on model validation" do
319+
let(:options) { { validate: true, batch_transaction: true } }
320+
321+
it "should render only the failed message" do
322+
upload_file!(:authors_invalid_model)
323+
expect(page).to have_content "Failed to import 1 author"
324+
expect(page).to_not have_content "Successfully imported"
325+
expect(Author.count).to eq(1)
326+
end
327+
end
328+
end
329+
303330
context "with invalid records" do
304331
context "with validation" do
305332
it "should render error" do
@@ -402,4 +429,4 @@ def upload_file!(name, ext='csv')
402429

403430
end
404431

405-
end
432+
end

spec/support/rails_template.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
generate :model, 'post title:string:uniq body:text author:references'
55

66
#Add validation
7-
inject_into_file "app/models/author.rb", " validates_presence_of :name\n", after: "Base\n"
7+
inject_into_file "app/models/author.rb", " validates_presence_of :name\n validates_uniqueness_of :last_name\n", after: "Base\n"
88
inject_into_file "app/models/post.rb", " validates_presence_of :author\n", after: ":author\n"
99

1010
# Configure default_url_options in test environment

0 commit comments

Comments
 (0)