Skip to content
This repository was archived by the owner on Mar 29, 2022. It is now read-only.

Commit 75a774a

Browse files
committed
Add spec for PullRequest
1 parent 311da5e commit 75a774a

File tree

3 files changed

+319
-42
lines changed

3 files changed

+319
-42
lines changed

app/models/pull_request.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ class PullRequest < ApplicationRecord
88

99
state_machine initial: :new do
1010
event :spam_repo do
11-
transition %i[new waiting eligible] => :spam_repo,
11+
transition %i[new waiting] => :spam_repo,
1212
if: ->(pr) { pr.spammy? }
1313
end
1414

1515
event :invalid_label do
16-
transition %i[new waiting eligible] => :invalid_label,
16+
transition %i[new waiting] => :invalid_label,
1717
if: ->(pr) { pr.labelled_invalid? }
1818
end
1919

@@ -30,11 +30,13 @@ class PullRequest < ApplicationRecord
3030
before_transition to: %i[waiting],
3131
from: %i[new] do |pr, _transition|
3232
pr.waiting_since = pr.created_at
33+
pr.save!
3334
end
3435

3536
before_transition to: %i[waiting],
3637
from: %i[spam_repo invalid_label] do |pr, _transition|
3738
pr.waiting_since = Time.zone.now
39+
pr.save!
3840
end
3941
end
4042

spec/models/pull_request_spec.rb

Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
# frozen_string_literal: true
2+
3+
require 'rails_helper'
4+
5+
RSpec.describe PullRequest, type: :model do
6+
describe '#labelled_invalid?' do
7+
context 'Pull request has a label for "invalid"' do
8+
let(:pr) { pr_helper(INVALID_LABEL_PR) }
9+
10+
it 'is considered labelled invalid' do
11+
expect(pr.labelled_invalid?).to eq(true)
12+
end
13+
14+
context "Pull request is merged" do
15+
let(:pr) { pr_helper(ELIGIBLE_INVALID_MERGED_PR) }
16+
17+
it 'is not considered labelled invalid' do
18+
expect(pr.labelled_invalid?).to eq(false)
19+
end
20+
end
21+
end
22+
23+
context 'Pull request has a label for "❌ Invalid"' do
24+
let(:pr) { pr_helper(INVALID_EMOJI_LABEL_PR) }
25+
26+
it 'is considered labelled labelled invalid' do
27+
expect(pr.labelled_invalid?).to eq(true)
28+
end
29+
end
30+
31+
context 'Pull request has no labels' do
32+
let(:pr) { pr_helper(ELIGIBLE_PR) }
33+
34+
it 'is not considered labelled invalid' do
35+
expect(pr.labelled_invalid?).to eq(false)
36+
end
37+
end
38+
end
39+
40+
describe '#spam_repo' do
41+
# TODO -- need to stub SpamRepositoryService
42+
# PR is in spam repo initially
43+
# PR is waiting, then becomes in spam repo
44+
# PR is eligible, then becomes in spam repo
45+
end
46+
47+
describe '#invalid_label' do
48+
context 'Pull request is labelled invalid initially' do
49+
let(:pr) { pr_helper(INVALID_LABEL_PR) }
50+
51+
it 'is put it in the invalid_label state' do
52+
expect(pr.state).to eq('invalid_label')
53+
end
54+
end
55+
56+
context 'Pull request is waiting' do
57+
let(:pr) { pr_helper(IMMATURE_PR) }
58+
59+
it 'is in the waiting state initially' do
60+
expect(pr.state).to eq('waiting')
61+
end
62+
63+
context 'Pull request becomes labelled invalid' do
64+
before do
65+
stub_helper(pr, INVALID_LABEL_PR, { 'id' => pr.github_id })
66+
pr.check_state
67+
end
68+
69+
it 'is put it in the invalid_label state' do
70+
expect(pr.state).to eq('invalid_label')
71+
end
72+
73+
context 'Seven days pass from pull request creation' do
74+
before do
75+
travel_to pr.waiting_since + 7.days
76+
pr.check_state
77+
end
78+
79+
it 'remains in the invalid_label state' do
80+
expect(pr.state).to eq('invalid_label')
81+
end
82+
83+
after { travel_back }
84+
end
85+
end
86+
end
87+
88+
context 'Pull request is eligible' do
89+
let(:pr) { pr_helper(ELIGIBLE_PR) }
90+
91+
it 'is in the eligible state initially' do
92+
expect(pr.state).to eq('eligible')
93+
end
94+
95+
context 'Pull request becomes labelled invalid' do
96+
before do
97+
stub_helper(pr, INVALID_LABEL_PR, { 'id' => pr.github_id })
98+
pr.check_state
99+
end
100+
101+
it 'remains in the eligible state' do
102+
expect(pr.state).to eq('eligible')
103+
end
104+
end
105+
end
106+
end
107+
108+
describe '#waiting' do
109+
context "Pull request is valid and created less than seven days ago" do
110+
let(:pr) { pr_helper(IMMATURE_PR) }
111+
112+
it 'is put it in the waiting state' do
113+
expect(pr.state).to eq('waiting')
114+
end
115+
116+
it 'has the waiting_since date set to the created date' do
117+
expect(pr.waiting_since).to eq(pr.created_at)
118+
end
119+
end
120+
121+
context "Pull request is in an invalid repo initially" do
122+
# TODO -- need to stub SpamRepositoryService
123+
end
124+
125+
context "Pull request is labelled invalid initially" do
126+
let(:pr) { pr_helper(INVALID_LABEL_PR) }
127+
128+
it 'is in the invalid_label state initially' do
129+
expect(pr.state).to eq('invalid_label')
130+
end
131+
132+
it 'has no set waiting_since' do
133+
expect(pr.waiting_since).to be_nil
134+
end
135+
136+
context 'Pull request has invalid label removed' do
137+
before do
138+
freeze_time
139+
140+
stub_helper(pr, IMMATURE_PR, { 'id' => pr.github_id })
141+
pr.check_state
142+
end
143+
144+
it 'is put it in the waiting state' do
145+
expect(pr.state).to eq('waiting')
146+
end
147+
148+
it 'has the waiting_since date set to now' do
149+
expect(pr.waiting_since).to eq(Time.zone.now)
150+
expect(pr.waiting_since).to_not eq(pr.created_at)
151+
end
152+
153+
after { travel_back }
154+
end
155+
end
156+
157+
context "Pull request is labelled invalid initially" do
158+
let(:pr) { pr_helper(INVALID_LABEL_PR) }
159+
160+
it 'is in the invalid_label state initially' do
161+
expect(pr.state).to eq('invalid_label')
162+
end
163+
164+
it 'has no set waiting_since' do
165+
expect(pr.waiting_since).to be_nil
166+
end
167+
168+
context 'Pull request is merged' do
169+
before do
170+
freeze_time
171+
172+
stub_helper(pr, IMMATURE_INVALID_MERGED_PR,
173+
{ 'id' => pr.github_id })
174+
pr.check_state
175+
end
176+
177+
it 'is put it in the waiting state' do
178+
expect(pr.state).to eq('waiting')
179+
end
180+
181+
it 'has the waiting_since date set to now' do
182+
expect(pr.waiting_since).to eq(Time.zone.now)
183+
expect(pr.waiting_since).to_not eq(pr.created_at)
184+
end
185+
186+
after { travel_back }
187+
end
188+
end
189+
end
190+
191+
describe '#eligible' do
192+
context "Pull request is valid and created over seven days ago" do
193+
let(:pr) { pr_helper(ELIGIBLE_PR) }
194+
195+
it 'is put it in the eligible state' do
196+
expect(pr.state).to eq('eligible')
197+
end
198+
end
199+
200+
context "Pull request is valid and created less than seven days ago" do
201+
let(:pr) { pr_helper(IMMATURE_PR) }
202+
203+
it 'is put it in the waiting state' do
204+
expect(pr.state).to eq('waiting')
205+
end
206+
207+
it 'has the waiting_since date set to the created date' do
208+
expect(pr.waiting_since).to eq(pr.created_at)
209+
end
210+
211+
context "Seven days pass from pull request creation" do
212+
before do
213+
travel_to pr.waiting_since + 7.days
214+
pr.check_state
215+
end
216+
217+
it 'is put it in the eligible state' do
218+
expect(pr.state).to eq('eligible')
219+
end
220+
221+
after { travel_back }
222+
end
223+
end
224+
end
225+
226+
def pr_helper(hash)
227+
PullRequest.delete_all
228+
PullRequest.from_github_pull_request(github_pull_request(hash))
229+
end
230+
231+
def stub_helper(target, hash, merge = {})
232+
allow(target).to receive(:github_pull_request)
233+
.and_return(github_pull_request(hash.merge(merge)))
234+
end
235+
end

0 commit comments

Comments
 (0)