-
Notifications
You must be signed in to change notification settings - Fork 102
/
import_spec.rb
283 lines (215 loc) · 7.72 KB
/
import_spec.rb
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
require 'spec_helper'
describe 'import', type: :feature do
def with_zipped_csv(name, &block)
zip_file = File.expand_path("./spec/fixtures/files/#{name}.zip")
begin
Zip::File.open(zip_file, Zip::File::CREATE) do |z|
z.add "#{name}.csv", File.expand_path("./spec/fixtures/files/#{name}.csv")
end
instance_eval &block
ensure
File.delete zip_file rescue nil
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 "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 "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) {
{template_object: ActiveAdminImport::Model.new(hint: "hint")}
}
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 "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) {
{template_object: ActiveAdminImport::Model.new(csv_headers: ['Name', 'Last name', 'Birthday'])}
}
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" 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
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) { {
template_object: ActiveAdminImport::Model.new(allow_archive: false)
} }
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
end
context "with different header attribute names" do
let(:options) {
{
headers_rewrites: {:'Second name' => :last_name}
}
}
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) {
{template_object: ActiveAdminImport::Model.new(csv_options: {col_sep: ";"})}
}
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
end
context "with callback procs options" do
let(:options) { {
before_import: proc { |_|},
after_import: proc { |_|},
before_batch_import: proc { |_|},
after_batch_import: proc { |_|}
} }
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
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
end