-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathimport_spec.rb
More file actions
734 lines (629 loc) · 24.1 KB
/
import_spec.rb
File metadata and controls
734 lines (629 loc) · 24.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
# frozen_string_literal: true
require 'spec_helper'
describe 'import', type: :feature do
shared_examples 'successful inserts' do |encoding, csv_file_name|
let(:options) do
attributes = { force_encoding: encoding }
{ template_object: ActiveAdminImport::Model.new(attributes) }
end
before do
upload_file!(csv_file_name)
end
it 'should import file with many records' do
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
Author.all.each do |author|
expect(author).to be_valid
expect(author.name).to be_present
expect(author.last_name).to be_present
end
end
end
def with_zipped_csv(name, &block)
zip_file = File.expand_path("./spec/fixtures/files/#{name}.zip")
begin
Zip::File.open(zip_file, create: true) do |z|
z.add "#{name}.csv", File.expand_path("./spec/fixtures/files/#{name}.csv")
end
instance_eval &block
ensure
begin
File.delete zip_file
rescue
nil
end
end
end
def upload_file!(name, ext = 'csv')
attach_file('active_admin_import_model_file', File.expand_path("./spec/fixtures/files/#{name}.#{ext}"))
find_button('Import').click
end
context 'posts index' do
before do
Author.create!(name: 'John', last_name: 'Doe')
Author.create!(name: 'Jane', last_name: 'Roe')
end
context 'for csv for particular author' do
let(:author) { Author.take }
shared_examples 'successful inserts for author' do
it 'should use predefined author_id' do
expect(Post.where(author_id: author.id).count).to eq(Post.count)
end
it 'should be imported' do
expect(Post.count).to eq(2)
expect(page).to have_content 'Successfully imported 2 posts'
end
end
context 'no headers' do
before do
add_post_resource(template_object: ActiveAdminImport::Model.new(author_id: author.id,
csv_headers: [:title, :body, :author_id]),
validate: true,
before_batch_import: lambda do |importer|
importer.csv_lines.map! { |row| row << importer.model.author_id }
end
)
visit '/admin/posts/import'
upload_file!(:posts_for_author_no_headers)
end
include_examples 'successful inserts for author'
end
context 'with headers' do
before do
add_post_resource(template_object: ActiveAdminImport::Model.new(author_id: author.id),
validate: true,
before_batch_import: lambda do |importer|
importer.csv_lines.map! { |row| row << importer.model.author_id }
importer.headers.merge!(:'Author Id' => :author_id)
end
)
visit '/admin/posts/import'
upload_file!(:posts_for_author)
end
include_examples 'successful inserts for author'
end
context 'when template object passed like proc' do
before do
add_post_resource(template_object: -> { ActiveAdminImport::Model.new(author_id: author.id) },
validate: true,
before_batch_import: lambda do |importer|
importer.csv_lines.map! { |row| row << importer.model.author_id }
importer.headers.merge!(:'Author Id' => :author_id)
end
)
visit '/admin/posts/import'
upload_file!(:posts_for_author)
end
include_examples 'successful inserts for author'
context 'after successful import try without file' do
let(:after_successful_import_do!) do
# reload page
visit '/admin/posts/import'
# submit form without file
find_button('Import').click
end
it 'should render validation error' do
after_successful_import_do!
expect(page).to have_content I18n.t('active_admin_import.no_file_error')
end
end
end
end
context 'for csv with author name' do
before do
add_post_resource(
validate: true,
template_object: ActiveAdminImport::Model.new,
headers_rewrites: { :'Author Name' => :author_id },
before_batch_import: lambda do |importer|
authors_names = importer.values_at(:author_id)
# replacing author name with author id
authors = Author.where(name: authors_names).pluck(:name, :id)
# {"Jane" => 2, "John" => 1}
options = Hash[*authors.flatten]
importer.batch_replace(:author_id, options)
end
)
visit '/admin/posts/import'
upload_file!(:posts)
end
it 'should resolve author_id by author name' do
Post.all.each do |post|
expect(Author.where(id: post.author.id)).to be_present
end
end
it 'should be imported' do
expect(Post.count).to eq(2)
expect(page).to have_content 'Successfully imported 2 posts'
end
end
end
context 'authors index' do
before do
add_author_resource
end
it 'should navigate to import page' do
# TODO: removing this causes undefined method `ransack' for #<ActiveRecord::Relation []>
allow_any_instance_of(Admin::AuthorsController).to receive(:find_collection).and_return(Author.all)
visit '/admin/authors'
find_link('Import Authors').click
expect(current_path).to eq('/admin/authors/import')
end
end
context 'with custom block' do
before do
add_author_resource({}) do
flash[:notice] = 'some custom message'
end
visit '/admin/authors/import'
end
it 'should display notice from custom block' do
upload_file!(:author)
expect(page).to have_content 'some custom message'
end
end
context 'authors already exist' do
before do
Author.create!(id: 1, name: 'Jane', last_name: 'Roe')
Author.create!(id: 2, name: 'John', last_name: 'Doe')
end
context 'having authors with the same Id' do
before do
add_author_resource(
before_batch_import: lambda do |importer|
Author.where(id: importer.values_at('id')).delete_all
end
)
visit '/admin/authors/import'
upload_file!(:authors_with_ids)
end
it 'should replace authors' do
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
it 'should replace authors by id' do
expect(Author.find(1).name).to eq('John')
expect(Author.find(2).name).to eq('Jane')
end
end
end
context 'with valid options' do
let(:options) { {} }
before do
add_author_resource(options)
visit '/admin/authors/import'
end
it 'has valid form' do
form = find('#new_active_admin_import_model')
expect(form['action']).to eq('/admin/authors/do_import')
expect(form['enctype']).to eq('multipart/form-data')
file_input = form.find('input#active_admin_import_model_file')
expect(file_input[:type]).to eq('file')
expect(file_input.value).to be_blank
submit_input = form.find('#active_admin_import_model_submit_action input')
expect(submit_input[:value]).to eq('Import')
expect(submit_input[:type]).to eq('submit')
end
context 'with hint defined' do
let(:options) do
{ template_object: ActiveAdminImport::Model.new(hint: 'hint') }
end
it 'renders hint at upload page' do
expect(page).to have_content options[:template_object].hint
end
end
context 'when importing file' do
[:empty, :only_headers].each do |file|
context "when #{file} file" do
it 'should render warning' do
upload_file!(file)
expect(page).to have_content I18n.t('active_admin_import.file_empty_error')
expect(Author.count).to eq(0)
end
end
end
context 'when no file' do
it 'should render error' do
find_button('Import').click
expect(Author.count).to eq(0)
expect(page).to have_content I18n.t('active_admin_import.no_file_error')
end
end
context 'auto detect encoding' do
include_examples 'successful inserts',
:auto,
:authors_win1251_win_endline
end
context 'Win1251' do
include_examples 'successful inserts',
'windows-1251',
:authors_win1251_win_endline
end
context 'BOM' do
it 'should import file with many records' do
upload_file!(:authors_bom)
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
end
context 'with headers' do
it 'should import file with many records' do
upload_file!(:authors)
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
it 'should import file with 1 record' do
upload_file!(:author)
expect(page).to have_content 'Successfully imported 1 author'
expect(Author.count).to eq(1)
end
end
context 'without headers' do
context 'with known csv headers' do
let(:options) do
attributes = { csv_headers: ['Name', 'Last name', 'Birthday'] }
{ template_object: ActiveAdminImport::Model.new(attributes) }
end
it 'should import file' do
upload_file!(:authors_no_headers)
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
end
context 'with unknown csv headers' do
it 'should render error' do
upload_file!(:authors_no_headers)
expect(page).to have_content 'Error:'
expect(Author.count).to eq(0)
end
end
end
context 'with invalid data insert on DB constraint' do
# :name field has an uniq index
it 'should render error' do
upload_file!(:authors_invalid_db)
expect(page).to have_content 'Error:'
expect(Author.count).to eq(0)
end
end
context 'with invalid data insert on model validation' do
let(:options) { { validate: true } }
before do
Author.create!(name: 'John', last_name: 'Doe')
end
it 'should render both successful and failed message' do
upload_file!(:authors_invalid_model)
expect(page).to have_content 'Failed to import 1 author'
expect(page).to have_content 'Successfully imported 1 author'
expect(page).to have_content 'Last name has already been taken - Doe'
expect(Author.count).to eq(2)
end
context 'use batch_transaction to make transaction work on model validation' do
let(:options) { { validate: true, batch_transaction: true } }
it 'should render only the failed message' do
upload_file!(:authors_invalid_model)
expect(page).to have_content 'Failed to import 1 author'
expect(page).to_not have_content 'Successfully imported'
expect(Author.count).to eq(1)
end
end
end
context 'with invalid records' do
context 'with validation' do
it 'should render error' do
upload_file!(:author_invalid)
expect(page).to have_content 'Failed to import 1 author'
expect(Author.count).to eq(0)
end
end
context 'without validation' do
let(:options) { { validate: false } }
it 'should render error' do
upload_file!(:author_invalid)
expect(page).to have_content 'Successfully imported 1 author'
expect(Author.count).to eq(1)
end
end
end
context 'when zipped' do
context 'when allowed' do
it 'should import file' do
with_zipped_csv(:authors) do
upload_file!(:authors, :zip)
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
end
end
context 'when not allowed' do
let(:options) do
attributes = { allow_archive: false }
{ template_object: ActiveAdminImport::Model.new(attributes) }
end
it 'should render error' do
with_zipped_csv(:authors) do
upload_file!(:authors, :zip)
expect(page).to have_content I18n.t('active_admin_import.file_format_error')
expect(Author.count).to eq(0)
end
end
end
context 'when zipped with Win1251 file' do
let(:options) do
attributes = { force_encoding: :auto }
{ template_object: ActiveAdminImport::Model.new(attributes) }
end
it 'should import file' do
with_zipped_csv(:authors_win1251_win_endline) do
upload_file!(:authors_win1251_win_endline, :zip)
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
end
end
end
context 'with different header attribute names' do
let(:options) do
{ headers_rewrites: { :'Second name' => :last_name } }
end
it 'should import file' do
upload_file!(:author_broken_header)
expect(page).to have_content 'Successfully imported 1 author'
expect(Author.count).to eq(1)
end
end
context 'with semicolons separator' do
let(:options) do
attributes = { csv_options: { col_sep: ';' } }
{ template_object: ActiveAdminImport::Model.new(attributes) }
end
it 'should import file' do
upload_file!(:authors_with_semicolons)
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
end
context 'with tab separator' do
let(:options) do
attributes = { csv_options: { col_sep: "\t" } }
{ template_object: ActiveAdminImport::Model.new(attributes) }
end
it 'should import file' do
upload_file!(:authors_with_tabs, 'tsv')
expect(page).to have_content 'Successfully imported 2 authors'
expect(Author.count).to eq(2)
end
end
context 'with empty csv and auto detect encoding' do
let(:options) do
attributes = { force_encoding: :auto }
{ template_object: ActiveAdminImport::Model.new(attributes) }
end
before do
upload_file!(:empty)
end
it 'should render warning' do
expect(page).to have_content I18n.t('active_admin_import.file_empty_error')
expect(Author.count).to eq(0)
end
end
context 'with csv which has exceeded values' do
before do
upload_file!(:authors_values_exceeded_headers)
end
it 'should render warning' do
# 5 columns: 'birthday, name, last_name, created_at, updated_at'
# 6 values: '"1988-11-16", "Jane", "Roe", " exceeded value", datetime, datetime'
msg = 'Number of values (6) exceeds number of columns (5)'
expect(page).to have_content I18n.t('active_admin_import.file_error', message: msg)
expect(Author.count).to eq(0)
end
end
end
context 'with callback procs options' do
let(:options) do
{
before_import: ->(_) { true },
after_import: ->(_) { true },
before_batch_import: ->(_) { true },
after_batch_import: ->(_) { true }
}
end
it 'should call each callback' do
expect(options[:before_import]).to receive(:call).with(kind_of(ActiveAdminImport::Importer))
expect(options[:after_import]).to receive(:call).with(kind_of(ActiveAdminImport::Importer))
expect(options[:before_batch_import]).to receive(:call).with(kind_of(ActiveAdminImport::Importer))
expect(options[:after_batch_import]).to receive(:call).with(kind_of(ActiveAdminImport::Importer))
upload_file!(:authors)
expect(Author.count).to eq(2)
end
context 'when the option before_import raises a ActiveAdminImport::Exception' do
let(:options) { { before_import: ->(_) { raise ActiveAdminImport::Exception, 'error message' } } }
before { upload_file!(:authors) }
it 'should show error' do
expect(page).to have_content I18n.t('active_admin_import.file_error', message: 'error message')
expect(Author.count).to eq(0)
end
end
context 'when the option before_batch_import raises a ActiveAdminImport::Exception' do
let(:options) { { before_batch_import: ->(_) { raise ActiveAdminImport::Exception, 'error message' } } }
before { upload_file!(:authors) }
it 'should show error' do
expect(page).to have_content I18n.t('active_admin_import.file_error', message: 'error message')
expect(Author.count).to eq(0)
end
end
end
end
context "with slice_columns option" do
let(:batch_size) { 2 }
before do
add_author_resource template_object: ActiveAdminImport::Model.new,
before_batch_import: lambda { |importer|
importer.batch_slice_columns(slice_columns)
},
batch_size: batch_size
visit "/admin/authors/import"
upload_file!(:authors)
end
context "slice last column and superfluous column" do
let(:slice_columns) { %w(name last_name not_existing_column) }
shared_examples_for "birthday column removed" do
it "should not fill `birthday` column" do
expect(Author.pluck(:name, :last_name, :birthday)).to match_array(
[
["Jane", "Roe", nil],
["John", "Doe", nil]
]
)
end
end
it_behaves_like "birthday column removed"
context "when doing more than one batch" do
let(:batch_size) { 1 }
it_behaves_like "birthday column removed"
end
end
context "slice column from the middle" do
let(:slice_columns) { %w(name birthday) }
it "should not fill `last_name` column" do
expect(Author.pluck(:name, :last_name, :birthday)).to match_array(
[
["Jane", nil, "1988-11-16".to_date],
["John", nil, "1986-05-01".to_date]
]
)
end
end
end
context 'with invalid options' do
let(:options) { { invalid_option: :invalid_value } }
it 'should raise TypeError' do
expect { add_author_resource(options) }.to raise_error(ArgumentError)
end
end
context 'when submitting empty form after validation error' do
let(:options) { {} }
before do
add_author_resource(options)
visit '/admin/authors/import'
end
it 'should NOT reuse cached file from previous submission' do
expect do
upload_file!(:author_broken_header)
expect(page).to have_content("can't write unknown attribute")
end.not_to change { Author.count }
# Second submission without selecting a file
expect do
find_button('Import').click
expect(page).to have_content(I18n.t('active_admin_import.no_file_error'))
end.not_to change { Author.count }
end
end
context 'when importing file with invalid format and auto force_encoding' do
let(:options) { { template_object: ActiveAdminImport::Model.new(force_encoding: :auto) } }
before do
add_author_resource(options)
visit '/admin/authors/import'
end
it 'should reject invalid file format before encoding' do
expect do
upload_file!(:author_invalid_format, 'txt')
expect(page).to have_content I18n.t('active_admin_import.file_format_error')
end.not_to change { Author.count }
end
end
context 'with active_admin_import_context defined on the controller' do
before { Author.create!(name: 'John', last_name: 'Doe') }
let(:author) { Author.take }
context 'when context returns request-derived attributes' do
before do
author_id = author.id
add_post_resource(
template_object: ActiveAdminImport::Model.new(author_id: author_id),
before_batch_import: lambda do |importer|
ip = importer.model.request_ip
a_id = importer.model.author_id
importer.csv_lines.map! { |row| row << ip << a_id }
importer.headers.merge!(:'Request Ip' => :request_ip, :'Author Id' => :author_id)
end,
controller_block: proc do
def active_admin_import_context
{ request_ip: request.remote_ip }
end
end
)
visit '/admin/posts/import'
upload_file!(:posts_for_author)
end
it 'merges the context into the import model so callbacks see it' do
expect(page).to have_content 'Successfully imported 2 posts'
expect(Post.count).to eq(2)
Post.all.each do |post|
expect(post.request_ip).to eq('127.0.0.1')
expect(post.author_id).to eq(author.id)
end
end
end
context 'when context returns parent id for a nested belongs_to resource' do
let(:post) { Post.create!(title: 'A post', body: 'body', author: author) }
before do
add_nested_post_comment_resource(
before_batch_import: lambda do |importer|
importer.csv_lines.map! { |row| row << importer.model.post_id }
importer.headers.merge!(:'Post Id' => :post_id)
end,
controller_block: proc do
def active_admin_import_context
{ post_id: parent.id }
end
end
)
visit "/admin/posts/#{post.id}/post_comments/import"
upload_file!(:post_comments)
end
it 'automatically assigns the parent post_id to every imported comment' do
expect(page).to have_content 'Successfully imported 2 post comments'
expect(PostComment.count).to eq(2)
expect(PostComment.where(post_id: post.id).count).to eq(2)
end
end
end
# PG-only: activerecord-import populates `result.ids` reliably on PostgreSQL
# via RETURNING. On MySQL/SQLite the array is not populated by default, so
# the assertion would not be meaningful there. The :result_class option
# itself works on every adapter — this spec just demonstrates the canonical
# PR #191 use case (collecting inserted ids) on the adapter that supports it.
if ENV['DB'] == 'postgres'
context 'with custom result_class (PostgreSQL)' do
# Subclass that captures inserted ids alongside the standard counters.
# Lives in user-land so the gem itself stays free of adapter-specific
# quirks around RETURNING. This is the example documented in the README.
class ImportResultWithIds < ActiveAdminImport::ImportResult
attr_reader :ids
def initialize
super
@ids = []
end
def add(batch_result, qty)
super
@ids.concat(Array(batch_result.ids))
end
end
before do
add_author_resource(result_class: ImportResultWithIds) do |result, _options|
# Expose the captured ids on the flash so the test asserts via the
# rendered page rather than closure capture.
flash[:notice] = "Imported ids: [#{result.ids.sort.join(',')}]"
end
visit '/admin/authors/import'
upload_file!(:authors)
end
it 'collects the ids of inserted records via the custom subclass' do
expect(Author.count).to eq(2)
expected = "Imported ids: [#{Author.pluck(:id).sort.join(',')}]"
expect(page).to have_content(expected)
end
end
end
end