File: graph_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (97 lines) | stat: -rw-r--r-- 2,925 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Network::Graph, feature_category: :source_code_management do
  let(:project) { create(:project, :repository) }

  shared_examples 'a collection of commits' do
    it 'returns a list of commits' do
      commits = graph.commits

      expect(commits).not_to be_empty
      expect(commits).to all(be_kind_of(Network::Commit))
    end

    it 'sorts commits by commit date (descending)' do
      # Remove duplicate timestamps because they make it harder to
      # assert that the commits are sorted as expected.
      commits = graph.commits.uniq(&:date)
      sorted_commits = commits.sort_by(&:date).reverse

      expect(commits).not_to be_empty
      expect(commits.map(&:id)).to eq(sorted_commits.map(&:id))
    end

    it 'sorts children before parents for commits with the same timestamp' do
      commits_by_time = graph.commits.group_by(&:date)

      commits_by_time.each do |_, commits|
        commit_ids = commits.map(&:id)

        commits.each_with_index do |commit, index|
          parent_indexes = commit.parent_ids.map { |parent_id| commit_ids.find_index(parent_id) }.compact

          # All parents of the current commit should appear after it
          expect(parent_indexes).to all(be > index)
        end
      end
    end
  end

  describe '#initialize' do
    let(:graph) do
      described_class.new(project, 'refs/heads/master', project.repository.commit, nil)
    end

    it 'has initialized' do
      expect(graph).to be_a(described_class)
    end
  end

  describe '#commits' do
    let(:graph) { described_class.new(project, 'refs/heads/master', project.repository.commit, nil) }

    context 'when use_list_commits_rpc_network_graph FF is enabled' do
      let(:opts) do
        {
          revisions: %w[--tags --branches],
          pagination_params: { limit: 650 },
          reverse: false,
          order: :date,
          ref: 'refs/heads/master',
          skip: 0
        }
      end

      before do
        stub_feature_flags(use_list_commits_rpc_network_graph: true)
      end

      it 'only fetches the commits once using `list_all`', :request_store do
        expect(Gitlab::Git::Commit).to receive(:list_all)
                                         .with(project.repository.raw_repository, opts)
                                         .once
                                         .and_call_original

        graph
      end

      it_behaves_like 'a collection of commits'
    end

    context 'when use_list_commits_rpc_network_graph FF is disabled' do
      before do
        stub_feature_flags(use_list_commits_rpc_network_graph: false)
      end

      it 'only fetches the commits once using `find_all`', :request_store do
        expect(Gitlab::Git::Commit).to receive(:find_all).once.and_call_original

        graph
      end

      it_behaves_like 'a collection of commits'
    end
  end
end