@@ -46,24 +46,24 @@ def upload_file!(name, ext='csv')
4646
4747 context "posts index" do
4848 before do
49- Author . create! ( name : ' John' , last_name : ' Doe' )
50- Author . create! ( name : ' Jane' , last_name : ' Roe' )
51- add_post_resource ( {
52- validate : true ,
53- headers_rewrites : {
54- :'Author Name' => :author_id
55- } ,
56- before_batch_import : -> ( importer ) do
57- authors_names = importer . values_at ( :author_id )
58- # replacing author name with author id
59- authors = Author . where ( name : authors_names ) . pluck ( :name , :id )
60- #{"Jane" => 2, "John" => 1}
61- options = Hash [ *authors . flatten ]
62- importer . batch_replace ( :author_id , options )
63- end
64- } )
65- visit ' /admin/posts/import'
66- upload_file! ( :posts )
49+ Author . create! ( name : " John" , last_name : " Doe" )
50+ Author . create! ( name : " Jane" , last_name : " Roe" )
51+ add_post_resource ( {
52+ validate : true ,
53+ headers_rewrites : {
54+ :'Author Name' => :author_id
55+ } ,
56+ before_batch_import : -> ( importer ) do
57+ authors_names = importer . values_at ( :author_id )
58+ # replacing author name with author id
59+ authors = Author . where ( name : authors_names ) . pluck ( :name , :id )
60+ #{"Jane" => 2, "John" => 1}
61+ options = Hash [ *authors . flatten ]
62+ importer . batch_replace ( :author_id , options )
63+ end
64+ } )
65+ visit " /admin/posts/import"
66+ upload_file! ( :posts )
6767 end
6868
6969 it "should resolve author_id by author name" do
@@ -114,6 +114,37 @@ def upload_file!(name, ext='csv')
114114
115115 end
116116
117+ context "authors already exist" do
118+ before do
119+ Author . create! ( id : 1 , name : "Jane" , last_name : "Roe" )
120+ Author . create! ( id : 2 , name : "John" , last_name : "Doe" )
121+ end
122+
123+ context "having authors with the same Id" do
124+ before do
125+ add_author_resource ( {
126+ before_batch_import : -> ( importer ) do
127+ Author . where ( id : importer . values_at ( "id" ) ) . delete_all
128+ end
129+ } )
130+ visit "/admin/authors/import"
131+ upload_file! ( :authors_with_ids )
132+ end
133+
134+ it "should replace authors" do
135+ expect ( page ) . to have_content "Successfully imported 2 authors"
136+ expect ( Author . count ) . to eq ( 2 )
137+ end
138+
139+ it "should replace authors by id" do
140+ expect ( Author . find ( 1 ) . name ) . to eq ( "John" )
141+ expect ( Author . find ( 2 ) . name ) . to eq ( "Jane" )
142+ end
143+
144+ end
145+
146+ end
147+
117148 context "with valid options" do
118149
119150 let ( :options ) { { } }
@@ -322,10 +353,10 @@ def upload_file!(name, ext='csv')
322353 context "with callback procs options" do
323354 let ( :options ) do
324355 {
325- before_import : proc { | _ | } ,
326- after_import : proc { | _ | } ,
327- before_batch_import : proc { | _ | } ,
328- after_batch_import : proc { | _ | }
356+ before_import : -> ( _ ) { true } ,
357+ after_import : -> ( _ ) { true } ,
358+ before_batch_import : -> ( _ ) { true } ,
359+ after_batch_import : -> ( _ ) { true }
329360 }
330361 end
331362
0 commit comments