Commit cec88ba6 authored by Gary Holtz's avatar Gary Holtz

Removes duplication in specs and removes extra code

parent 0c82bf04
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
class DiffsEntity < Grape::Entity class DiffsEntity < Grape::Entity
include DiffHelper include DiffHelper
include RequestAwareEntity include RequestAwareEntity
include Gitlab::Utils::StrongMemoize
expose :real_size expose :real_size
expose :size expose :size
......
...@@ -33,14 +33,10 @@ module API ...@@ -33,14 +33,10 @@ module API
end end
expose :prev_commit_id, if: { type: :full } do |commit| expose :prev_commit_id, if: { type: :full } do |commit|
next unless options[:prev_commit_id]
options[:prev_commit_id] options[:prev_commit_id]
end end
expose :next_commit_id, if: { type: :full } do |commit| expose :next_commit_id, if: { type: :full } do |commit|
next unless options[:next_commit_id]
options[:next_commit_id] options[:next_commit_id]
end end
......
...@@ -28,51 +28,45 @@ describe DiffsEntity do ...@@ -28,51 +28,45 @@ describe DiffsEntity do
end end
context "when a commit_id is passed" do context "when a commit_id is passed" do
let(:commits) { [nil] + merge_request.commits + [nil] } let(:commits) { merge_request.commits }
let(:entity) do let(:entity) do
described_class.new( described_class.new(
merge_request_diffs.first.diffs, merge_request_diffs.first.diffs,
request: request, request: request,
merge_request: merge_request, merge_request: merge_request,
merge_request_diffs: merge_request_diffs, merge_request_diffs: merge_request_diffs,
commit: @commit commit: commit
) )
end end
subject { entity.as_json } subject { entity.as_json }
context "when the passed commit is not the first or last in the group" do context "when the passed commit is not the first or last in the group" do
let(:commit) { commits.third }
it 'includes commit references for previous and next' do it 'includes commit references for previous and next' do
@commit = commits.third expect(subject[:commit][:prev_commit_id]).to eq(commits.second.id)
check_neighbor_commits(commits, @commit) expect(subject[:commit][:next_commit_id]).to eq(commits.fourth.id)
end end
end end
context "when the passed commit is the first in the group" do context "when the passed commit is the first in the group" do
let(:commit) { commits.first }
it 'includes commit references for nil and next' do it 'includes commit references for nil and next' do
@commit = commits.compact.last expect(subject[:commit][:prev_commit_id]).to eq(nil)
check_neighbor_commits(commits, @commit) expect(subject[:commit][:next_commit_id]).to eq(commits.second.id)
end end
end end
context "when the passed commit is the last in the group" do context "when the passed commit is the last in the group" do
let(:commit) { commits.last }
it 'includes commit references for previous and nil' do it 'includes commit references for previous and nil' do
@commit = commits.compact.first expect(subject[:commit][:prev_commit_id]).to eq(commits[-2].id)
check_neighbor_commits(commits, @commit) expect(subject[:commit][:next_commit_id]).to eq(nil)
end end
end end
end end
end end
private
def check_neighbor_commits(commits, commit)
index = commits.index(commit)
prev_commit = commits[index - 1]&.id
next_commit = commits[index + 1]&.id
expect(subject[:commit]).to include(:prev_commit_id, :next_commit_id)
expect(subject[:commit][:prev_commit_id]).to eq(prev_commit)
expect(subject[:commit][:next_commit_id]).to eq(next_commit)
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment