22
33describe 'import' , type : :feature do
44
5+ shared_examples 'successful inserts' do |encoding , csv_file_name |
6+ let ( :options ) do
7+ attributes = { force_encoding : encoding }
8+ { template_object : ActiveAdminImport ::Model . new ( attributes ) }
9+ end
10+
11+ before do
12+ upload_file! ( csv_file_name )
13+ end
14+
15+ it "should import file with many records" do
16+ expect ( page ) . to have_content "Successfully imported 2 authors"
17+ expect ( Author . count ) . to eq ( 2 )
18+ Author . all . each do |author |
19+ expect ( author ) . to be_valid
20+ expect ( author . name ) . to be_present
21+ expect ( author . last_name ) . to be_present
22+ end
23+ end
24+
25+ end
26+
527 def with_zipped_csv ( name , &block )
628
729 zip_file = File . expand_path ( "./spec/fixtures/files/#{ name } .zip" )
@@ -25,7 +47,6 @@ def upload_file!(name, ext='csv')
2547 context "authors index" do
2648 before do
2749 add_author_resource
28-
2950 end
3051
3152 it "should navigate to import page" do
@@ -81,16 +102,14 @@ def upload_file!(name, ext='csv')
81102 end
82103
83104 context "with hint defined" do
84- let ( :options ) {
85- { template_object : ActiveAdminImport ::Model . new ( hint : "hint" ) }
86- }
105+ let ( :options ) do
106+ { template_object : ActiveAdminImport ::Model . new ( hint : "hint" ) }
107+ end
87108 it "renders hint at upload page" do
88109 expect ( page ) . to have_content options [ :template_object ] . hint
89110 end
90-
91111 end
92112
93-
94113 context "when importing file" do
95114
96115 [ :empty , :only_headers ] . each do |file |
@@ -111,26 +130,16 @@ def upload_file!(name, ext='csv')
111130 end
112131 end
113132
114- context "Win1251" do
115- let ( :options ) do
116- attributes = { force_encoding : "Windows-1251" }
117- { template_object : ActiveAdminImport ::Model . new ( attributes ) }
118- end
119-
120- before do
121- upload_file! ( :authors_win1251_win_endline )
122- end
123-
124- it "should import file with many records" do
125- expect ( page ) . to have_content "Successfully imported 2 authors"
126- expect ( Author . count ) . to eq ( 2 )
127- Author . all . each do |author |
128- expect ( author ) . to be_valid
129- expect ( author . name ) . to be_present
130- expect ( author . last_name ) . to be_present
131- end
132- end
133+ context "auto detect encoding" do
134+ include_examples 'successful inserts' ,
135+ :auto ,
136+ :authors_win1251_win_endline
137+ end
133138
139+ context "Win1251" do
140+ include_examples 'successful inserts' ,
141+ 'windows-1251' ,
142+ :authors_win1251_win_endline
134143 end
135144
136145 context "BOM" do
@@ -205,7 +214,7 @@ def upload_file!(name, ext='csv')
205214
206215
207216 context "without validation" do
208- let ( :options ) { { validate : false } }
217+ let ( :options ) { { validate : false } }
209218 it "should render error" do
210219 upload_file! ( :author_invalid )
211220 expect ( page ) . to have_content "Successfully imported 1 author"
@@ -248,7 +257,7 @@ def upload_file!(name, ext='csv')
248257 context "with different header attribute names" do
249258
250259 let ( :options ) do
251- { headers_rewrites : { :'Second name' => :last_name } }
260+ { headers_rewrites : { :'Second name' => :last_name } }
252261 end
253262
254263 it "should import file" do
@@ -299,7 +308,7 @@ def upload_file!(name, ext='csv')
299308 end
300309
301310 context "with invalid options" do
302- let ( :options ) { { invalid_option : :invalid_value } }
311+ let ( :options ) { { invalid_option : :invalid_value } }
303312
304313 it "should raise TypeError" do
305314 expect { add_author_resource ( options ) } . to raise_error ( ArgumentError )
0 commit comments