summaryrefslogtreecommitdiff
path: root/spec/models/resource_weight_event_spec.rb
blob: 8a37883d93318e65d2726a3068b3e2f6bf24b533 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ResourceWeightEvent, type: :model do
  it_behaves_like 'a resource event'
  it_behaves_like 'a resource event for issues'

  let_it_be(:user1) { create(:user) }
  let_it_be(:user2) { create(:user) }

  let_it_be(:issue1) { create(:issue, author: user1) }
  let_it_be(:issue2) { create(:issue, author: user1) }
  let_it_be(:issue3) { create(:issue, author: user2) }

  describe 'validations' do
    it { is_expected.not_to allow_value(nil).for(:issue) }
    it { is_expected.to allow_value(nil).for(:weight) }
  end

  describe 'associations' do
    it { is_expected.to belong_to(:issue) }
  end

  describe '.by_issue' do
    let_it_be(:event1) { create(:resource_weight_event, issue: issue1) }
    let_it_be(:event2) { create(:resource_weight_event, issue: issue2) }
    let_it_be(:event3) { create(:resource_weight_event, issue: issue1) }

    it 'returns the expected records for an issue with events' do
      events = ResourceWeightEvent.by_issue(issue1)

      expect(events).to contain_exactly(event1, event3)
    end

    it 'returns the expected records for an issue with no events' do
      events = ResourceWeightEvent.by_issue(issue3)

      expect(events).to be_empty
    end
  end

  describe '.created_after' do
    let!(:created_at1) { 1.day.ago }
    let!(:created_at2) { 2.days.ago }
    let!(:created_at3) { 3.days.ago }

    let!(:event1) { create(:resource_weight_event, issue: issue1, created_at: created_at1) }
    let!(:event2) { create(:resource_weight_event, issue: issue2, created_at: created_at2) }
    let!(:event3) { create(:resource_weight_event, issue: issue2, created_at: created_at3) }

    it 'returns the expected events' do
      events = ResourceWeightEvent.created_after(created_at3)

      expect(events).to contain_exactly(event1, event2)
    end

    it 'returns no events if time is after last record time' do
      events = ResourceWeightEvent.created_after(1.minute.ago)

      expect(events).to be_empty
    end
  end

  describe '#discussion_id' do
    let_it_be(:event) { create(:resource_weight_event, issue: issue1, created_at: Time.utc(2019, 12, 30)) }

    it 'returns the expected id' do
      allow(Digest::SHA1).to receive(:hexdigest)
                               .with("ResourceWeightEvent-#{event.id}-#{user1.id}")
                               .and_return('73d167c478')

      expect(event.discussion_id).to eq('73d167c478')
    end
  end
end