|
| 1 | +require 'rails_helper' |
| 2 | + |
| 3 | +RSpec.describe Builders::Distribution::PullRequests::ReviewCoverageRepository do |
| 4 | + describe '.call' do |
| 5 | + let(:from) { 4.weeks.ago } |
| 6 | + let(:to) { Time.zone.now } |
| 7 | + let(:repository_one) { create(:repository) } |
| 8 | + let(:repository_two) { create(:repository) } |
| 9 | + |
| 10 | + let!(:first_pull_request) do |
| 11 | + create(:pull_request, :merged, |
| 12 | + repository: repository_one, |
| 13 | + html_url: 'test_pr_url_one', |
| 14 | + merged_at: 6.hours.ago) |
| 15 | + end |
| 16 | + |
| 17 | + let!(:second_pull_request) do |
| 18 | + create(:pull_request, :merged, |
| 19 | + repository: repository_two, |
| 20 | + html_url: 'test_pr_url_two', |
| 21 | + merged_at: 14.hours.ago) |
| 22 | + end |
| 23 | + |
| 24 | + let!(:third_pull_request) do |
| 25 | + create(:pull_request, :merged, |
| 26 | + repository: repository_one, |
| 27 | + html_url: 'test_pr_url_three', |
| 28 | + merged_at: 26.hours.ago) |
| 29 | + end |
| 30 | + |
| 31 | + before do |
| 32 | + first_pull_request.review_coverage.update!(coverage_percentage: 0.25) |
| 33 | + second_pull_request.review_coverage.update!(coverage_percentage: 0.75) |
| 34 | + third_pull_request.review_coverage.update!(coverage_percentage: 0.50) |
| 35 | + end |
| 36 | + |
| 37 | + context 'with correct params' do |
| 38 | + subject do |
| 39 | + described_class.call( |
| 40 | + repository_name: repository_one.name, |
| 41 | + from: from, |
| 42 | + to: to |
| 43 | + ) |
| 44 | + end |
| 45 | + |
| 46 | + it 'returns data for 0-25% coverage' do |
| 47 | + expect(subject).to have_key('20-40') |
| 48 | + expect(subject['20-40'].first[:value]).to eq(25.0) |
| 49 | + end |
| 50 | + |
| 51 | + it 'returns data for 50-75% coverage' do |
| 52 | + expect(subject).to have_key('40-60') |
| 53 | + expect(subject['40-60'].first[:value]).to eq(50.0) |
| 54 | + end |
| 55 | + |
| 56 | + it 'does not return data for 25-50% coverage' do |
| 57 | + expect(subject).not_to have_key('10-20') |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + context 'when pull request has html_url attribute nil' do |
| 62 | + let!(:pull_request_html_url_nil) do |
| 63 | + create(:pull_request, :merged, |
| 64 | + repository: repository_one, |
| 65 | + html_url: nil, |
| 66 | + merged_at: 40.hours.ago) |
| 67 | + end |
| 68 | + |
| 69 | + before do |
| 70 | + pull_request_html_url_nil.review_coverage.update!(coverage_percentage: 0.90) |
| 71 | + end |
| 72 | + |
| 73 | + subject do |
| 74 | + described_class.call( |
| 75 | + repository_name: repository_one.name, |
| 76 | + from: from, |
| 77 | + to: to |
| 78 | + ) |
| 79 | + end |
| 80 | + |
| 81 | + it 'does not add the pull request to the data' do |
| 82 | + expect(subject).not_to have_key('60-80') |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context 'when pull request belongs to ignored user' do |
| 87 | + let(:ignored_user) { create(:user, login: 'ignored_user') } |
| 88 | + let!(:setting) { create(:setting, key: 'ignored_users', value: ignored_user.login) } |
| 89 | + |
| 90 | + let!(:ignored_pull_request) do |
| 91 | + create(:pull_request, :merged, |
| 92 | + repository: repository_one, |
| 93 | + merged_at: 6.hours.ago, |
| 94 | + owner: ignored_user) |
| 95 | + end |
| 96 | + |
| 97 | + before do |
| 98 | + ignored_pull_request.review_coverage.update!(coverage_percentage: 0.25) |
| 99 | + end |
| 100 | + |
| 101 | + subject do |
| 102 | + described_class.call( |
| 103 | + repository_name: repository_one.name, |
| 104 | + from: from, |
| 105 | + to: to |
| 106 | + ) |
| 107 | + end |
| 108 | + |
| 109 | + it 'does not include pull requests from ignored users' do |
| 110 | + expect(subject['20-40'].pluck(:html_url)).not_to include('ignored_pr_url') |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | +end |
0 commit comments