summaryrefslogtreecommitdiff
path: root/spec/services/work_items/export_csv_service_spec.rb
blob: 0718d3b686a556141f784f8dc9d0d8333d651d2a (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe WorkItems::ExportCsvService, :with_license, feature_category: :team_planning do
  let_it_be(:user) { create(:user) }
  let_it_be(:group) { create(:group) }
  let_it_be(:project) { create(:project, :public, group: group) }
  let_it_be(:work_item_1) { create(:work_item, project: project) }
  let_it_be(:work_item_2) { create(:work_item, :incident, project: project) }

  subject { described_class.new(WorkItem.all, project) }

  def csv
    CSV.parse(subject.csv_data, headers: true)
  end

  context 'when import_export_work_items_csv flag is not enabled' do
    before do
      stub_feature_flags(import_export_work_items_csv: false)
    end

    it 'renders an error' do
      expect { subject.csv_data }.to raise_error(described_class::NotAvailableError)
    end
  end

  it 'renders csv to string' do
    expect(subject.csv_data).to be_a String
  end

  describe '#email' do
    # TODO - will be implemented as part of https://gitlab.com/gitlab-org/gitlab/-/issues/379082
    xit 'emails csv' do
      expect { subject.email(user) }.o change { ActionMailer::Base.deliveries.count }.from(0).to(1)
    end
  end

  it 'returns two work items' do
    expect(csv.count).to eq(2)
  end

  specify 'iid' do
    expect(csv[0]['Id']).to eq work_item_1.iid.to_s
  end

  specify 'title' do
    expect(csv[0]['Title']).to eq work_item_1.title
  end

  specify 'type' do
    expect(csv[0]['Type']).to eq('Issue')
    expect(csv[1]['Type']).to eq('Incident')
  end

  specify 'author name' do
    expect(csv[0]['Author']).to eq(work_item_1.author_name)
  end

  specify 'author username' do
    expect(csv[0]['Author Username']).to eq(work_item_1.author.username)
  end

  specify 'created_at' do
    expect(csv[0]['Created At (UTC)']).to eq(work_item_1.created_at.to_s(:csv))
  end

  it 'preloads fields to avoid N+1 queries' do
    control = ActiveRecord::QueryRecorder.new { subject.csv_data }

    create(:work_item, :task, project: project)

    expect { subject.csv_data }.not_to exceed_query_limit(control)
  end

  it_behaves_like 'a service that returns invalid fields from selection'
end