debian-mirror-gitlab/spec/services/import_csv/base_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

90 lines
2.3 KiB
Ruby
Raw Normal View History

2023-04-23 21:23:45 +05:30
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe ImportCsv::BaseService, feature_category: :importers do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:csv_io) { double }
subject { described_class.new(user, project, csv_io) }
shared_examples 'abstract method' do |method, args|
it "raises NotImplemented error when #{method} is called" do
if args
expect { subject.send(method, args) }.to raise_error(NotImplementedError)
else
expect { subject.send(method) }.to raise_error(NotImplementedError)
end
end
end
it_behaves_like 'abstract method', :email_results_to_user
it_behaves_like 'abstract method', :attributes_for, "any"
it_behaves_like 'abstract method', :validate_headers_presence!, "any"
it_behaves_like 'abstract method', :create_object_class
2023-05-27 22:25:52 +05:30
context 'when given a class' do
let(:importer_klass) do
Class.new(described_class) do
def attributes_for(row)
{ title: row[:title] }
end
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
def validate_headers_presence!(headers)
raise CSV::MalformedCSVError.new("Missing required headers", 1) unless headers.present?
end
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
def create_object_class
Class.new
end
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
def email_results_to_user
# no-op
2023-04-23 21:23:45 +05:30
end
end
2023-05-27 22:25:52 +05:30
end
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
let(:service) do
uploader = FileUploader.new(project)
uploader.store!(file)
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
importer_klass.new(user, project, uploader)
end
subject { service.execute }
it_behaves_like 'correctly handles invalid files'
describe '#detect_col_sep' do
using RSpec::Parameterized::TableSyntax
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
let(:file) { double }
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
before do
allow(service).to receive_message_chain('csv_data.lines.first').and_return(header)
2023-04-23 21:23:45 +05:30
end
2023-05-27 22:25:52 +05:30
where(:sep_character, :valid) do
'&' | false
'?' | false
';' | true
',' | true
"\t" | true
end
with_them do
let(:header) { "Name#{sep_character}email" }
2023-04-23 21:23:45 +05:30
2023-05-27 22:25:52 +05:30
it 'responds appropriately' do
if valid
expect(service.send(:detect_col_sep)).to eq sep_character
else
expect { service.send(:detect_col_sep) }.to raise_error(CSV::MalformedCSVError)
end
end
2023-04-23 21:23:45 +05:30
end
end
end
end