summaryrefslogtreecommitdiff
path: root/spec/models/merge_request/metrics_spec.rb
blob: 13ff239a30652f02bd87ffecc6192f85786d4bb9 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe MergeRequest::Metrics do
  describe 'associations' do
    it { is_expected.to belong_to(:merge_request) }
    it { is_expected.to belong_to(:target_project).class_name('Project') }
    it { is_expected.to belong_to(:latest_closed_by).class_name('User') }
    it { is_expected.to belong_to(:merged_by).class_name('User') }
  end

  describe 'scopes' do
    let_it_be(:metrics_1) { create(:merge_request).metrics.tap { |m| m.update!(merged_at: 10.days.ago) } }
    let_it_be(:metrics_2) { create(:merge_request).metrics.tap { |m| m.update!(merged_at: 5.days.ago) } }

    describe '.merged_after' do
      subject { described_class.merged_after(7.days.ago) }

      it 'finds the record' do
        is_expected.to eq([metrics_2])
      end

      it "doesn't include record outside of the filter" do
        is_expected.not_to include([metrics_1])
      end
    end

    describe '.merged_before' do
      subject { described_class.merged_before(7.days.ago) }

      it 'finds the record' do
        is_expected.to eq([metrics_1])
      end

      it "doesn't include record outside of the filter" do
        is_expected.not_to include([metrics_2])
      end
    end

    describe '.by_target_project' do
      let(:target_project) { metrics_1.target_project }

      subject { described_class.by_target_project(target_project) }

      it 'finds metrics record with the associated target project' do
        is_expected.to eq([metrics_1])
      end
    end
  end
end