From c9826e73d7f5b44a858d43e865c4b654d527c40c Mon Sep 17 00:00:00 2001 From: vijendra Date: Sat, 4 Feb 2012 12:38:48 +0530 Subject: [PATCH 1/3] delete attachment. Issue #12 --- app/assets/images/destroy.png | Bin 0 -> 522 bytes app/controllers/attachments_controller.rb | 5 +++++ app/models/attachment.rb | 4 ++-- app/views/attachments/index.html.haml | 3 +++ config/locales/en.yml | 3 ++- config/routes.rb | 2 +- .../attachments_controller_spec.rb | 21 ++++++++++++++++++ spec/models/attachment_spec.rb | 8 ++++++- 8 files changed, 41 insertions(+), 5 deletions(-) create mode 100755 app/assets/images/destroy.png diff --git a/app/assets/images/destroy.png b/app/assets/images/destroy.png new file mode 100755 index 0000000000000000000000000000000000000000..76a1c72b56d92b29e36fc6c02c56d20f73086694 GIT binary patch literal 522 zcmV+l0`>igP)zhtLp>@2MUfQDiJIt t('attachments.delete_confirm'), :method => :delete) diff --git a/config/locales/en.yml b/config/locales/en.yml index 94fa03f..9b200f8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -35,6 +35,7 @@ en: new: Upload new CSV file upload: Upload proceed_to_mapping: Proceed to mapping + delete_confirm: "Please note that, this is irreversible process. This will delete associated imports. Are you sure?" mappings: title: one: "%{count} field: %{fields}" @@ -69,4 +70,4 @@ en: confirm_destroy: Really delete this record? step: "Step %{count} from %{total}:" errors: - sk_login_required: "Please login to SalesKing and navigate to this app from inside there." \ No newline at end of file + sk_login_required: "Please login to SalesKing and navigate to this app from inside there." diff --git a/config/routes.rb b/config/routes.rb index a97dc1a..abe2513 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ CsvImporter::Application.routes.draw do - resources :attachments, except: [:edit, :destroy] do + resources :attachments, except: [:edit] do resources :mappings, only: [:new, :create] resources :imports, only: [:new, :create] end diff --git a/spec/controllers/attachments_controller_spec.rb b/spec/controllers/attachments_controller_spec.rb index da251ad..4685fff 100644 --- a/spec/controllers/attachments_controller_spec.rb +++ b/spec/controllers/attachments_controller_spec.rb @@ -38,6 +38,13 @@ put :update, id: Factory(:attachment).id, attachment: {} end end + + describe "DELETE #destroy" do + it "triggers access_denied" do + controller.should_receive(:access_denied) + delete :destroy, id: Factory(:attachment).id + end + end end context "for authenticaned user" do @@ -173,5 +180,19 @@ end end end + + describe "DELETE destroy" do + it "destroys the requested attachment" do + expect { + delete :destroy, :id => @authorized_attachment.id + }.to change(Attachment, :count).by(-1) + end + + it "redirects to the attachments list after destroying the requested attachment" do + delete :destroy, :id => @authorized_attachment.id + response.should redirect_to(attachments_path) + end + end + end end diff --git a/spec/models/attachment_spec.rb b/spec/models/attachment_spec.rb index 95fd006..374732e 100644 --- a/spec/models/attachment_spec.rb +++ b/spec/models/attachment_spec.rb @@ -19,6 +19,12 @@ File.exist?(file_path).should be_false end + it "should silently ignore missing files on destroy" do + file_path = @attachment.full_filename + File.delete(file_path) + lambda {@attachment.destroy}.should_not raise_error(Errno::ENOENT) + end + it "parses csv data" do @attachment.rows.size.should == 2 @attachment.rows.first.size.should be > 1 @@ -28,7 +34,7 @@ @attachment.rows(1).size.should == 1 end - describe "different csv formats" do + describe "formats" do {'google_native_test_.csv' => 3, 'google_outlook_test.csv' => 3, 'test1.csv' => 2}.each do |csv_file, count| it "should able to read #{csv_file}" do attachment = Factory(:attachment, :uploaded_data => file_upload(csv_file)) From 0b21fb6d1cde107edb856072a450423896a6d56b Mon Sep 17 00:00:00 2001 From: vijendra Date: Sat, 4 Feb 2012 22:37:51 +0530 Subject: [PATCH 2/3] unit test for data row model. For issue #13 --- spec/factories.rb | 29 ++++++++++++++++++++++-- spec/fixtures/valid_data.csv | 2 ++ spec/models/data_row_spec.rb | 43 ++++++++++++++++++++++++++++++++++++ spec/models/import_spec.rb | 2 +- 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 spec/fixtures/valid_data.csv diff --git a/spec/factories.rb b/spec/factories.rb index 234f995..b27e94e 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -3,19 +3,44 @@ uploaded_data { file_upload('test1.csv') } col_sep ';' quote_char '"' - association :mapping + association :mapping, :factory => :mapping end factory :mapping do + end factory :mapping_element do - association :mapping + mapping source 0 target 'organization' end + factory :email_mapping_element, :class => MappingElement do + mapping + source 2 + target 'email' + end + + factory :gender_mapping_element, :class => MappingElement do + association :mapping, :factory => :mapping + source 3 + target 'gender' + conv_type 'enum' + conv_opts "{\"male\":\"Herr\",\" female\":\"Frau\"}" + end + + factory :birthday_mapping_element, :class => MappingElement do + association :mapping + source 4 + target 'birthday' + conv_type 'date' + conv_opts "{\"date\":\"%Y-%M-%d\"}" + + end + factory :import do association :attachment end + end diff --git a/spec/fixtures/valid_data.csv b/spec/fixtures/valid_data.csv new file mode 100644 index 0000000..c8612cd --- /dev/null +++ b/spec/fixtures/valid_data.csv @@ -0,0 +1,2 @@ +"Organization";"Name";"Email";"Gender";"Birthday" +"Test organization";"Test user male";"test_user1@email.com";"Herr";"1980-01-10" diff --git a/spec/models/data_row_spec.rb b/spec/models/data_row_spec.rb index 2772cb3..28c9efa 100644 --- a/spec/models/data_row_spec.rb +++ b/spec/models/data_row_spec.rb @@ -2,4 +2,47 @@ describe DataRow do it { should belong_to(:import) } + + describe "#populate_client" do + before :each do + @mapping = Factory(:mapping) + @attachment = Factory(:attachment, :uploaded_data => file_upload('valid_data.csv'), mapping: @mapping) + + @import = Factory.build(:import, attachment: @attachment) + @import.should_receive(:populate_data_rows).and_return(true) #To avoid firing data_row create + @import.save + @client = stub_sk_client + end + + context 'creating sk client record' do + before :each do + Factory(:mapping_element, mapping: @mapping) + Factory(:gender_mapping_element, mapping: @mapping) + Factory(:birthday_mapping_element, mapping: @mapping) + end + + context 'successful' do + before :each do + @client.should_receive(:save).and_return(true) + @client.should_receive(:id).and_return("some_uuid") + @data_row = @import.data_rows.new(data: @attachment.rows[1]) + @data_row.save! + end + + it "should save Client ID in corresponding data row" do + @data_row.sk_id.should == "some_uuid" + end + + it "should have converted enum field value" do + @client.gender.should == "male" + end + + it "should have formatted date field value" do + @client.birthday.should == "1980.01.10" + end + + end + end + + end end diff --git a/spec/models/import_spec.rb b/spec/models/import_spec.rb index 52e95f0..d898486 100644 --- a/spec/models/import_spec.rb +++ b/spec/models/import_spec.rb @@ -46,4 +46,4 @@ @import.should be_success end end -end \ No newline at end of file +end From 2ad7a167926ebd4e19e7124c5a5e80127b36040f Mon Sep 17 00:00:00 2001 From: vijendra Date: Sun, 5 Feb 2012 10:58:24 +0530 Subject: [PATCH 3/3] improved unit tests for data_row model. Related to issue #13 --- spec/factories.rb | 23 ++++++++---- spec/fixtures/valid_data.csv | 2 - spec/models/data_row_spec.rb | 71 ++++++++++++++++++++++++++++-------- 3 files changed, 71 insertions(+), 25 deletions(-) delete mode 100644 spec/fixtures/valid_data.csv diff --git a/spec/factories.rb b/spec/factories.rb index b27e94e..a042805 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -3,42 +3,51 @@ uploaded_data { file_upload('test1.csv') } col_sep ';' quote_char '"' - association :mapping, :factory => :mapping + association :mapping end factory :mapping do end + #data rwo will be like this ["Test organization", "Test", "male_user", "test_user1@email.com", "Herr", "1980-01-10"] factory :mapping_element do mapping source 0 target 'organization' end + factory :fname_mapping_element, :class => MappingElement do + mapping + source '1,2' + target 'first_name' + conv_type 'join' + end + factory :email_mapping_element, :class => MappingElement do mapping - source 2 + source 3 target 'email' end factory :gender_mapping_element, :class => MappingElement do - association :mapping, :factory => :mapping - source 3 + mapping + source 4 target 'gender' conv_type 'enum' conv_opts "{\"male\":\"Herr\",\" female\":\"Frau\"}" end factory :birthday_mapping_element, :class => MappingElement do - association :mapping - source 4 + mapping + source 5 target 'birthday' conv_type 'date' conv_opts "{\"date\":\"%Y-%M-%d\"}" - end + + factory :import do association :attachment end diff --git a/spec/fixtures/valid_data.csv b/spec/fixtures/valid_data.csv deleted file mode 100644 index c8612cd..0000000 --- a/spec/fixtures/valid_data.csv +++ /dev/null @@ -1,2 +0,0 @@ -"Organization";"Name";"Email";"Gender";"Birthday" -"Test organization";"Test user male";"test_user1@email.com";"Herr";"1980-01-10" diff --git a/spec/models/data_row_spec.rb b/spec/models/data_row_spec.rb index 28c9efa..bea96ea 100644 --- a/spec/models/data_row_spec.rb +++ b/spec/models/data_row_spec.rb @@ -6,43 +6,82 @@ describe "#populate_client" do before :each do @mapping = Factory(:mapping) - @attachment = Factory(:attachment, :uploaded_data => file_upload('valid_data.csv'), mapping: @mapping) + @attachment = Factory(:attachment, mapping: @mapping) @import = Factory.build(:import, attachment: @attachment) - @import.should_receive(:populate_data_rows).and_return(true) #To avoid firing data_row create + @import.should_receive(:populate_data_rows).and_return(true) #To avoid firing data_row create from after_save @import.save @client = stub_sk_client end context 'creating sk client record' do - before :each do - Factory(:mapping_element, mapping: @mapping) - Factory(:gender_mapping_element, mapping: @mapping) - Factory(:birthday_mapping_element, mapping: @mapping) - end - - context 'successful' do + context 'successfully' do before :each do + #TODO try moving these to factoris file. defining association with mapping in factory is not working at the moment. + Factory(:mapping_element, mapping: @mapping) + Factory(:gender_mapping_element, mapping: @mapping) + Factory(:birthday_mapping_element, mapping: @mapping) + Factory(:fname_mapping_element, mapping: @mapping ) + Factory(:mapping_element, mapping: @mapping, source: 6, target: 'address.zip') + Factory(:mapping_element, mapping: @mapping, source: 7, target: 'address.address1') + Factory(:mapping_element, mapping: @mapping, source: 8, target: 'address.city') + @csv_row = ["Test org", "T", "Male_user", "test@email.com", "Herr", "1980-01-10", '83620', 'Hubertstr. 205', 'Feldkirchen'] @client.should_receive(:save).and_return(true) @client.should_receive(:id).and_return("some_uuid") - @data_row = @import.data_rows.new(data: @attachment.rows[1]) + @data_row = @import.data_rows.new(data: @csv_row) @data_row.save! end it "should save Client ID in corresponding data row" do - @data_row.sk_id.should == "some_uuid" + @data_row.sk_id.should == 'some_uuid' + end + + it "should have organization field value" do + @client.organization.should == 'Test org' end it "should have converted enum field value" do - @client.gender.should == "male" + @client.gender.should == 'male' end it "should have formatted date field value" do - @client.birthday.should == "1980.01.10" + @client.birthday.should == '1980.01.10' end - end - end - + it "should join initial and first_name" do + @client.first_name.should == 'T Male_user' + end + + it "should create an address" do + @client.addresses[0].zip.should == '83620' + @client.addresses[0].address1.should == 'Hubertstr. 205' + @client.addresses[0].city.should == 'Feldkirchen' + end + end #successfully + + context 'fails' do + before :each do + @csv_row = ["", "T", "Male_user", "test@email.com", "Herr", "1980-01-10", '83620', 'Hubertstr. 205', 'Feldkirchen'] + @client.should_receive(:save).and_return(false) + @client.errors[:base] = 'Organisation or lastname must be present.' + @data_row = @import.data_rows.new(data: @csv_row) + @data_row.save! + end + + it "should not save Client ID in corresponding data row" do + @data_row.sk_id.should be_nil + end + + it "should save failed row as source" do + @data_row.source.should == @csv_row.to_csv(col_sep: @attachment.col_sep, quote_char: @attachment.quote_char) + end + + it "should save error log returned from client" do + @data_row.log.should == 'Organisation or lastname must be present.' + end + end #Fails + + end #creating sk client record + end end