summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/github_import/importer/releases_importer_spec.rb
blob: 23ae026fb14243c0ebc60e6eaae36405a52cb91f (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
require 'spec_helper'

describe Gitlab::GithubImport::Importer::ReleasesImporter do
  let(:project) { create(:project) }
  let(:client) { double(:client) }
  let(:importer) { described_class.new(project, client) }
  let(:created_at) { Time.new(2017, 1, 1, 12, 00) }
  let(:updated_at) { Time.new(2017, 1, 1, 12, 15) }

  let(:release) do
    double(
      :release,
      tag_name: '1.0',
      body: 'This is my release',
      created_at: created_at,
      updated_at: updated_at
    )
  end

  describe '#execute' do
    it 'imports the releases in bulk' do
      release_hash = {
        tag_name: '1.0',
        description: 'This is my release',
        created_at: created_at,
        updated_at: updated_at
      }

      expect(importer).to receive(:build_releases).and_return([release_hash])
      expect(importer).to receive(:bulk_insert).with(Release, [release_hash])

      importer.execute
    end
  end

  describe '#build_releases' do
    it 'returns an Array containnig release rows' do
      expect(importer).to receive(:each_release).and_return([release])

      rows = importer.build_releases

      expect(rows.length).to eq(1)
      expect(rows[0][:tag]).to eq('1.0')
    end

    it 'does not create releases that already exist' do
      create(:release, project: project, tag: '1.0', description: '1.0')

      expect(importer).to receive(:each_release).and_return([release])
      expect(importer.build_releases).to be_empty
    end

    it 'uses a default release description if none is provided' do
      expect(release).to receive(:body).and_return('')
      expect(importer).to receive(:each_release).and_return([release])

      release = importer.build_releases.first

      expect(release[:description]).to eq('Release for tag 1.0')
    end
  end

  describe '#build' do
    let(:release_hash) { importer.build(release) }

    it 'returns the attributes of the release as a Hash' do
      expect(release_hash).to be_an_instance_of(Hash)
    end

    context 'the returned Hash' do
      it 'includes the tag name' do
        expect(release_hash[:tag]).to eq('1.0')
      end

      it 'includes the release description' do
        expect(release_hash[:description]).to eq('This is my release')
      end

      it 'includes the project ID' do
        expect(release_hash[:project_id]).to eq(project.id)
      end

      it 'includes the created timestamp' do
        expect(release_hash[:created_at]).to eq(created_at)
      end

      it 'includes the updated timestamp' do
        expect(release_hash[:updated_at]).to eq(updated_at)
      end
    end
  end

  describe '#each_release' do
    let(:release) { double(:release) }

    before do
      allow(project).to receive(:import_source).and_return('foo/bar')

      allow(client)
        .to receive(:releases)
        .with('foo/bar')
        .and_return([release].to_enum)
    end

    it 'returns an Enumerator' do
      expect(importer.each_release).to be_an_instance_of(Enumerator)
    end

    it 'yields every release to the Enumerator' do
      expect(importer.each_release.next).to eq(release)
    end
  end

  describe '#description_for' do
    it 'returns the description when present' do
      expect(importer.description_for(release)).to eq(release.body)
    end

    it 'returns a generated description when one is not present' do
      allow(release).to receive(:body).and_return('')

      expect(importer.description_for(release)).to eq('Release for tag 1.0')
    end
  end
end