merge_request_diff_commit_spec.rb 3.46 KB
Newer Older
1
2
require 'rails_helper'

3
describe MergeRequestDiffCommit do
4
  let(:merge_request) { create(:merge_request) }
5
  let(:project) { merge_request.project }
6
7

  describe '#to_hash' do
8
9
    subject { merge_request.commits.first }

10
    it 'returns the same results as Commit#to_hash, except for parent_ids' do
11
      commit_from_repo = project.repository.commit(subject.sha)
12
13
14
15
16
      commit_from_repo_hash = commit_from_repo.to_hash.merge(parent_ids: [])

      expect(subject.to_hash).to eq(commit_from_repo_hash)
    end
  end
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38

  describe '.create_bulk' do
    let(:sha_attribute)  { Gitlab::Database::ShaAttribute.new }
    let(:merge_request_diff_id) { merge_request.merge_request_diff.id }
    let(:commits) do
      [
        project.commit('5937ac0a7beb003549fc5fd26fc247adbce4a52e'),
        project.commit('570e7b2abdd848b95f2f578043fc23bd6f6fd24d')
      ]
    end
    let(:rows) do
      [
        {
          "message": "Add submodule from gitlab.com\n\nSigned-off-by: Dmitriy Zaporozhets \u003cdmitriy.zaporozhets@gmail.com\u003e\n",
          "authored_date": "2014-02-27T10:01:38.000+01:00".to_time,
          "author_name": "Dmitriy Zaporozhets",
          "author_email": "dmitriy.zaporozhets@gmail.com",
          "committed_date": "2014-02-27T10:01:38.000+01:00".to_time,
          "committer_name": "Dmitriy Zaporozhets",
          "committer_email": "dmitriy.zaporozhets@gmail.com",
          "merge_request_diff_id": merge_request_diff_id,
          "relative_order": 0,
39
          "sha": sha_attribute.serialize("5937ac0a7beb003549fc5fd26fc247adbce4a52e")
40
41
42
43
44
45
46
47
48
49
50
        },
        {
          "message": "Change some files\n\nSigned-off-by: Dmitriy Zaporozhets \u003cdmitriy.zaporozhets@gmail.com\u003e\n",
          "authored_date": "2014-02-27T09:57:31.000+01:00".to_time,
          "author_name": "Dmitriy Zaporozhets",
          "author_email": "dmitriy.zaporozhets@gmail.com",
          "committed_date": "2014-02-27T09:57:31.000+01:00".to_time,
          "committer_name": "Dmitriy Zaporozhets",
          "committer_email": "dmitriy.zaporozhets@gmail.com",
          "merge_request_diff_id": merge_request_diff_id,
          "relative_order": 1,
51
          "sha": sha_attribute.serialize("570e7b2abdd848b95f2f578043fc23bd6f6fd24d")
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
        }
      ]
    end

    subject { described_class.create_bulk(merge_request_diff_id, commits) }

    it 'inserts the commits into the database en masse' do
      expect(Gitlab::Database).to receive(:bulk_insert)
        .with(described_class.table_name, rows)

      subject
    end

    context 'with dates larger than the DB limit' do
      let(:commits) do
        # This commit's date is "Sun Aug 17 07:12:55 292278994 +0000"
        [project.commit('ba3343bc4fa403a8dfbfcab7fc1a8c29ee34bd69')]
      end
      let(:timestamp) { Time.at((1 << 31) - 1) }
      let(:rows) do
        [{
          "message": "Weird commit date\n",
          "authored_date": timestamp,
          "author_name": "Alejandro Rodríguez",
          "author_email": "alejorro70@gmail.com",
          "committed_date": timestamp,
          "committer_name": "Alejandro Rodríguez",
          "committer_email": "alejorro70@gmail.com",
          "merge_request_diff_id": merge_request_diff_id,
          "relative_order": 0,
82
          "sha": sha_attribute.serialize("ba3343bc4fa403a8dfbfcab7fc1a8c29ee34bd69")
83
84
85
86
87
88
89
90
91
92
93
        }]
      end

      it 'uses a sanitized date' do
        expect(Gitlab::Database).to receive(:bulk_insert)
          .with(described_class.table_name, rows)

        subject
      end
    end
  end
94
end